I have the way too familiar unsaved/transient object exception. I've read and re-read HIA, searched the forums for four days. Finally I'm posting, knowing its probably something quite simple.
I'm pretty new to Hibernate annotations. I though my problem was not setting inverse=true, but found out the "mappedBy='owner'" annotation attribute is the equivalent.
Any ideas on figuring out how to save my Story object would be appreciated.
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.2.5
Mapping documents:
@Entity
@Table(name="story",uniqueConstraints={@UniqueConstraint(columnNames={"owner_id", "title"} ) } )
public class Story extends BaseEntityOwned implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {return id;}
@Version
public Integer getVersion() {return version;}
@ManyToOne
@JoinColumn(name="status_id")
public Status getStatus() {return this.status;}
@ManyToOne(optional=false,fetch=FetchType.LAZY)
@JoinColumn(name="owner_id",insertable=true,
updatable=true,unique=false,nullable=false)
public User getOwner() {return this.owner;}
}
@Entity
@Table(name="user")
public class User extends BaseObject
implements Serializable, UserDetails, Auditable {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {return id;}
@Version
public Integer getVersion() {return version;}
@ManyToOne
@JoinColumn(name="status_id")
public Status getStatus() {return this.status;}
@OneToMany(mappedBy="owner",
fetch = FetchType.LAZY,cascade=CascadeType.ALL)
@OrderBy(clause="createdOn desc")
public Set<Story> getStories() {return stories;}
public void setStories(Set<Story> stories) {this.stories = stories;}
public Story addStory(Story story) {
story.setOwner(this);
this.stories.add(story);
return story;
}
public void removeStory(Story story) {
story.setOwner(null);
this.stories.remove(story);
}
}
Code between sessionFactory.openSession() and session.close():
// Populater story object from web page.
story.setTitle("Some title, etc.");
// ...
// Then set owner of story
storyOwner = userManager.getUser(story_owner_id[0]);
storyOwner.addStory(story);
// And set to the Active Status object
story.setStatus(this.getActiveStatus())
session.saveOrUpdate(storyOwner);
// Have also tried just saving Story:
// session.saveOrUpdate(story);
Full stack trace of any exception that occurs:
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.dsbg.paratale.model.User
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:597)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3123)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:479)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:204)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:127)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558)
... 175 more
Name and version of the database you are using:
MySql 5.0.45 on Mac OsX 10.5
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Problems with Session and transaction handling?
Read this:
http://hibernate.org/42.html
Thanks - Richard