-->
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.  [ 1 post ] 
Author Message
 Post subject: Clarification on use of set and one-to-many
PostPosted: Thu Jun 17, 2004 1:44 am 
Newbie

Joined: Thu Jun 17, 2004 1:15 am
Posts: 7
Hibernate Version: 2.1 (Also using Spring)
Database: Oracle 8i

I have read through many posts regarding a similar issue to this, but I cannot find the correct solution.

Here is my environment:

Database Tables:
Code:
    CLIENT
        CLIENT_SEQ        NUMBER    PK
        Other Attributes...

    CLIENT_ADDRESS
        CLIENT_SEQ        NUMBER    PK FK (Ref CLIENT.CLIENT_SEQ)
        ADDRESS_TYPE      VARCHAR   PK
        Other Attributes...

Model Classes:
Code:
    public class Client
    {
        private Long id;
        private Set addresses = new HashSet();
    }

    public class Address
    {
        private String addressType;
        private String address1;
        private String address2;
        ...
    }

Basically, each Client object can have 0 or more Address objects attached to it. ie: A Client can have both a STREET and POSTAL Address.

I have attempted to model this as:

Code:
<hibernate-mapping schema="APM">
    <class name="apm.client.Client" table="CLIENT">
        <id name="id" type="long" column="CLIENT_SEQ">
            <generator class="sequence">
                <param name="sequence">
                    CLIENT_NUMBER_SEQ</param>
            </generator>
        </id>

    <set name="addresses">
        <key column="CLIENT_SEQ"/>
        <one-to-many class="apm.client.Address"/>
    </set>
</hibernate-mapping>

<hibernate-mapping schema="APM">
    <class name="apm.client.Address" table="CLIENT_ADDRESS">
        <id name="addressType" type="string" column="ADDRESS_TYPE">
            <generator class="assigned"/>
        </id>

        <property name="address1" column="ADDRESS_1" type="string"/>
        <property name="address2" column="ADDRESS_2" type="string"/>
    </class>
</hibernate-mapping>

Code for executing this:

Code:
// This is using the Spring interface
ClientDAO clientDAO = (ClientDAO)DAOFactory.getDAO("clientDAO");
Client client = new Client();

Address address = new Address();
address.setAddressType("POSTAL");
address.setAddress1("Address line 1");
address.setAddress2("Address line 2");

client.addAddress(address);
clientDAO.save(client);

When I execute the above code it works without any errors, but it does not insert the Address into the database. Infact, it seems to run the following:

INSERT CLIENT
UPDATE CLIENT
UPDATE CLIENT_ADDRESS

Which is obviously missing the INSERT CLIENT_ADDRESS.

Any help with this will be greatly appreciated.

Here is an excerpt from the execution log (interesting parts highlighted):

