В приложении создана сущность:
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size(max = 50)
@Column
private String firstName;
@NotNull
@Size(max = 50)
@Column
private String lastName;
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "project_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Project project;
public User() {
super();
}
public User(@NotNull @Size(max = 50) String firstName, @NotNull @Size(max = 50) String lastName, Project project) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.project = project;
}
//Getters, Setters, etc.
репозиторий
public interface UserRepository extends JpaRepository<User, Long> {
Page<User> findByProjectId(Long userId, Pageable pageable);
}
В UserController есть два метода для создания объекта:
@GetMapping("/users/add")
public String showAddUser(Model model) {
User user = new User();
model.addAttribute("add", true);
model.addAttribute("user", user);
return "users/user-edit";
}
@PostMapping("/users/add")
public String addUser(Model model, @ModelAttribute("user") User user) {
try {
User newUser = userService.save(user);
return "redirect:/users/" + String.valueOf(newUser.getId());
} catch (Exception ex) {
String errorMessage = ex.getMessage();
logger.error(errorMessage);
model.addAttribute("errorMessage", errorMessage);
model.addAttribute("add", true);
return "users/user-edit";
}
}
на странице создания объекта (использую thymeleaf):
<form th:action="${add} ? @{/users/add} : @{/users/{userId}/edit(userId=${user.id})}"
th:object="${user}" method="POST">
<table border="0">
<tr th:if="${user.id}">
<td>ID</td>
<td>:</td>
<td th:utext="${user.id}">...</td>
</tr>
<tr>
<td>First Name</td>
<td>:</td>
<td><input type="text" th:field="*{firstName}" /></td>
</tr>
<tr>
<td>Last Name</td>
<td>:</td>
<td><input type="text" th:field="*{lastName}" /></td>
</tr>
</table>
<input type="submit" th:value="${add} ? 'Create' : 'Update'" />
</form>
При попытке создания объекта User (на html странице создания) без указания ссылки на объект project выпрыгивает
nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : ru.surpavel.bugtrackingsystem.entity.User.project
Если в классе User поле project сделать nullable = true, то выскакивает:
could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Как это исправить?