-->
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.  [ 8 posts ] 
Author Message
 Post subject: subclass obj references unsaved transient instance of base
PostPosted: Mon Apr 28, 2008 1:42 pm 
Newbie

Joined: Mon Apr 28, 2008 11:21 am
Posts: 3
Hi,
I have a problem with 2 entities - a subclass (Person) that has a base class (SystemUser) with the JOINED Inheritance strategy.

I am using:
JBoss 4.2.2 GA, Hibernate version: 3.2.4 sp1, Hibernate EntityManager 3.2.1 GA


The Code:

Here is the code for each class:

Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public class SystemUser implements Serializable {
    private static final long serialVersionUID = 1L;
    private Long id;
    private String userName;
    private HashedAuthenticationToken passphrase;
   
    @Column(nullable=false,unique=true)
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @OneToOne(cascade=CascadeType.ALL, mappedBy="principal")
    public HashedAuthenticationToken getPassPhrase() {
        return passphrase;
    }

    public void setPassPhrase(HashedAuthenticationToken passphrase) {
        this.passphrase = passphrase;
    }
   
    public void setId(Long id) {
        this.id = id;
    }

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


Code:
@Entity
public class Person extends SystemUser implements Serializable {
   private static final long serialVersionUID = 1L;
   
    private String name;
    private PersonRegistrationRequest creationRequest;

    @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="person", optional=false)
    public PersonRegistrationRequest getCreationRequest()
    {
        return creationRequest;
    }

    public void setCreationRequest(PersonRegistrationRequest creationRequest)
    {
        this.creationRequest = creationRequest;
    }
    @Column(nullable=false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}



In my DAO the relevant code is:
Code:
@PersistenceContext(unitName=SystemConstants.PERSISTENCE_CONTEXT_UNIT_NAME) EntityManager manager;
    public Person savePerson(Person person){
        return manager.merge(person);
    }


At the time of calling savePerson, the person's Id is null, the person's name has a value, and the person's username and password also have values, although the Password's Id is null.
The person does have a reference to a an attached CreationRequest (it's Id is not null).

Quote:
Full stack trace of any exception that occurs:
16:18:25,640 ERROR [STDERR] org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xyz.middleTier.entities.SystemUser
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
16:18:25,640 ERROR [STDERR] at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
16:18:25,640 ERROR [STDERR] at org.hibernate.type.EntityType.replace(EntityType.java:253)
16:18:25,640 ERROR [STDERR] at org.hibernate.type.AbstractType.replace(AbstractType.java:153)
16:18:25,640 ERROR [STDERR] at org.hibernate.type.TypeFactory.replace(TypeFactory.java:515)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:377)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:179)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
16:18:25,640 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
16:18:25,640 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
16:18:25,640 ERROR [STDERR] at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:194)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
16:18:25,640 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
16:18:25,640 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
16:18:25,640 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
16:18:25,640 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
16:18:25,640 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)
16:18:25,640 ERROR [STDERR] ... 147 more



I really have tried to find out what is going on here on my own,
but this is really starting to hold up my project,
and It does seem to me that this should work - I do apologise for
polluting your forum if this turns out to be a real newbie question. :-)

Many thanks,
Kipster


Top
 Profile  
 
 Post subject: seen this before, but no solution
PostPosted: Tue Apr 29, 2008 2:35 am 
Newbie

Joined: Tue Apr 29, 2008 1:15 am
Posts: 1
I've seen this problem before, but I never got around to finding a solution because my project was cancelled.
Will be interesting to see what the guru's say about it!


Top
 Profile  
 
 Post subject: toplink
PostPosted: Tue Apr 29, 2008 7:39 am 
Newbie

Joined: Tue Apr 29, 2008 6:26 am
Posts: 1
Should be possibile - I got this same pattern working on Toplink previously - does it work with other inheritance strategies?


Top
 Profile  
 
 Post subject: Good suggestion, but not a solution :-(
PostPosted: Tue Apr 29, 2008 2:00 pm 
Newbie

Joined: Mon Apr 28, 2008 11:21 am
Posts: 3
Quote:
Posted: Tue Apr 29, 2008 1:39 pm Post subject: toplink

Should be possibile - I got this same pattern working on Toplink previously - does it work with other inheritance strategies?


Thanks for the suggestion, I tried
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

It resulted in the same exception (see below)


org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.xyz.middleTier.entities.SystemUser
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
18:19:06,062 ERROR [STDERR] at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
18:19:06,062 ERROR [STDERR] at org.hibernate.type.EntityType.replace(EntityType.java:253)
18:19:06,062 ERROR [STDERR] at org.hibernate.type.AbstractType.replace(AbstractType.java:153)
18:19:06,062 ERROR [STDERR] at org.hibernate.type.TypeFactory.replace(TypeFactory.java:515)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:377)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:179)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
18:19:06,062 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:687)
18:19:06,062 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:669)
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.CascadingAction$6.cascade(CascadingAction.java:245)
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
18:19:06,062 ERROR [STDERR] at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:194)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:123)
18:19:06,062 ERROR [STDERR] at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
18:19:06,062 ERROR [STDERR] at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
18:19:06,062 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
18:19:06,062 ERROR [STDERR] at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
18:19:06,062 ERROR [STDERR] at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:227)


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 02, 2008 11:27 am 
Newbie

Joined: Mon Apr 28, 2008 4:14 pm
Posts: 5
Hi,

Could you paste/attach all your files referenced here. As I couldn't see the Password Id and password variables.


Top
 Profile  
 
 Post subject: using merge
PostPosted: Sun May 04, 2008 4:31 am 
Senior
Senior

Joined: Sun Jun 11, 2006 10:41 am
Posts: 164
You are using merge, which requires the entity to already be attached, i.e. have an id. Who come it does not have an ID in your case? If it's a new entity, you should call persist() - not merge().


Top
 Profile  
 
 Post subject: Bauer disagrees
PostPosted: Sun Jun 22, 2008 5:51 pm 
Newbie

Joined: Mon Apr 28, 2008 11:21 am
Posts: 3
Code:
You are using merge, which requires the entity to already be attached, i.e. have an id. Who come it does not have an ID in your case? If it's a new entity, you should call persist() - not merge().


From Java Persistence with Hibernate, Bauer, King:
Quote:
The merge(item) call D results in several actions. First, Hibernate checks
whether a persistent instance in the persistence context has the same database
identifier as the detached instance you’re merging. In this case, this is true: item
and item2, which were loaded with get() C, have the same primary key value.
If there is an equal persistent instance in the persistence context, Hibernate
copies the state of the detached instance onto the persistent instance E. In other
words, the new description that has been set on the detached item is also set on
the persistent item2.
If there is no equal persistent instance in the persistence context, Hibernate
loads it from the database (effectively executing the same retrieval by identifier as
you did with get()) and then merges the detached state with the retrieved
object’s state.


p.s. I have since solved this problem - was actually completely unrelated to this post - apologies.
Quote:
Quote:


Top
 Profile  
 
 Post subject: subclass obj references unsaved transient instance of base
PostPosted: Tue Feb 17, 2009 9:58 am 
Newbie

Joined: Tue Feb 17, 2009 8:15 am
Posts: 1
Hi,

I'm having the same problem!
How did you solved it?

Thanks,

Douglas


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