Hi,
I'm running into a foreign key cannot be null error trying to persist an entity using merge.
The reason is that the containing entity is inserted before the referenced entity. If I remove the not null restraint from the column, everything works. But according to the manual that should not be necessary.
I'm wondering if I'm doing something wrong here. Perhaps the inheritance hierarchy is playing a role here since in other places in the app, I don't see this issue.
Using Hibernate 3.6.0
Code:
@Entity
@DiscriminatorValue("RST")
@SecondaryTable(name="RestaurantOffer", pkJoinColumns =
{
@PrimaryKeyJoinColumn(name = "offerId", referencedColumnName = "offerId")
}
)
public class RestaurantOffer extends Offer{
private RestaurantRating rating = null;
public RestaurantOffer(){
rating = new RestaurantRating();
rating.setOffer(this);
}
@OneToOne(optional = false, cascade = CascadeType.ALL, orphanRemoval=true,fetch = FetchType.LAZY)
@JoinColumn(name = "FK_RatingID", nullable = false,updatable=false,table="RestaurantOffer")
@IndexedEmbedded
public RestaurantRating getRating() {
return rating;
}
[..]
}
@Entity
@DiscriminatorValue(value = "RST")
public class RestaurantRating extends Rating {
@OneToOne(mappedBy="rating",optional=false)
public RestaurantOffer getOffer() {
return offer;
}
Caused by: org.hibernate.exception.ConstraintViolationException: could not insert: [nl.msw.compraventa.model.RestaurantOffer]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2436)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2836)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:415)
at org.hibernate.event.def.DefaultMergeEventListener.mergeTransientEntity(DefaultMergeEventListener.java:341)
at org.hibernate.event.def.DefaultMergeEventListener.retryMergeTransientEntities(DefaultMergeEventListener.java:174)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:112)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:867)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:851)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:855)
at org.springframework.orm.hibernate3.HibernateTemplate$23.doInHibernate(HibernateTemplate.java:817)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:406)
... 108 more
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'FK_RatingID' cannot be null
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1038)
Kind regards,
Marc