После перехода из hibernate 5.1 в hibernate 5.4 мое приложение выдает следующее исключение: org.hibernate.PropertyValueException : свойство not-null ссылается на нулевое или временное значение : com. vermeg.apps.myJpaProject.Contract.customer
public class Application {
public static void main(String[] args) {
final EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.mydb.ds");
final EntityManager entityManager = entityManagerFactory.createEntityManager();
Customer customer = entityManager.find(Customer.class, new Integer(1));
customer.setLastName("Chemek");
Contract contract = new Contract();
contract.setContractType("type1");
contract.setCustomer(customer);
customer.setContract(contract);
entityManager.getTransaction().begin();
entityManager.merge(customer);
entityManager.getTransaction().commit();
}
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Customer {
@Id
@GeneratedValue
private Integer id;
private String firstName;
private String lastName;
@OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL)
@JoinColumns({ @JoinColumn(name = "contract_id", referencedColumnName = "id", unique = false, nullable = true) })
private Contract contract;
}
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Contract {
@Id
@GeneratedValue
private Integer id;
private String contractType;
@OneToOne(fetch = FetchType.LAZY, optional = false, mappedBy = "contract")
private Customer customer;
}
org.hibernate.PropertyValueException: not-null property references a null or transient value : com.vermeg.apps.myJpaProject.Contract.customer
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:813)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:776)
at org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:492)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:416)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:218)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:151)
at org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:519)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsPersistent(DefaultMergeEventListener.java:204)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:178)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:70)
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:783)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:770)
at com.vermeg.apps.myJpaProject.Application.main(Application.java:19)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : com.vermeg.apps.myJpaProject.Contract.customer
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:111)
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:55)