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;
}
}