Hi,
We try to import/export XML documents with parent/child relationship using Hibernate 3.0 new XML mapping feature.
The sample XML document is based on XML Mapping chapter in Hibernate Reference.
We can't find a way to link child back to parent using <many-to-child>. If we set not-null to false, the foreign key will be empty. If set not-null to true, the exception:
not-null property references a null or transient value:
will be thrown.
Even we added parent id to child element and accessed it in <many-to-child>, it didn't work.
Another issue is how we can access parent id in <many-to-child> if we don't add parent id in the child's element? We can't use XPath in node property.
Any help will be appreciated. Thanks.
Hibernate version:
3.0.5
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="customer"
table="CUSTOMER"
node="customer">
<id name="id"
column="CUSTOMER_ID"
node="@id"
type="string"/>
<set name="accounts"
node="."
cascade="all,delete-orphan"
inverse="true"
embed-xml="true">
<key column="CUSTOMER_ID"
not-null="true"/>
<one-to-many entity-name="account"
embed-xml="true"
node="account"/>
</set>
<component name="name"
node="name">
<property name="firstName"
node="first-name"
column="FIRST_NAME"
type="string"/>
<property name="lastName"
node="last-name"
column="LAST_NAME"
type="string"/>
</component>
</class>
<class entity-name="account"
table="ACCOUNTS"
node="account">
<id name="id"
column="ACCOUNT_ID"
node="@id"
type="string"/>
<many-to-one name="customerId"
column="CUSTOMER_ID"
embed-xml="false"
update="false" insert="true"
not-null="true"
entity-name="customer"/>
<property name="short-desc"
column="SHORT_DESC"
node="@short-desc"
type="string"/>
<property name="balance"
column="BALANCE"
node="balance"
type="big_decimal"/>
</class>
</hibernate-mapping>
XML document to be importedCode:
<?xml version="1.0" encoding="UTF-8"?>
<customer id="123456790">
<account id="987632567" short-desc="Savings">
<balance>100.29</balance>
</account>
<account id="985612323" short-desc="Credit Card">
<balance>-2370.34</balance>
</account>
<name>
<first-name>Gavin</first-name>
<last-name>King</last-name>
</name>
</customer>
Code between sessionFactory.openSession() and session.close():Code:
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Transaction tx = session.beginTransaction();
dom4jSession.save(doc.getRootElement());
tx.commit();
Full stack trace of any exception that occurs:
[java] Hibernate: select account_.ACCOUNT_ID, account_.SHORT_DESC as SHORT3
_1_, account_.BALANCE as BALANCE1_ from ACCOUNTS account_ where account_.ACCOUNT
_ID=?
[java] Exception: org.hibernate.PropertyValueException: not-null property r
eferences a null or transient value: account.customerId
[java] org.hibernate.PropertyValueException: not-null property references a
null or transient value: account.customerId
[java] at org.hibernate.engine.Nullability.checkNullability(Nullability
.java:72)
[java] at org.hibernate.event.def.AbstractSaveEventListener.performSave
OrReplicate(AbstractSaveEventListener.java:236)
[java] at org.hibernate.event.def.AbstractSaveEventListener.performSave
(AbstractSaveEventListener.java:160)
[java] at org.hibernate.event.def.AbstractSaveEventListener.saveWithGen
eratedId(AbstractSaveEventListener.java:108)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.save
WithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.enti
tyIsTransient(DefaultSaveOrUpdateEventListener.java:173)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.perf
ormSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96)
[java] at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSa
veOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
[java] at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:
468)
Name and version of the database you are using:
Oracle 8.1.7
The generated SQL (show_sql=true):
create table ACCOUNTS (ACCOUNT_ID varchar2(255) not null, CUSTOMER_ID varchar2(2
55) not null, SHORT_DESC varchar2(255), BALANCE number(19,2), primary key (ACCOU
NT_ID))
create table CUSTOMER (CUSTOMER_ID varchar2(255) not null, FIRST_NAME varchar2(2
55), LAST_NAME varchar2(255), primary key (CUSTOMER_ID))
alter table ACCOUNTS add constraint FKAF43ABE6BA81FCFA foreign key (CUSTOMER_ID)
references CUSTOMER