Hibernate version: 3.0.5 & 3.1
OK, I've been using Hibernate for almost a year now with no problem ... but now I got one I cannot move with. So I am usign I advanced Hibernate feature the ability to work with a Session over DOM4J.
Everything works fine until there is a <component> with an embeded <many-to-one> an I set the embed-xml="false". So let's suppose I have to mapped classes like these:
Region class mapping:
Code:
<hibernate-mapping default-access="field">
<class name="Region" table="REGION" node="region" lazy="false">
<id name="id" type="long" column="ID" node="@id">
<generator class="native"/>
</id>
<property name="code" column="CODE" node="code" type="string" unique="true" length="30" not-null="true"/>
<property name="shortcut" column="SHORTCUT" node="shortcut" type="string" unique="true" length="30" not-null="true"/>
<map name="text" table="REGION_TEXT" node="." embed-xml="true" lazy="false">
<key column="REGION_ID"/>
<map-key column="LOCALE" node="@locale" type="string" length="5"/>
<element column="TEXT" node="text" type="string" length="255" unique="false" not-null="true"/>
</map>
</class>
</hibernate-mapping>
Building class mapping:Code:
<class name="Building" table="BUILDING" node="building" lazy="false">
<id name="id" type="long" column="ID" node="@id">
<generator class="native"/>
</id>
<property name="code" column="CODE" node="code" type="string" unique="true" length="30" not-null="true"/>
<!-- Adress -->
<component name="address" class="Address" node="address">
<component name="street" class="Street" node="street">
<property name="name" column="ADR_STREET_NAME" node="name" type="string" length="50" not-null="true"/>
<property name="number" column="ADR_STREET_NUMBER" node="number" type="string" length="20" not-null="true"/>
</component>
<property name="zip" column="ADR_ZIP" node="zip" type="string" length="10" not-null="true"/>
<!-- THIS HERE IS IMPORTANT -->
<many-to-one name="region" class="Region"
column="ADR_REGION_ID"
node="region/@id" embed-xml="false"
not-null="true" cascade="none"
/>
</component>
</class>
So there is a <many-to-one> tag inside an <component> one, the classes are not important I guess ... it happened in more than just one place ... this is a simplified version of my two classes I am pretty sure I have included the important stuff.
So what sounds strange to me is the fact that if a
embed-xml="true" everything works fine I've got an XML and the Region is embeded correctly, but if i turn the
embed-xml="false" on the <many-to-one> inside a <component> (otherwise it works greatly !) I got the following:
Exception when embed-xml="false":Code:
Exception in thread "main" java.lang.NullPointerException
at org.hibernate.type.EntityType.getIdentifierType(EntityType.java:226)
at org.hibernate.type.EntityType.setToXMLNode(EntityType.java:449)
at org.hibernate.property.Dom4jAccessor$ElementAttributeSetter.set(Dom4jAccessor.java:352)
at org.hibernate.tuple.AbstractComponentTuplizer.setPropertyValues(AbstractComponentTuplizer.java:159)
at org.hibernate.type.ComponentType.setPropertyValues(ComponentType.java:312)
at org.hibernate.type.ComponentType.resolve(ComponentType.java:530)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:113)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2150)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:369)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:300)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:146)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1093)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at sk.mais.persistence.data.HibernateExport.doExport(HibernateExport.java:105)
at sk.mais.persistence.data.HibernateExport.exportData(HibernateExport.java:70)
at sk.mais.persistence.data.HibernateExport.main(HibernateExport.java:226)
To be COMPLETE here is what I am doing (not sure if matters):
Code:
private static void doExport(Session session, XmlDataConfig data) {
Session xmlSession = session.getSession(EntityMode.DOM4J);
List results =
xmlSession
.createQuery("FROM " + data.getEntityName())
.list();
Document doc = createDocument(data);
Element root = doc.getRootElement();
Transaction tx = session.beginTransaction();
try {
for (int i=0; i<results.size(); i++) {
// add the data to XML root:
Element nextElement =
(Element) results.get(i);
root.add(nextElement);
}
tx.commit();
}
catch (Exception e) {
...
}
data.storeData(doc);
session.flush();
}
Once again I have to NOTE:
everything works fine until embed-xml is true i got the data from the DB in the XML as expected but canot get it working with embed-xml set to false !
ANY IDEAS ???