-->
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: TransientObjectException - cannot save parent or child
PostPosted: Tue Apr 01, 2008 2:35 am 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
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


Top
 Profile  
 
 Post subject: TransientObjectException - cannot save parent or child
PostPosted: Thu Apr 24, 2008 5:19 pm 
Beginner
Beginner

Joined: Tue Sep 09, 2003 5:20 pm
Posts: 43
Here is what ended up fixing this. Not sure why this pattern stumps so many people. As I said before it happens every year or so to me:

On the Story object, took off all options except name= on the JoinColumn:
Code:
    @ManyToOne(optional=false,fetch=FetchType.LAZY)
    @JoinColumn(name="owner_id")
    public User getOwner() {
      return this.owner;
   }


On the User object ended up specifying it this way (no change).
Code:
    @OneToMany(mappedBy="owner",fetch = FetchType.LAZY,cascade=CascadeType.ALL)
    @OrderBy(clause="createdOn desc")
    public Set<Story> getStories() {
       // log.debug("stories='"+stories+"'");
        return stories;
    }


That was it - though I'm not sure of the rationale at this point.


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.