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