Using hibernate 3.0.
I have a table with example data like so:
Code:
+----+------------+----------+------------------------+-------+
| id | group_name | sequence | label | value |
+----+------------+----------+------------------------+-------+
| 1 | CURRENCY | 1 | Sterling | GBP |
| 2 | CURRENCY | 2 | Euro | EUR |
| 3 | CURRENCY | 3 | Dollar | USD |
| 6 | COUNTRY | 3 | Albania | AL |
| 7 | COUNTRY | 4 | Algeria | DZ |
| 8 | COUNTRY | 5 | American Samoa | AS |
| 9 | COUNTRY | 6 | Andorra | AD |
| 10 | COUNTRY | 7 | Angola | AO |
These are values which will end up in a HTML <select> box via struts <html:optionsCollection> etc
I have groups of options of several different grouping types.
The above two are special cases where I'm also looking to map them to Currency and Country classes as well as use them in dynamically created drop-downs. I was trying to map it so that a Country object contained a List of possible values for countries, of which itself was the currently selected one. (self-referencing) I'm not sure how sane it was to even try this, but it's a tribute to Hibernate that I got this far.
I came up with this mapping:
Code:
<hibernate-mapping default-lazy="false">
<class name="dynamics.PossibleValue" table="possible_values" discriminator-value="not null" lazy="false">
<id column="id" name="id" type="int" unsaved-value="-1">
<generator class="native"/>
</id>
<discriminator column="group_name" type="string"/>
<property column="group_name" length="50" name="groupName" type="string" insert="false" update="false"/>
<property column="sequence" name="sequence" type="int"/>
<property column="label" length="100" name="label" type="string"/>
<property column="value" length="100" name="value" type="string"/>
<subclass name="objects.dynamics.Country" discriminator-value="COUNTRY" lazy="false">
<bag name="possibleValues" inverse="true">
<key column="group_name" property-ref="groupName"/>
<one-to-many class="objects.dynamics.Country"/>
</bag>
</subclass>
<subclass name="objects.dynamics.Currency" discriminator-value="CURRENCY" lazy="false">
<bag name="possibleValues" inverse="true">
<key column="group_name" property-ref="groupName"/>
<one-to-many class="objects.dynamics.Currency"/>
</bag>
</subclass>
</class>
</hibernate-mapping>
In my JUnit tests I've done the following:
1. Inside a for loop of id values, pulled back each Country object without error using:
Code:
Object bean = session.get(Country.class, i);
Each Country object had its possibleValues List correctly populated.
2. Used a criteria search on an object which maps by association a Country object:
Code:
List beans = session.createCriteria(Address.class).list();
and accessed
Code:
((Address) bean).getCountry()
without error.
3. Used a criteria search on the Country class itself:
Code:
List beans = session.createCriteria(Country.class).list();
This last produces good SQL:
Code:
select this_.id as id0_,
this_.group_name as group2_12_0_,
this_.sequence as sequence12_0_,
this_.label as label12_0_,
this_.value as value12_0_
from possible_values this_
where this_.group_name='COUNTRY'
but throws an exception:
Code:
org.hibernate.HibernateException: collection is not associated with any session at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:275)
I realise that my mapping and table is a slight departure from the recommended but is there any reason why a get() would work but a criteria based find would have trouble with a closed session?
Many thanks,