-->
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.  [ 3 posts ] 
Author Message
 Post subject: Session.get returns 'disconnected' object
PostPosted: Tue Dec 21, 2004 6:29 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 2:22 am
Posts: 41
Question:
My problem is: i need to compare a new instance of a User to the persisted instance before saving the new instance. to do this i am constructing the new instance manually and using that object to 'get' the persisted value from hibernate. i then would expect to have 2 different instances of the User object with different values so that i can compare them.
i can't work out why but the Session.get returns the object i just created and populates it's fields with the db values. why does this happen? do i have to clone the one i constructed before passing it into Session.get ?
Mapping documents:
Code:
<hibernate-mapping package="com.x.etime.jdo">
<class name="User" table="GUNN_USER">
   <cache usage="read-write" />   
   <composite-id>
        <key-property name="code" type="string" column="USERID" length="20"/>
        <key-property name="companyNo" type="string" column="COMPANY_NO" length="4"/>                       
    </composite-id>
    <property name="firstName" type="string" column="FIRST_NAME" not-null="true" length="40"/>
    <property name="lastName" type="string" column="LAST_NAME" not-null="true" length="40"/>
    <property name="email" type="string"    column="EMAIL_ADDRESS" length="150"/>
    <property name="enabled" type="boolean" column="ENABLED" not-null="true" length="1"/>   
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
// create user and populate identifier fields and some others
User constructedUser = new User();
constructedUser.setCode("mruser");
constructedUser.setCompanyNo("mrcompany");
constructedUser.setEmail("newemail@somewhere.com");
constructedUser.setFirstName("dick");
constructedUser.setLastName("rogers");
constructedUser.setEnabled(true);

// grab back persisted object
User persistedUser = (User) LocalSession.currentSession().get(constructedUser.getClass(), constructedUser);

// print objects
System.out.println(persistedUser);
System.out.println(constructedUser);

Output:
Code:
com.x.etime.jdo.User@176a40
com.x.etime.jdo.User@176a40

Hibernate version: 217c
Name and version of the database you are using:oracle 8i


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 21, 2004 10:32 pm 
Regular
Regular

Joined: Thu Nov 20, 2003 10:04 pm
Posts: 64
Location: Melbourne, Australia
Have you defined equals and hashcode correctly?

Might also be worth looking into:

1. Making a primary key class
2. Moving to a single key primary key


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 22, 2004 8:48 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 2:22 am
Posts: 41
equals and hashcode are implemented correctly:
I don't really like the idea of the primary key object, it seems a little overkill for this project. we'd end up having way too many classes. i think i'll try with a clone solution:
Code:
User persistedUser = (User) LocalSession.currentSession().get(constructedUser.getClass(), constructedUser.clone());
haven't tested that yet tho!


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