Hi
I am importing an XML document, and I am having two problems.
1. Hibernate is ignoring embedded id's and generating new ones
2. Hibernate is not reading the second Object in a many-one relationship
We need to embed the id (foreign key) in order to create relationships between objects where we do not cascade on save.
Thank-you for your help
Chris Love
Hibernate version: 3.05
XML File Used
Code:
<?xml version="1.0" encoding="UTF-8"?>
<CDOM>
<ContactInfo>
<title>Card Specialist</title>
<name>Raj</name>
<phone>999-999-9999</phone>
<fax>999-999-9999</fax>
<email>wow@wow.com</email>
<Address>
<ADDRESSID>7000000L</ADDRESSID>
<recordversion>0</recordversion>
<streetAddressL1>999 Utah Ave South</streetAddressL1>
<streetAddressL2/>
<city>Seattle</city>
<state>WA</state>
<zipcode>98124</zipcode>
<country>USA</country>
</Address>
<Address>
<ADDRESSID>7000001L</ADDRESSID>
<recordversion>0</recordversion>
<streetAddressL1>45 Utah Ave South</streetAddressL1>
<streetAddressL2/>
<city>Seattle</city>
<state>WA</state>
<zipcode>98124</zipcode>
<country>USA</country>
</Address>
</ContactInfo>
</CDOM>
Mapping documents:ContactInfo
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.valuelink.nextrends.cdom.common.models.ContactInfo"
table="CDOM_CONTACTINFO"
node="ContactInfo"
>
<id
name="id"
column="CONTACTINFOID"
type="java.lang.Long"
node="ContactInfo/@id"
>
<generator class="native">
<param name="sequence">seq_contactinfo</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-ContactInfo.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<version
name="recordVersion"
node="recordVersion"
column="recordVersion"
type="long"
/>
<property
name="title"
node="title"
type="java.lang.String"
update="true"
insert="true"
column="TITLE"
length="30"
/>
<property
name="name"
node="name"
type="java.lang.String"
update="true"
insert="true"
column="NAME"
length="100"
/>
<property
name="phone"
node="phone"
type="java.lang.String"
update="true"
insert="true"
column="PHONE"
length="30"
/>
<property
name="fax"
node="fax"
type="java.lang.String"
update="true"
insert="true"
column="FAX"
length="30"
/>
<property
name="email"
node="email"
type="java.lang.String"
update="true"
insert="true"
column="EMAIL"
length="100"
/>
<property
name="sbmUserName"
node="sbmUserName"
type="java.lang.String"
update="true"
insert="true"
column="SBMUSERNAME"
length="100"
/>
<many-to-one
name="address"
class="com.valuelink.nextrends.cdom.common.models.Address"
cascade="save-update"
outer-join="true"
update="true"
insert="true"
column="ADDRESSID"
node="Address" embed-xml="true"
/>
<many-to-one
name="affiliationType"
node="AffiliationType/@id" embed-xml="true"
class="com.valuelink.nextrends.cdom.common.types.AffiliationType"
cascade="none"
outer-join="true"
update="true"
insert="true"
column="AFFILIATIONTYPEID"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-ContactInfo.xml
containing the additional properties and place it in your merge dir.
-->
</class>
Address
Code:
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="com.valuelink.nextrends.cdom.common.models.Address"
table="CDOM_ADDRESS"
node="Customer"
>
<id
name="id"
column="ADDRESSID"
type="java.lang.Long"
node="id"
>
<generator class="native">
<param name="sequence">seq_address</param>
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Address.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<version
name="recordVersion"
column="recordVersion"
type="long"
node="recordVersion"
/>
<property
name="streetAddressL1"
node="streetAddressL1"
type="java.lang.String"
update="true"
insert="true"
column="STREETADDRESSL1"
length="30"
/>
<property
name="streetAddressL2"
node="streetAddressL2"
type="java.lang.String"
update="true"
insert="true"
column="STREETADDRESSL2"
length="30"
/>
<property
name="city"
node="city"
type="java.lang.String"
update="true"
insert="true"
column="CITY"
length="30"
/>
<property
name="state"
node="state"
type="java.lang.String"
update="true"
insert="true"
column="STATE"
length="30"
/>
<property
name="zipCode"
node="zipCode"
type="java.lang.String"
update="true"
insert="true"
column="ZIPCODE"
length="30"
/>
<property
name="country"
node="country"
type="java.lang.String"
update="true"
insert="true"
column="COUNTRY"
length="30"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Address.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
reader = new SAXReader();
File configFile = new File("build/classes/hibernate.cfg.xml");
try {
document = reader.read(uri);
root = document.getRootElement();
Configuration config = new Configuration();
config.configure(configFile);
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
dom4jSession = session.getSession(EntityMode.DOM4J);
tx = session.beginTransaction();
// iterate through child elements of root
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
System.out.println(element.getName());
dom4jSession.persist(
"com.valuelink.nextrends.cdom.common.models."
+ element.getName(), element);
}
} catch (DocumentException e) {
e.printStackTrace(); // FIXME more elegant
tx.rollback();
} finally {
tx.commit();
session.close();
}
Full stack trace of any exception that occurs:No exception occurs
Name and version of the database you are using: Oracle 10The generated SQL (show_sql=true):Code:
Hibernate: select seq_address.nextval from dual
Hibernate: select seq_address.nextval from dual
Hibernate: insert into CDOM_CONTACTINFO (recordVersion, TITLE, NAME, PHONE, FAX, EMAIL, SBMUSERNAME, ADDRESSID, AFFILIATIONTYPEID, CONTACTINFOID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into CDOM_ADDRESS (recordVersion, STREETADDRESSL1, STREETADDRESSL2, CITY, STATE, ZIPCODE, COUNTRY, ADDRESSID) values (?, ?, ?, ?, ?, ?, ?, ?)
Debug level Hibernate log excerpt:Code:
32500 [Main Thread] DEBUG transaction.JDBCTransaction - begin
32500 [Main Thread] DEBUG jdbc.ConnectionManager - opening JDBC connection
32500 [Main Thread] DEBUG connection.DriverManagerConnectionProvider - total checked-out connections: 0
32500 [Main Thread] DEBUG connection.DriverManagerConnectionProvider - using pooled JDBC connection, pool size: 0
32500 [Main Thread] DEBUG transaction.JDBCTransaction - current autocommit status: false
32594 [Main Thread] DEBUG def.AbstractSaveEventListener - transient instance of: com.valuelink.nextrends.cdom.common.models.ContactInfo
32594 [Main Thread] DEBUG def.DefaultPersistEventListener - saving transient instance
32609 [Main Thread] DEBUG jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
32625 [Main Thread] DEBUG hibernate.SQL - select seq_contactinfo.nextval from dual
32625 [Main Thread] DEBUG jdbc.AbstractBatcher - preparing statement
33078 [Main Thread] DEBUG id.SequenceGenerator - Sequence identifier generated: 26
33078 [Main Thread] DEBUG jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
33078 [Main Thread] DEBUG jdbc.AbstractBatcher - closing statement
33094 [Main Thread] DEBUG def.AbstractSaveEventListener - generated identifier: 26, using strategy: org.hibernate.id.SequenceGenerator
33094 [Main Thread] DEBUG def.AbstractSaveEventListener - saving [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33109 [Main Thread] DEBUG engine.Cascades - processing cascade ACTION_PERSIST for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33125 [Main Thread] DEBUG engine.Cascades - done processing cascade ACTION_PERSIST for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33125 [Main Thread] DEBUG engine.Versioning - Seeding: 0
33156 [Main Thread] DEBUG engine.Cascades - processing cascade ACTION_PERSIST for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33156 [Main Thread] DEBUG engine.Cascades - done processing cascade ACTION_PERSIST for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33156 [Main Thread] DEBUG transaction.JDBCTransaction - commit
33156 [Main Thread] DEBUG impl.SessionImpl - automatically flushing session
33156 [Main Thread] DEBUG def.AbstractFlushingEventListener - flushing session
33156 [Main Thread] DEBUG def.AbstractFlushingEventListener - processing flush-time cascades
33156 [Main Thread] DEBUG engine.Cascades - processing cascade ACTION_SAVE_UPDATE for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33172 [Main Thread] DEBUG engine.Cascades - cascading to saveOrUpdate: com.valuelink.nextrends.cdom.common.models.Address
33172 [Main Thread] DEBUG def.AbstractSaveEventListener - transient instance of: com.valuelink.nextrends.cdom.common.models.Address
33172 [Main Thread] DEBUG def.DefaultSaveOrUpdateEventListener - saving transient instance
33172 [Main Thread] DEBUG jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
33172 [Main Thread] DEBUG hibernate.SQL - select seq_address.nextval from dual
33172 [Main Thread] DEBUG jdbc.AbstractBatcher - preparing statement
33172 [Main Thread] DEBUG id.SequenceGenerator - Sequence identifier generated: 13
33172 [Main Thread] DEBUG jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
33172 [Main Thread] DEBUG jdbc.AbstractBatcher - closing statement
33172 [Main Thread] DEBUG def.AbstractSaveEventListener - generated identifier: 13, using strategy: org.hibernate.id.SequenceGenerator
33172 [Main Thread] DEBUG def.AbstractSaveEventListener - saving [com.valuelink.nextrends.cdom.common.models.Address#13]
33172 [Main Thread] DEBUG engine.Versioning - Seeding: 0
33172 [Main Thread] DEBUG engine.Cascades - done processing cascade ACTION_SAVE_UPDATE for: com.valuelink.nextrends.cdom.common.models.ContactInfo
33344 [Main Thread] DEBUG def.AbstractFlushingEventListener - dirty checking collections
33344 [Main Thread] DEBUG def.AbstractFlushingEventListener - Flushing entities and processing referenced collections
33375 [Main Thread] DEBUG entity.BasicEntityPersister - com.valuelink.nextrends.cdom.common.models.ContactInfo.address is dirty
33375 [Main Thread] DEBUG def.DefaultFlushEntityEventListener - Updating entity: [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33375 [Main Thread] DEBUG engine.Versioning - Incrementing: 0 to 1
33391 [Main Thread] DEBUG def.AbstractFlushingEventListener - Processing unreferenced collections
33391 [Main Thread] DEBUG def.AbstractFlushingEventListener - Scheduling collection removes/(re)creates/updates
33391 [Main Thread] DEBUG def.AbstractFlushingEventListener - Flushed: 2 insertions, 1 updates, 0 deletions to 2 objects
33391 [Main Thread] DEBUG def.AbstractFlushingEventListener - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
33391 [Main Thread] DEBUG pretty.Printer - listing entities:
33391 [Main Thread] DEBUG pretty.Printer - org.dom4j.tree.DefaultElement
33391 [Main Thread] DEBUG pretty.Printer - org.dom4j.tree.DefaultElement
33391 [Main Thread] DEBUG def.AbstractFlushingEventListener - executing flush
33406 [Main Thread] DEBUG entity.BasicEntityPersister - Inserting entity: [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33406 [Main Thread] DEBUG entity.BasicEntityPersister - Version: 0
33406 [Main Thread] DEBUG jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
33406 [Main Thread] DEBUG hibernate.SQL - insert into CDOM_CONTACTINFO (recordVersion, TITLE, NAME, PHONE, FAX, EMAIL, SBMUSERNAME, ADDRESSID, AFFILIATIONTYPEID, CONTACTINFOID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
33406 [Main Thread] DEBUG jdbc.AbstractBatcher - preparing statement
33406 [Main Thread] DEBUG entity.BasicEntityPersister - Dehydrating entity: [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33406 [Main Thread] DEBUG type.LongType - binding '0' to parameter: 1
33422 [Main Thread] DEBUG type.StringType - binding 'Card Specialist' to parameter: 2
33422 [Main Thread] DEBUG type.StringType - binding 'Raj TheTerrible' to parameter: 3
33422 [Main Thread] DEBUG type.StringType - binding '720-332-5073' to parameter: 4
33422 [Main Thread] DEBUG type.StringType - binding '720-332-0502' to parameter: 5
33422 [Main Thread] DEBUG type.StringType - binding 'wow@wow.com' to parameter: 6
33422 [Main Thread] DEBUG type.StringType - binding null to parameter: 7
33422 [Main Thread] DEBUG type.LongType - binding null to parameter: 8
33422 [Main Thread] DEBUG type.LongType - binding null to parameter: 9
33422 [Main Thread] DEBUG type.LongType - binding '26' to parameter: 10
33438 [Main Thread] DEBUG entity.BasicEntityPersister - Inserting entity: [com.valuelink.nextrends.cdom.common.models.Address#13]
33438 [Main Thread] DEBUG entity.BasicEntityPersister - Version: 0
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - closing statement
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
33438 [Main Thread] DEBUG hibernate.SQL - insert into CDOM_ADDRESS (recordVersion, STREETADDRESSL1, STREETADDRESSL2, CITY, STATE, ZIPCODE, COUNTRY, ADDRESSID) values (?, ?, ?, ?, ?, ?, ?, ?)
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - preparing statement
33438 [Main Thread] DEBUG entity.BasicEntityPersister - Dehydrating entity: [com.valuelink.nextrends.cdom.common.models.Address#13]
33438 [Main Thread] DEBUG type.LongType - binding '0' to parameter: 1
33438 [Main Thread] DEBUG type.StringType - binding '45 Utah Ave South' to parameter: 2
33438 [Main Thread] DEBUG type.StringType - binding null to parameter: 3
33438 [Main Thread] DEBUG type.StringType - binding 'Seattle' to parameter: 4
33438 [Main Thread] DEBUG type.StringType - binding 'WA' to parameter: 5
33438 [Main Thread] DEBUG type.StringType - binding null to parameter: 6
33438 [Main Thread] DEBUG type.StringType - binding 'USA' to parameter: 7
33438 [Main Thread] DEBUG type.LongType - binding '13' to parameter: 8
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
33438 [Main Thread] DEBUG jdbc.AbstractBatcher - closing statement
33469 [Main Thread] DEBUG entity.BasicEntityPersister - Updating entity: [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33469 [Main Thread] DEBUG entity.BasicEntityPersister - Existing version: 0 -> New version: 1
33469 [Main Thread] DEBUG jdbc.AbstractBatcher - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
33469 [Main Thread] DEBUG hibernate.SQL - update CDOM_CONTACTINFO set recordVersion=?, TITLE=?, NAME=?, PHONE=?, FAX=?, EMAIL=?, SBMUSERNAME=?, ADDRESSID=?, AFFILIATIONTYPEID=? where CONTACTINFOID=? and recordVersion=?
33469 [Main Thread] DEBUG jdbc.AbstractBatcher - preparing statement
33469 [Main Thread] DEBUG entity.BasicEntityPersister - Dehydrating entity: [com.valuelink.nextrends.cdom.common.models.ContactInfo#26]
33469 [Main Thread] DEBUG type.LongType - binding '1' to parameter: 1
33469 [Main Thread] DEBUG type.StringType - binding 'Card Specialist' to parameter: 2
33469 [Main Thread] DEBUG type.StringType - binding 'Raj TheTerrible' to parameter: 3
33469 [Main Thread] DEBUG type.StringType - binding '720-332-5073' to parameter: 4
33469 [Main Thread] DEBUG type.StringType - binding '720-332-0502' to parameter: 5
33469 [Main Thread] DEBUG type.StringType - binding 'wow@wow.com' to parameter: 6
33469 [Main Thread] DEBUG type.StringType - binding null to parameter: 7
33469 [Main Thread] DEBUG type.LongType - binding '13' to parameter: 8
33469 [Main Thread] DEBUG type.LongType - binding null to parameter: 9
33469 [Main Thread] DEBUG type.LongType - binding '26' to parameter: 10
33469 [Main Thread] DEBUG type.LongType - binding '0' to parameter: 11
33469 [Main Thread] DEBUG jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
33469 [Main Thread] DEBUG jdbc.AbstractBatcher - closing statement
33469 [Main Thread] DEBUG def.AbstractFlushingEventListener - post flush
33469 [Main Thread] DEBUG jdbc.JDBCContext - before transaction completion
33469 [Main Thread] DEBUG impl.SessionImpl - before transaction completion
33469 [Main Thread] DEBUG transaction.JDBCTransaction - committed JDBC Connection
33469 [Main Thread] DEBUG jdbc.JDBCContext - after transaction completion
33469 [Main Thread] DEBUG impl.SessionImpl - after transaction completion
33484 [Main Thread] DEBUG impl.SessionImpl - closing session
33484 [Main Thread] DEBUG impl.SessionImpl - closing session
33484 [Main Thread] DEBUG jdbc.ConnectionManager - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
33484 [Main Thread] DEBUG connection.DriverManagerConnectionProvider - returning connection to pool, pool size: 1
33484 [Main Thread] DEBUG jdbc.JDBCContext - after transaction completion
33484 [Main Thread] DEBUG impl.SessionImpl - after transaction completion
34109 [Finalizer] DEBUG jdbc.ConnectionManager - running Session.finalize()