-->
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.  [ 2 posts ] 
Author Message
 Post subject: Cascaded persist out of order on one-to-one relationship
PostPosted: Tue Oct 26, 2010 4:09 pm 
Newbie

Joined: Tue Oct 26, 2010 3:43 pm
Posts: 2
I have an one-to-one, bidirectional entity relationship with shared keys. When I attempt to save the owner of the association I get a "null id generated" exception against the owned side of the relationship. I am utilizing hibernate-entitymanager and using spring for transaction management.

Owning Entity
Code:

@Entity
@Table(name = "lead")
public class Lead
{
   private Long leadId;

   private LeadAffiliate leadAffiliate;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getLeadId()
   {
      return leadId;
   }

   @OneToOne(cascade = CascadeType.ALL)
   @PrimaryKeyJoinColumn
   public LeadAffiliate getLeadAffiliate()
   {
      return leadAffiliate;
   }
}



Owned Entity
Code:

@Entity
@Table(name = "lead_affiliate")
public class LeadAffiliate
{
   private Long leadId;

   private Lead lead;

   @Id
   public Long getLeadId()
   {
      return leadId;
   }

   @MapsIdmappedBy = "leadAffiliate")
   @OneToOne(cascade = CascadeType.All)
   @PrimaryKeyJoinColumn
   @JoinColumn(name = "lead_id")
   public Lead getLead()
   {
      return lead;
   }
}


and the code below is being used to save the entity:

Code:

LeadAffiliate aff = new LeadAffiliate();

aff.setLead(lead);
lead.setLeadAffiliate(aff);

em.persist(lead);



This all works perfectly fine in hibernate 3.5.0-Final. When attempting to upgrade to 3.5.6-Final or 3.6.0.Final is when I start getting the "null id generated for LeadAffiliate" error:

Code:
javax.persistence.PersistenceException: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.sellingsource.bizdev.entities.LeadAffiliate
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
   at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:365)
   at $Proxy152.persist(Unknown Source)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:240)
   at $Proxy120.persist(Unknown Source)
   at com.sellingsource.common.dao.JpaGenericDao.create(JpaGenericDao.java:38)
   ... 64 more
Caused by: org.hibernate.id.IdentifierGenerationException: null id generated for:class com.sellingsource.bizdev.entities.LeadAffiliate
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123)
   at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
   at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
   at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:799)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:791)
   at org.hibernate.engine.EJB3CascadingAction$1.cascade(EJB3CascadingAction.java:48)
   at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:392)
   at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:335)
   at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:204)
   at org.hibernate.engine.Cascade.cascade(Cascade.java:161)
   at org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:450)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:282)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
   at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
   at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
   at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
   at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
   at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
   at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
   ... 77 more


As an aside, I am not sure that the annotations on Lead Affiliate were quite right to begin with. They worked, but seemed kind of kludgey. So I have since changed them to:

Code:

@Entity
@Table(name = "lead_affiliate")
public class LeadAffiliate
{
   private Long leadId;

   private Lead lead;

   @Id
   @GenericGenerator(name = "foreign", strategy = "foreign", parameters = {
               @org.hibernate.annotations.Parameter(name = "property", value="lead")
   })
   @GeneratedValue(generator = "foreign")
   public Long getLeadId()
   {
      return leadId;
   }

   @OneToOne(mappedBy = "leadAffiliate")
   @PrimaryKeyJoinColumn
   public Lead getLead()
   {
      return lead;
   }
}


However, with these changes I get the same result. (Works in 3.5.0 but not 3.5.6 or 3.6.0)

Is there a new way I need to be doing this or is this a bug? My concern is that my code is currently working because of a bug :/.


Top
 Profile  
 
 Post subject: Re: Cascaded persist out of order on one-to-one relationship
PostPosted: Tue Oct 26, 2010 7:13 pm 
Newbie

Joined: Tue Oct 26, 2010 3:43 pm
Posts: 2
It does appear that my code worked in 3.5.0 due to a bug.

The proper annotations are:

Code:
@Entity
@Table(name = "lead")
public class Lead
{
   private Long leadId;

   private LeadAffiliate leadAffiliate;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public Long getLeadId()
   {
      return leadId;
   }

   @OneToOne(cascade = CascadeType.ALL, mappedBy = "lead")
   public LeadAffiliate getLeadAffiliate()
   {
      return leadAffiliate;
   }
}


and

Code:
@Entity
@Table(name = "lead_affiliate")
public class LeadAffiliate
{
   private Long leadId;

   private Lead lead;

   @Id
   public Long getLeadId()
   {
      return leadId;
   }

   @OneToOne
   @MapsId
   @JoinColumn(name = "lead_id")
   public Lead getLead()
   {
      return lead;
   }
}


This works in both 3.5.6 and 3.6.0

There are two key differences, one is leveraging @MapsId as opposed to the foreign generator. The second is to put the mappedBy on the proper side of the relationship. It should be on the side that is owned in the relationship. I am guessing that this is specifically what confused the order.


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

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.