13:35:08,328 DEBUG [TransactionSynchronizationManager] Bound value [org.springframework.orm.hibernate.SessionHolder@24c4a3] for key [net.sf.hibernate.impl.SessionFactoryImpl@697b67] to thread [main]
13:35:08,338 DEBUG [TransactionSynchronizationManager] Bound value [org.springframework.jdbc.datasource.ConnectionHolder@554d32] for key [org.apache.commons.dbcp.BasicDataSource@4ce7a] to thread [main]
13:35:08,338 DEBUG [TransactionSynchronizationManager] Initializing transaction synchronization
Attempting to save: id: null
clientType: PERSON
contactMethod: PHONE
workNumber: 123456
faxNumber: 123456
emailAddress: blah@blah
taxFileNumber: 1087450508238
addressType: POSTAL
address1: Address 1
address2: Address 2
address3: null
address4: null
state: WA
postcode: 6000
title: Mr
surname: Forlin
givenNames: Ray
salutation: null
dateOfBirth: null
sex: M
homeNumber: null
mobileNumber: null
13:35:08,348 DEBUG [TransactionSynchronizationManager] Retrieved value [org.springframework.orm.hibernate.SessionHolder@24c4a3] for key [net.sf.hibernate.impl.SessionFactoryImpl@697b67] bound to thread [main]
13:35:08,348 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
13:35:08,348 DEBUG [SQL] select OTUSER.ASGARD_ACCOUNT_NUMBER.nextval from dual
Hibernate: select OTUSER.ASGARD_ACCOUNT_NUMBER.nextval from dual
13:35:08,348 DEBUG [BatcherImpl] preparing statement
13:35:08,408 DEBUG [SequenceGenerator] Sequence identifier generated: 566126
13:35:08,408 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
13:35:08,408 DEBUG [BatcherImpl] closing statement
13:35:08,408 DEBUG [SessionImpl] generated identifier: 566126
13:35:08,408 DEBUG [SessionImpl] saving [au.com.sealcorp.apm.client.Person#566126]
13:35:08,418 DEBUG [WrapVisitor] Wrapped collection in role: au.com.sealcorp.apm.client.Client.addresses
13:35:08,428 DEBUG [TransactionInterceptor] Invoking commit for transaction on method 'save' in class [au.com.sealcorp.apm.client.ClientDAO]
13:35:08,428 DEBUG [HibernateTransactionManager] Triggering beforeCommit synchronization
13:35:08,428 DEBUG [HibernateTransactionManager] Triggering beforeCompletion synchronization
13:35:08,428 INFO [HibernateTransactionManager] Initiating transaction commit
13:35:08,428 DEBUG [HibernateTransactionManager] Committing Hibernate transaction on session [net.sf.hibernate.impl.SessionImpl@402af3]
13:35:08,428 DEBUG [JDBCTransaction] commit
13:35:08,428 DEBUG [SessionImpl] flushing session
13:35:08,428 DEBUG [SessionImpl] Flushing entities and processing referenced collections
13:35:08,428 DEBUG [AbstractEntityPersister] au.com.sealcorp.apm.client.Person.clientType is dirty
13:35:08,428 DEBUG [AbstractEntityPersister] au.com.sealcorp.apm.client.Person.contactMethod is dirty
13:35:08,428 DEBUG [SessionImpl] Updating entity: [au.com.sealcorp.apm.client.Person#566126]
13:35:08,438 DEBUG [SessionImpl] Collection found: [au.com.sealcorp.apm.client.Client.addresses#566126], was: [<unreferenced>]
13:35:08,438 DEBUG [SessionImpl] Processing unreferenced collections
13:35:08,438 DEBUG [SessionImpl] Scheduling collection removes/(re)creates/updates
13:35:08,438 DEBUG [SessionImpl] Flushed: 1 insertions, 1 updates, 0 deletions to 1 objects
13:35:08,438 DEBUG [SessionImpl] Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections
13:35:08,448 DEBUG [Printer] listing entities:
13:35:08,448 DEBUG [Printer] au.com.sealcorp.apm.client.Person{clientType=PERSON, id=566126, emailAddress=blah@blah, faxNumber=123456, addresses=[Address#POSTAL], contactMethod=PHONE, homeNumber=null, surname=Forlin, dateOfBirth=null, salutation=null, givenNames=Ray, sex=M, title=Mr, taxFileNumber=1087450508238, mobileNumber=null, workNumber=123456}
13:35:08,448 DEBUG [SessionImpl] executing flush
13:35:08,448 DEBUG [NormalizedEntityPersister] Inserting entity: [au.com.sealcorp.apm.client.Person#566126]
13:35:08,448 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
13:35:08,448 DEBUG [SQL] insert into APM.CLIENT (CLIENT_TYPE, CONTACT_METHOD, WORK_NUMBER, FAX_NUMBER, EMAIL_ADDRESS, TAX_FILE_NUMBER, CLIENT_SEQ) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into APM.CLIENT (CLIENT_TYPE, CONTACT_METHOD, WORK_NUMBER, FAX_NUMBER, EMAIL_ADDRESS, TAX_FILE_NUMBER, CLIENT_SEQ) values (?, ?, ?, ?, ?, ?, ?)
13:35:08,448 DEBUG [BatcherImpl] preparing statement
13:35:08,448 DEBUG [BatcherImpl] about to open: 1 open PreparedStatements, 0 open ResultSets
13:35:08,448 DEBUG [SQL] insert into APM.PERSON (TITLE, SURNAME, GIVEN_NAMES, SALUTATION, DATE_OF_BIRTH, SEX, HOME_NUMBER, MOBILE_NUMBER, CLIENT_SEQ) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into APM.PERSON (TITLE, SURNAME, GIVEN_NAMES, SALUTATION, DATE_OF_BIRTH, SEX, HOME_NUMBER, MOBILE_NUMBER, CLIENT_SEQ) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
13:35:08,458 DEBUG [BatcherImpl] preparing statement
13:35:08,458 DEBUG [NormalizedEntityPersister] Dehydrating entity: [au.com.sealcorp.apm.client.Person#566126]
13:35:08,458 DEBUG [StringType] binding 'PERSON' to parameter: 1
13:35:08,458 DEBUG [StringType] binding 'PHONE' to parameter: 2
13:35:08,458 DEBUG [StringType] binding '123456' to parameter: 3
13:35:08,458 DEBUG [StringType] binding '123456' to parameter: 4
13:35:08,458 DEBUG [StringType] binding 'blah@blah' to parameter: 5
13:35:08,458 DEBUG [StringType] binding '1087450508238' to parameter: 6
13:35:08,458 DEBUG [LongType] binding '566126' to parameter: 7
13:35:08,458 DEBUG [StringType] binding 'Mr' to parameter: 1
13:35:08,458 DEBUG [StringType] binding 'Forlin' to parameter: 2
13:35:08,458 DEBUG [StringType] binding 'Ray' to parameter: 3
13:35:08,458 DEBUG [StringType] binding null to parameter: 4
13:35:08,468 DEBUG [TimestampType] binding null to parameter: 5
13:35:08,468 DEBUG [CharacterType] binding 'M' to parameter: 6
13:35:08,468 DEBUG [StringType] binding null to parameter: 7
13:35:08,468 DEBUG [StringType] binding null to parameter: 8
13:35:08,468 DEBUG [LongType] binding '566126' to parameter: 9
13:35:08,488 DEBUG [BatcherImpl] done closing: 1 open PreparedStatements, 0 open ResultSets
13:35:08,488 DEBUG [BatcherImpl] closing statement
13:35:08,488 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
13:35:08,488 DEBUG [BatcherImpl] closing statement
13:35:08,488 DEBUG [NormalizedEntityPersister] Updating entity: [au.com.sealcorp.apm.client.Person#566126]
13:35:08,488 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
13:35:08,488 DEBUG [SQL] update APM.CLIENT set CLIENT_TYPE=?, CONTACT_METHOD=?, WORK_NUMBER=?, FAX_NUMBER=?, EMAIL_ADDRESS=?, TAX_FILE_NUMBER=? where CLIENT_SEQ=?
Hibernate: update APM.CLIENT set CLIENT_TYPE=?, CONTACT_METHOD=?, WORK_NUMBER=?, FAX_NUMBER=?, EMAIL_ADDRESS=?, TAX_FILE_NUMBER=? where CLIENT_SEQ=?
13:35:08,498 DEBUG [BatcherImpl] preparing statement
13:35:08,498 DEBUG [NormalizedEntityPersister] Dehydrating entity: [au.com.sealcorp.apm.client.Person#566126]
13:35:08,498 DEBUG [StringType] binding 'PERSON' to parameter: 1
13:35:08,498 DEBUG [StringType] binding 'PHONE' to parameter: 2
13:35:08,498 DEBUG [StringType] binding '123456' to parameter: 3
13:35:08,498 DEBUG [StringType] binding '123456' to parameter: 4
13:35:08,498 DEBUG [StringType] binding 'blah@blah' to parameter: 5
13:35:08,498 DEBUG [StringType] binding '1087450508238' to parameter: 6
13:35:08,498 DEBUG [LongType] binding '566126' to parameter: 7
13:35:08,508 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
13:35:08,508 DEBUG [BatcherImpl] closing statement
13:35:08,508 DEBUG [BasicCollectionPersister] Inserting collection: [au.com.sealcorp.apm.client.Client.addresses#566126]
13:35:08,508 DEBUG [BatcherImpl] about to open: 0 open PreparedStatements, 0 open ResultSets
13:35:08,508 DEBUG [SQL] update APM.CLIENT_ADDRESS set CLIENT_SEQ=? where ADDRESS_TYPE=?
Hibernate: update APM.CLIENT_ADDRESS set CLIENT_SEQ=? where ADDRESS_TYPE=?
13:35:08,508 DEBUG [BatcherImpl] preparing statement
13:35:08,508 DEBUG [LongType] binding '566126' to parameter: 1
13:35:08,508 DEBUG [Cascades] id unsaved-value strategy NULL
13:35:08,508 DEBUG [StringType] binding 'POSTAL' to parameter: 2
13:35:08,508 DEBUG [BatcherImpl] Adding to batch
13:35:08,508 DEBUG [BasicCollectionPersister] done inserting collection: 1 rows inserted
13:35:08,508 DEBUG [BatcherImpl] Executing batch size: 1
13:35:08,518 DEBUG [BatcherImpl] success of batch update unknown: 0
13:35:08,518 DEBUG [BatcherImpl] done closing: 0 open PreparedStatements, 0 open ResultSets
13:35:08,518 DEBUG [BatcherImpl] closing statement
13:35:08,518 DEBUG [SessionImpl] post flush
13:35:08,518 DEBUG [SessionImpl] transaction completion
13:35:08,518 DEBUG [JDBCTransaction] re-enabling autocommit
13:35:08,518 DEBUG [HibernateTransactionManager] Triggering afterCompletion synchronization
13:35:08,518 DEBUG [TransactionSynchronizationManager] Clearing transaction synchronization
13:35:08,518 DEBUG [TransactionSynchronizationManager] Removed value [org.springframework.jdbc.datasource.ConnectionHolder@554d32] for key [org.apache.commons.dbcp.BasicDataSource@4ce7a] from thread [main]
13:35:08,518 DEBUG [TransactionSynchronizationManager] Removed value [org.springframework.orm.hibernate.SessionHolder@24c4a3] for key [net.sf.hibernate.impl.SessionFactoryImpl@697b67] from thread [main]
13:35:08,518 DEBUG [HibernateTransactionManager] Closing Hibernate session [net.sf.hibernate.impl.SessionImpl@402af3] after transaction
13:35:08,518 DEBUG [SessionFactoryUtils] Closing Hibernate session
13:35:08,518 DEBUG [SessionImpl] closing session
13:35:08,518 DEBUG [SessionImpl] disconnecting session
13:35:08,528 DEBUG [SessionImpl] transaction completion


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.