Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0
In this scenario I have 3 objects/tables:
A Parent class - BusinessRelationship
A joined-subclass - Client
A collection on the parent of addresses.
It seems that the primary cache in the session is not associating new addresses with the collection on the parent if the address is persisted individually(yes, I set the client on the address prior to persisting).
Is this a known limitation or could I be doing something wrong? I would think that regardless of how you added a particular record, the cache should recognize it.
Mapping documents:
<class name="BusinessRelationship" table="BUSINESS_RELATIONSHIP">
...
<set name="addresses" inverse="true" cascade="all,delete-orphan">
<key column="BUSINESS_RELATIONSHIP_ID"/>
<one-to-many class="Address"/>
</set>
...
<joined-subclass name="Client" extends="BusinessRelationship" table="CLIENT">
<key column="BUSINESS_RELATIONSHIP_ID"/>
...
<class name="Address" table="CONTACT_ADDRESS">
...
<many-to-one name="businessRelationship" column="BUSINESS_RELATIONSHIP_ID"/>
...
Code between sessionFactory.openSession() and session.close():
session.update(obj);
Full stack trace of any exception that occurs:
N/A
Name and version of the database you are using:
Oracle 9i
The generated SQL (show_sql=true):
********************** Setting up: testBusinessRelationshipAddress ************************************
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: insert into BUSINESS_RELATIONSHIP (VERSION, TYPE_CODE, BUSINESS_REFERENCE_NAME, BUSINESS_REFERENCE_ID, PRIOR_ID, ADMINISTRATOR_CODE, LAST_CHANGED_DATE, LAST_CHANGED_BY, EFFECTIVE_DATE, TERMINATION_DATE, CREATION_DATE, HEIRARCHY_LEVEL_1, HEIRARCHY_LEVEL_2, HEIRARCHY_LEVEL_3, HEIRARCHY_LEVEL_4, HEIRARCHY_LEVEL_5, BUSINESS_RELATIONSHIP_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into CLIENT (PARENT_BIZ_RELATIONSHIP_ID, SIGNED_DATE, THIRD_PARTY_IND, CROSS_SELL_IND, INACTIVE_DATE, REACTIVATE_DATE, DATE_FIRST_LOAN_CLOSED, CROSS_SELL_COMMENT, CREDIT_CARD_CODE, PROFITABILITY_STATUS, VOE_CODE, VOE_COMPANY, VOE_PHONE, SHARED_FEE_ACCOUNT_TYPE, SHARED_FEE_PAYMENT_TYPE, SHARED_FEE_AMOUNT_TYPE, SHARED_FEE_PERCENT, SHARED_FEE_AMOUNT, SHARED_FEE_PAYMENT_TERM, SHARED_FEE_DISCLOSE_ON_HUD, BUSINESS_RELATIONSHIP_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into CONTACT_ADDRESS (VERSION, BUSINESS_RELATIONSHIP_ID, TYPE_CODE, ADDRESS1, ADDRESS2, CITY, STATE_CODE, ZIP, COUNTRY_CODE, CONTACT_ADDRESS_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Client Id 7359
Address Count on Client after persisting new client with a new address: 1
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: insert into CONTACT_ADDRESS (VERSION, BUSINESS_RELATIONSHIP_ID, TYPE_CODE, ADDRESS1, ADDRESS2, CITY, STATE_CODE, ZIP, COUNTRY_CODE, CONTACT_ADDRESS_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Address Count on Client after persisting new address with client: 1
Hibernate: select ENTITY_ID_SEQ.nextval from dual
Hibernate: insert into CONTACT_ADDRESS (VERSION, BUSINESS_RELATIONSHIP_ID, TYPE_CODE, ADDRESS1, ADDRESS2, CITY, STATE_CODE, ZIP, COUNTRY_CODE, CONTACT_ADDRESS_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update BUSINESS_RELATIONSHIP set VERSION=?, TYPE_CODE=?, BUSINESS_REFERENCE_NAME=?, BUSINESS_REFERENCE_ID=?, PRIOR_ID=?, ADMINISTRATOR_CODE=?, LAST_CHANGED_DATE=?, LAST_CHANGED_BY=?, EFFECTIVE_DATE=?, TERMINATION_DATE=?, CREATION_DATE=?, HEIRARCHY_LEVEL_1=?, HEIRARCHY_LEVEL_2=?, HEIRARCHY_LEVEL_3=?, HEIRARCHY_LEVEL_4=?, HEIRARCHY_LEVEL_5=? where BUSINESS_RELATIONSHIP_ID=? and VERSION=?
Address Count on Client after persisting client with third address: 2
********************** Shutting down: testBusinessRelationshipAddress ************************************
Code:
Client client = getNewPersistedClient(); //adds one address
Long clientId = client.getId();
System.out.println("Client Id " + clientId);
System.out.println("Address Count on Client after persisting new client with a new address: " + client.getAddresses().size());
Address a = getAddress();
a.setBusinessRelationship(client);
Long id = delegate.persistAddress(a);
client = delegate.getClient(clientId);
System.out.println("Address Count on Client after persisting new address with client: " + client.getAddresses().size());
//On Client
client = delegate.getClient(clientId);
Set addresses = client.getAddresses();
Address a1 = getAddress();
a1.setBusinessRelationship(client);
addresses.add(a1);
client.setAddresses(addresses);
delegate.persistClient(client);
Client savedClient2 = delegate.getClient(clientId);
System.out.println("Address Count on Client after persisting client with third address: " + client.getAddresses().size());
assertNotNull(savedClient2);
Set as = savedClient2.getAddresses();
assertNotNull(as);
assertTrue(as.size() == 3);
assertEquals(addressZip,((Address)as.toArray()[0]).getZip());