-->
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.  [ 4 posts ] 
Author Message
 Post subject: Cascade on one-to-many problem using annotations
PostPosted: Tue Jan 04, 2011 1:35 pm 
Newbie

Joined: Fri Jul 17, 2009 1:35 pm
Posts: 3
Hi everyone. I am having a problem with a fairly simple scenario. Given the following classes, Item and ItemMetaData, I would like to persist them in the following way:

Code:
ItemMetaData meta1 = new ItemMetaData();

Item itemA = new Item();
itemA.getMetaData().add(meta1);

getHibernateTemplate().save(itemA);


When I call this I get the exception "org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ItemMetaData". I have read a bunch of posts from other people that have experienced this and the solutions usually suggest setting the CascadeType to the hibernate CascadeType.SAVE_UPDATE but as you can see below, still doesn't work for me. Shouldn't I be able to persist a new child object by saving the new parent?

Thanks for any assistance.

Geoff

Code:
@Entity
@Table(name = "item")
public class Item implements Serializable {

   @Id
   @Column(name = "item_id", nullable = false)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer itemId;

   @OneToMany
   @Cascade(value={org.hibernate.annotations.CascadeType.SAVE_UPDATE})
   @JoinTable(name = "item_item_metadata", joinColumns = @JoinColumn(name = "item_id"), inverseJoinColumns = @JoinColumn(name = "item_metadata_id"))
   private Collection<ItemMetaData> metaData;

   public Collection<ItemMetaData> getMetaData() {
      return metaData;
   }

   public void setMetaData(Collection<ItemMetaData> metaData) {
      this.metaData = metaData;
   }
}


Code:
@Entity
@Table(name = "item_metadata")
public class ItemMetaData implements Serializable {

   @Id
   @Column(name = "item_metadata_id")
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer itemMetaDataId;

   public ItemMetaData() {
   }

   public Integer getItemMetaDataId() {
      return itemMetaDataId;
   }

   public void setItemMetaDataId(Integer itemMetaDataId) {
      this.itemMetaDataId = itemMetaDataId;
   }
}


Top
 Profile  
 
 Post subject: Re: Cascade on one-to-many problem using annotations
PostPosted: Fri Jan 28, 2011 1:08 pm 
Newbie

Joined: Fri Jul 17, 2009 1:35 pm
Posts: 3
There have been 40 views on this and nobody has commented. Is there something obvious that I am missing? I have better luck by calling getHibernateTemplate.merge rather than save but still see this issue in certain cases with merge. If I have asked a dumb question, feel free to let me know. I would just like to know that this is possible and a direction to go to resolve this issue.

Thanks.


Top
 Profile  
 
 Post subject: Re: Cascade on one-to-many problem using annotations
PostPosted: Fri Jan 28, 2011 3:48 pm 
Newbie

Joined: Wed Oct 06, 2010 1:35 pm
Posts: 6
This is probably not much help, but I have similar problems getting responses on these forums and the Spring forums as well. I think the people who work on these open source projects feel like they put in their time writing the code and someone else should answer the questions. (Maybe.)

If anybody has a different explanation, feel free to correct me. :)


Top
 Profile  
 
 Post subject: Re: Cascade on one-to-many problem using annotations
PostPosted: Fri Jan 28, 2011 5:00 pm 
Newbie

Joined: Sun May 09, 2010 4:48 am
Posts: 11
This would work but without a JoinTable...
To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is obviously not optimized from the number of needed statements.
Code:
@Entity
public class Troop {
    @OneToMany
    @JoinColumn(name="troop_fk") //we need to duplicate the physical information
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk", insertable=false, updatable=false)
    public Troop getTroop() {
    ...
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.