-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Transitive persistence foreigen cannot be null error
PostPosted: Fri Dec 03, 2010 10:48 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.