Hi,
I've got a mapping problem with an
AttributeValue Pattern. I want do be my classes to be some kind of dynamic extensible. So I've created an Object
AttributeValue which can be added in a collection in each of my business classes.
Each business class contains a map with the related AttributeValues of the object. Now I'm looking for a solution to map this behavior with hibernate. Everything works fine as long as I can set the <index> attribute of my Maps to not-null but when I allow foreign keys with null values my values aren't retrieved from the database (they are stored, I can see them with an manual SQL statement).
I would also be glad if there is some better solution for this problem.
I've used mySQL and HSQLDB but I guess that database doesn't matter.
Thanks a lot
Joerg
Hibernate version: 3.05
Mapping documents:
Code:
<class name="AttributeValueImpl" table="AttributeValue">
<id name="id" access="org.hibernate.property.DirectPropertyAccessor">
<generator class="native"/>
</id>
<property name="date" type="java.util.Date" column="datevalue" />
<property name="double" column="doublevalue" type="double"/>
<property name="integer" column="integervalue" type="integer"/>
<property name="string" column="stringvalue" type="string"/>
</class>
Code:
<class name="AddressImpl" table="Address" >
<id name="id" access="org.hibernate.property.DirectPropertyAccessor">
<generator class="native"/>
</id>
<property name="addressNo" />
<map name="avMap" lazy="false" access="org.hibernate.property.DirectPropertyAccessor" cascade="all-delete-orphan">
<key column="ADDRESS_ID"/>
<index column="addresskey" type="string"/>
<one-to-many class="de.gish.am.data.AttributeValueImpl"/>
</map>
...
</class>
Code:
<class name="AbstractBusinessPartner" table="BusinessPartner">
<id name="id"
access="org.hibernate.property.DirectPropertyAccessor">
<generator class="native" />
</id>
<map name="avMap" lazy="false" access="org.hibernate.property.DirectPropertyAccessor" cascade="all-delete-orphan">
<key column="BUSINESSPARTNER_ID"/>
<index column="businesspartnerkey" type="string" />
<one-to-many class="de.gish.am.data.AttributeValueImpl" />
</map>
...
</class>
Code between sessionFactory.openSession() and session.close():Code:
Company comp = BusinessObjectFactory.createCompany();
AttributeValueImpl av = new AttributeValueImpl();
av.setString("Sachsen-Anhalt");
comp.put("Bundesland",av);
dao.saveObject(comp);
Integer id = comp.getId();
dao.clearSession();
Company comploaded = (Company)dao.loadByID(Company.class, id.intValue());
assertNotNull(comploaded);
>>>> Fails
assertEquals("Sachsen-Anhalt", comploaded.get("Bundesland"));