Problem Summary: I'm using detached objects with Hibernate. I have my
application working in a single process architecture and am now trying to separate the
client and server over RMI. When the client sends a request via RMI to the server to
login, the server returns a User object to the client. Before ending the Hibernate Session
on the server, I call Hibernate.initialize() to initialize the portions of User that the client
requires. When RMI tries to deserialize the User object on the client, a
ClassCastException is thrown (shown below): (Note: User is a subclass
of Person):
Information below...
Thank you in advance.
Rick
Hibernate version:
3.1rc1
Mapping documents:
Here are the Hibernate mapping file definitions for Person and Address:
Code:
<class name="com.toolcafe.common.model.Person" table="person">
<id name="id" column="id">
<generator class="com.toolcafe.common.database.UserHiLoGenerator"/>
</id>
<discriminator column="subclass" type="string"/>
<version name="versionNbr" column="version_nbr" type="long" unsaved-value="null" />
<component name="multiUserData" class="com.toolcafe.common.model.MultiUserData">
<property name="createdById" column="created_by_id"/>
<property name="userGroupId" column="user_group_id"/>
<property name="private" type="boolean"/>
<property name="lastServerVersionNbr" column="last_server_version_nbr"/>
<property name="lastClientVersionNbr" column="last_client_version_nbr"/>
<property name="lastUpdated" column="last_updated" type="timestamp"/>
<property name="synchComplete" column="synch_complete"/>
<property name="deleted" type="boolean"/>
<property name="auditTrail" type="serializable"/>
</component>
<property name="firstName" length="32">
<column name="first_name" index="firstName_index"/>
</property>
<property name="lastName" length="32">
<column name="last_name" index="lastName_index"/>
</property>
<property name="middleInitial" length="2"/>
<property name="workEmail" length="64">
<column name="work_email" index="workEmail_index"/>
</property>
<property name="homeEmail" length="64">
<column name="home_email" index="homeEmail_index"/>
</property>
<property name="salutation" column="salutation" length="32"/>
<set name="phoneNbrs" cascade="all">
<key column="person_id"/>
<one-to-many class="com.toolcafe.common.model.PhoneNbr"/>
</set>
<many-to-one name="company" column="company_id" cascade="all"/>
<many-to-one name="address" column="address_id" cascade="all"/>
<subclass name="com.toolcafe.common.model.Contact">
<property name="contactType" column="contact_type" length="64"/>
<property name="contactSummary" column="contact_summary" length="1024"/>
<property name="notes" column="notes" length="1024"/>
<property name="structuredNotes">
<column name="structured_notes"/>
</property>
<property name="title" column="title" length="64"/>
<property name="department" column="department" length="64"/>
<property name="source" length="64"/>
<property name="interest" length="64"/>
<many-to-one name="acctMgr" column="acct_mgr_id" cascade="all"/>
<many-to-one name="referredBy" column="referred_by_id" cascade="all"/>
<many-to-one name="assistant" column="assistant_id" cascade="all"/>
<subclass name="com.toolcafe.common.model.User">
<property name="username" length="32">
<column name="username" index="username_index"/>
</property>
<property name="password" length="32">
<column name="password" sql-type="VARCHAR(32)"/>
</property>
<property name="administrator" column="administrator"/>
<set name="userGroupPermissions" cascade="all">
<key column="user_id"/>
<one-to-many class="com.toolcafe.common.model.UserGroupPermission"/>
</set>
<set name="emailAccts" cascade="all">
<key column="user_id"/>
<one-to-many class="com.toolcafe.common.model.EmailAcct"/>
</set>
<many-to-one name="defaultEmailAcct" column="default_email_acct_id" cascade="all"/>
<many-to-one name="defaultUserGroup" column="default_user_group_id" cascade="all"/>
<property name="enabled"/>
</subclass>
</subclass>
</class>
<class name="com.toolcafe.common.model.Address" table="address">
<id name="id" column="id">
<generator class="com.toolcafe.common.database.UserHiLoGenerator"/>
</id>
<version name="versionNbr" column="version_nbr" type="long" unsaved-value="null" />
<property name="address1" column="address1" length="32"/>
<property name="address2" column="address2" length="32"/>
<property name="city" column="city" length="32"/>
<property name="state" column="state" length="2"/>
<property name="country" column="country" length="32"/>
<property name="postalCode" column="postal_code" length="12"/>
</class>
Here's how I create the SessionFactory:Code:
Properties hiProps = new Properties();
hiProps.setProperty("hibernate.connection.url", dbUrl);
hiProps.setProperty("hibernate.connection.username", dbCfg.getUsername());
hiProps.setProperty("hibernate.connection.password", dbCfg.getPassword());
hiProps.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
hiProps.setProperty("hibernate.connection.pool_size", String.valueOf(10));
hiProps.setProperty("hibernate.statement_cache.size", String.valueOf(10));
hiProps.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
hiProps.setProperty("hibernate.max_fetch_depth", "3");
hiProps.setProperty("hibernate.connection.isolation",
Integer.toString(Connection.TRANSACTION_READ_COMMITTED));
hiProps.setProperty("hibernate.transaction.factory_class", "org.hibernate.transaction.JDBCTransactionFactory");
Configuration cfg = new Configuration();
cfg.addProperties(hiProps);
cfg.addResource(dbCfg.getHiConfigFile());
SessionFactory sessionFactory = cfg.buildSessionFactory();
Code between sessionFactory.openSession() and session.close():Following is a simplification of the actual code, but I think all the relevant parts are included:
Code:
Session s = _sessionFactory.openSession();
Transaction tx = s.beginTransaction();
// Do a case-insensitive search
List users =
_session.createQuery(
"from User as user where upper(user.username) = ?")
.setString(0, username.toUpperCase())
.list();
User user = null;
if (users.size() == 1) {
user = (User) (users.get(0));
// Test for correct login....
// Prepare the object for the client
Hibernate.initialize(user);
Hibernate.initialize(user);
Hibernate.initialize(user.getCompany());
Hibernate.initialize(user.getAddress());
Hibernate.initialize(user.getPhoneNbrs());
// Initialize each ScheduledItem
Hibernate.initialize(user.getScheduledItems());
for (Iterator siIter = user.getScheduledItems().iterator(); siIter.hasNext(); ) {
ScheduledItem si = (ScheduledItem)siIter.next();
Hibernate.initialize(si.getAllParticipants());
for (Iterator opIter = si.getAllParticipants().iterator(); opIter.hasNext(); ) {
Contact op = (Contact)opIter.next();
Hibernate.initialize(op.getScheduledItems());
Hibernate.initialize(op.getPhoneNbrs());
op.getScheduledItems().iterator().hasNext();
}
}
// Initialize the UserGroupPermission objects
for (Iterator ugpIter = user.getUserGroupPermissions().iterator(); ugpIter.hasNext(); ) {
UserGroupPermission ugp = (UserGroupPermission)ugpIter.next();
Hibernate.initialize(ugp.getUserGroup());
}
// Initialize the EmailAcctS
for (Iterator emailIter = user.getEmailAccts().iterator(); emailIter.hasNext(); ) {
EmailAcct emailAcct = (EmailAcct)emailIter.next();
Hibernate.initialize(emailAcct);
}
Hibernate.initialize(user.getDefaultEmailAcct());
}
}
// Commit the txn and close the session
tx.commit();
s.close();
// Return the User to the client
return user;
Full stack trace of any exception that occurs:
java.lang.ClassCastException: cannot assign instance of java.lang.Long to field
com.toolcafe.common.model.Person._address of type
com.toolcafe.common.model.Address in instance of com.toolcafe.common.model.User
at java.io.ObjectStreamClass$FieldReflector.setObjFieldValues(ObjectStreamClass.java:1977)
at java.io.ObjectStreamClass.setObjFieldValues(ObjectStreamClass.java:1157)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1836)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1713)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:290)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:139)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:179)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy1.login(Unknown Source)
at com.toolcafe.contact.client.gui.LoginDialog.processServerLogin(LoginDialog.java:327)
Name and version of the database you are using:
HSQLDB version 1.8.0_2
The generated SQL (show_sql=true):
22:42:09,615 DEBUG JDBCTransaction:54 - begin
22:42:09,688 DEBUG JDBCTransaction:59 - current autocommit status: false
22:42:09,690 DEBUG SQL:344 - select user0_.id as id7_, user0_.version_nbr as
version3_7_, user0_.created_by_id as created4_7_, user0_.user_group_id as
user5_7_, user0_.private as private7_, user0_.last_server_version_nbr as last7_7_,
user0_.last_client_version_nbr as last8_7_, user0_.last_updated as last9_7_,
user0_.synch_complete as synch10_7_, user0_.deleted as deleted7_, user0_.auditTrail
as auditTrail7_, user0_.first_name as first13_7_, user0_.last_name as last14_7_,
user0_.middleInitial as middleI15_7_, user0_.work_email as work16_7_,
user0_.home_email as home17_7_, user0_.salutation as salutation7_,
user0_.company_id as company19_7_, user0_.address_id as address20_7_,
user0_.contact_type as contact21_7_, user0_.contact_summary as contact22_7_,
user0_.notes as notes7_, user0_.structured_notes as structured24_7_, user0_.title as
title7_, user0_.department as department7_, user0_.source as source7_,
user0_.interest as interest7_, user0_.acct_mgr_id as acct29_7_, user0_.referred_by_id
as referred30_7_, user0_.assistant_id as assistant31_7_, user0_.username as
username7_, user0_.password as password7_, user0_.administrator as adminis34_7_,
user0_.default_email_acct_id as default35_7_, user0_.default_user_group_id as
default36_7_, user0_.enabled as enabled7_ from person user0_ where
user0_.subclass='com.toolcafe.common.model.User' and upper(user0_.username)=?
22:42:09,710 DEBUG StringType:79 - binding 'RICK' to parameter: 1
22:42:09,714 DEBUG StringType:123 - returning 'rick-32769' as column: id7_
22:42:09,716 DEBUG LongType:123 - returning '0' as column: version3_7_
22:42:09,717 DEBUG StringType:123 - returning 'ROOT_USER' as column: created4_7_
22:42:09,719 DEBUG StringType:116 - returning null as column: user5_7_
22:42:09,722 DEBUG BooleanType:123 - returning 'false' as column: private7_
22:42:09,723 DEBUG LongType:123 - returning '0' as column: last7_7_
22:42:09,772 DEBUG LongType:123 - returning '0' as column: last8_7_
22:42:09,775 DEBUG TimestampType:116 - returning null as column: last9_7_
22:42:09,778 DEBUG BooleanType:123 - returning 'false' as column: synch10_7_
22:42:09,779 DEBUG BooleanType:123 - returning 'false' as column: deleted7_
22:42:09,780 DEBUG SerializableType:116 - returning null as column: auditTrail7_
22:42:09,782 DEBUG StringType:116 - returning null as column: first13_7_
22:42:09,783 DEBUG StringType:116 - returning null as column: last14_7_
22:42:09,784 DEBUG StringType:116 - returning null as column: middleI15_7_
22:42:09,785 DEBUG StringType:123 - returning
'rickhoro@comcast.net' as column: work16_7_
22:42:09,792 DEBUG StringType:116 - returning null as column: home17_7_
22:42:09,794 DEBUG StringType:116 - returning null as column: salutation7_
22:42:09,795 DEBUG StringType:116 - returning null as column: company19_7_
22:42:09,797 DEBUG StringType:116 - returning null as column: address20_7_
22:42:09,798 DEBUG StringType:116 - returning null as column: contact21_7_
22:42:09,799 DEBUG StringType:116 - returning null as column: contact22_7_
22:42:09,801 DEBUG StringType:116 - returning null as column: notes7_
22:42:09,804 DEBUG SerializableType:123 - returning '2c6d8085f3f2809fe3efedaef4efefece3e1e6e5aee3efededefeeaeedefe4e5ecaeceeff4e5f38043430a3facefb5828083cc8088dfe3efeef4e1e3f4f480a3cce3efedaff4efefece3e1e6e5afe3efededefeeafedefe4e5ecafc3efeef4e1e3f4bbcc808cdfece1f3f4d5f0e4e1f4e5e4f48090cceae1f6e1aff5f4e9ecafc4e1f4e5bbcc8086dfeeeff4e5f3f48095cceae1f6e1aff5f4e9ecafd3eff2f4e5e4d3e5f4bbf8f0f0f0f3f28091eae1f6e1aef5f4e9ecaed4f2e5e5d3e5f45d18d013156d07db838080f8f0f0f78480808080f8' as column: structured24_7_
22:42:09,810 DEBUG StringType:116 - returning null as column: title7_
22:42:09,814 DEBUG StringType:116 - returning null as column: department7_
22:42:09,815 DEBUG StringType:116 - returning null as column: source7_
22:42:09,816 DEBUG StringType:116 - returning null as column: interest7_
22:42:09,818 DEBUG StringType:116 - returning null as column: acct29_7_
22:42:09,824 DEBUG StringType:116 - returning null as column: referred30_7_
22:42:09,832 DEBUG StringType:116 - returning null as column: assistant31_7_
22:42:09,833 DEBUG StringType:123 - returning 'rick' as column: username7_
22:42:09,834 DEBUG StringType:123 - returning 'tqNhB' as column: password7_
22:42:09,876 DEBUG BooleanType:123 - returning 'true' as column: adminis34_7_
22:42:09,877 DEBUG StringType:123 - returning 'rick-65537' as column: default35_7_
22:42:09,878 DEBUG StringType:123 - returning 'rick-1' as column: default36_7_
22:42:09,880 DEBUG BooleanType:123 - returning 'true' as column: enabled7_
22:42:09,884 DEBUG SQL:344 - select phonenbrs0_.person_id as person9_1_, phonenbrs0_.id as id1_, phonenbrs0_.id as id0_0_, phonenbrs0_.version_nbr as version2_0_0_, phonenbrs0_.digits as digits0_0_, phonenbrs0_.phone_nbr_type as phone4_0_0_, phonenbrs0_.country_code as country5_0_0_, phonenbrs0_.phone_nbr_str as phone6_0_0_, phonenbrs0_.formatted_phone_nbr as formatted7_0_0_, phonenbrs0_.extension as extension0_0_ from phone_nbr phonenbrs0_ where phonenbrs0_.person_id=?
22:42:09,970 DEBUG StringType:79 - binding 'rick-32769' to parameter: 1
22:42:09,979 DEBUG SQL:344 - select usergroupp0_.user_id as user7_1_, usergroupp0_.id as id1_, usergroupp0_.id as id6_0_, usergroupp0_.user_group_id as user2_6_0_, usergroupp0_.create_allowed as create3_6_0_, usergroupp0_.read_allowed as read4_6_0_, usergroupp0_.update_allowed as update5_6_0_, usergroupp0_.delete_allowed as delete6_6_0_ from user_group_permission usergroupp0_ where usergroupp0_.user_id=?
22:42:09,984 DEBUG StringType:79 - binding 'rick-32769' to parameter: 1
22:42:09,991 DEBUG StringType:123 - returning 'rick-98305' as column: id6_0_
22:42:09,992 DEBUG StringType:123 - returning 'rick-2' as column: user2_6_0_
22:42:09,993 DEBUG BooleanType:123 - returning 'true' as column: create3_6_0_
22:42:09,994 DEBUG BooleanType:123 - returning 'true' as column: read4_6_0_
22:42:09,995 DEBUG BooleanType:123 - returning 'true' as column: update5_6_0_
22:42:09,996 DEBUG BooleanType:123 - returning 'false' as column: delete6_6_0_
22:42:09,996 DEBUG StringType:123 - returning 'rick-32769' as column: user7_1_
22:42:09,997 DEBUG StringType:123 - returning 'rick-98305' as column: id1_
22:42:10,019 DEBUG StringType:123 - returning 'rick-98306' as column: id6_0_
22:42:10,020 DEBUG StringType:123 - returning 'rick-1' as column: user2_6_0_
22:42:10,021 DEBUG BooleanType:123 - returning 'true' as column: create3_6_0_
22:42:10,022 DEBUG BooleanType:123 - returning 'true' as column: read4_6_0_
22:42:10,023 DEBUG BooleanType:123 - returning 'true' as column: update5_6_0_
22:42:10,024 DEBUG BooleanType:123 - returning 'true' as column: delete6_6_0_
22:42:10,025 DEBUG StringType:123 - returning 'rick-32769' as column: user7_1_
22:42:10,026 DEBUG StringType:123 - returning 'rick-98306' as column: id1_
22:42:10,058 DEBUG SQL:344 - select usergroup0_.id as id5_0_, usergroup0_.version_nbr as version2_5_0_, usergroup0_.group_name as group3_5_0_, usergroup0_.group_description as group4_5_0_ from user_group usergroup0_ where usergroup0_.id=?
22:42:10,114 DEBUG StringType:79 - binding 'rick-2' to parameter: 1
22:42:10,119 DEBUG LongType:123 - returning '0' as column: version2_5_0_
22:42:10,120 DEBUG StringType:123 - returning 'Personal' as column: group3_5_0_
22:42:10,121 DEBUG StringType:123 - returning '' as column: group4_5_0_
22:42:10,123 DEBUG SQL:344 - select usergroup0_.id as id5_0_, usergroup0_.version_nbr as version2_5_0_, usergroup0_.group_name as group3_5_0_, usergroup0_.group_description as group4_5_0_ from user_group usergroup0_ where usergroup0_.id=?
22:42:10,135 DEBUG StringType:79 - binding 'rick-1' to parameter: 1
22:42:10,138 DEBUG LongType:123 - returning '0' as column: version2_5_0_
22:42:10,146 DEBUG StringType:123 - returning 'Business' as column: group3_5_0_
22:42:10,147 DEBUG StringType:123 - returning '' as column: group4_5_0_
22:42:10,150 DEBUG SQL:344 - select emailaccts0_.user_id as user12_1_, emailaccts0_.id as id1_, emailaccts0_.id as id8_0_, emailaccts0_.version_nbr as version2_8_0_, emailaccts0_.created_by_id as created3_8_0_, emailaccts0_.user_group_id as user4_8_0_, emailaccts0_.private as private8_0_, emailaccts0_.last_server_version_nbr as last6_8_0_, emailaccts0_.last_client_version_nbr as last7_8_0_, emailaccts0_.last_updated as last8_8_0_, emailaccts0_.synch_complete as synch9_8_0_, emailaccts0_.deleted as deleted8_0_, emailaccts0_.auditTrail as auditTrail8_0_, emailaccts0_.user_id as user12_8_0_, emailaccts0_.acct_name as acct13_8_0_, emailaccts0_.pop3_server as pop14_8_0_, emailaccts0_.username as username8_0_, emailaccts0_.password as password8_0_, emailaccts0_.smtp_server as smtp17_8_0_, emailaccts0_.return_email_addr as return18_8_0_ from email_acct emailaccts0_ where emailaccts0_.user_id=?
22:42:10,154 DEBUG StringType:79 - binding 'rick-32769' to parameter: 1
22:42:10,156 DEBUG StringType:123 - returning 'rick-65537' as column: id8_0_
22:42:10,202 DEBUG LongType:123 - returning '1' as column: version2_8_0_
22:42:10,204 DEBUG StringType:123 - returning 'ROOT_USER' as column: created3_8_0_
22:42:10,210 DEBUG StringType:116 - returning null as column: user4_8_0_
22:42:10,212 DEBUG BooleanType:123 - returning 'false' as column: private8_0_
22:42:10,214 DEBUG LongType:123 - returning '0' as column: last6_8_0_
22:42:10,216 DEBUG LongType:123 - returning '0' as column: last7_8_0_
22:42:10,218 DEBUG TimestampType:116 - returning null as column: last8_8_0_
22:42:10,226 DEBUG BooleanType:123 - returning 'false' as column: synch9_8_0_
22:42:10,241 DEBUG BooleanType:123 - returning 'false' as column: deleted8_0_
22:42:10,243 DEBUG SerializableType:116 - returning null as column: auditTrail8_0_
22:42:10,248 DEBUG StringType:123 - returning 'rick-32769' as column: user12_8_0_
22:42:10,250 DEBUG StringType:123 - returning 'Comcast' as column: acct13_8_0_
22:42:10,252 DEBUG StringType:123 - returning 'mail.comcast.net' as column: pop14_8_0_
22:42:10,253 DEBUG StringType:123 - returning 'rickhoro' as column: username8_0_
22:42:10,256 DEBUG SerializableType:123 - returning '2c6d8085f5f28082dbc330a6e63062dd042c828080f8f08080808880f080e480f180f280b780b480e480b5' as column: password8_0_
22:42:10,258 DEBUG StringType:123 - returning 'smtp.comcast.net' as column: smtp17_8_0_
22:42:10,259 DEBUG StringType:123 - returning
'rickhoro@comcast.net' as column: return18_8_0_
22:42:10,261 DEBUG StringType:123 - returning 'rick-32769' as column: user12_1_
22:42:10,266 DEBUG StringType:123 - returning 'rick-65537' as column: id1_
22:42:10,269 DEBUG JDBCTransaction:103 - commit
22:42:10,274 DEBUG JDBCTransaction:116 - committed JDBC Connection