One-to-One Bidirectional Failure on Retention if @NotNull is Used

I have the following one-to-one relationship between objects Userand UserSetup:

@Entity
class User {

   @OneToOne(mappedBy = "user", optional = false, cascade = ALL)
   private UserSetup setup;

   public User() {
      this.setup = new UserSetup(this);
   }
}

and

@Entity
public class UserSetup {

    @OneToOne(cascade = ALL)
    @JoinColumn(name = "USER_ID", nullable = false, unique = true)
    private User user;

    public UserSetup(User user) {
        this.user = user;
    }
}

Everything works well, however, if I add @NotNullin setupto the field Userand call savein the repository User, it will fail:

: javax.persistence.RollbackException: org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:77)    org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:71)    org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)   ... 64 more : javax.validation.ConstraintViolationException: [] [javax.validation.groups.Default,] : [   ConstraintViolationImpl {interpolatedMessage = ' , propertyPath = setup, rootBeanclass= class User, messageTemplate = '{javax.validation.constraints.NotNull.message}'}]

? , setup . , , @NotNull @OneToOne, optional?

+4
2

, Hibernate, , Hibernate , .

, @NotNull setup User, , User - , UserSetup User.

+1

-, @Notnull User UserSetup. , , UserSetup, . , , , .

+1

Source: https://habr.com/ru/post/1695509/


All Articles