-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: session.get() -> OK; criteria.list() -> session error
PostPosted: Tue Nov 29, 2005 3:51 pm 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
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,

_________________
Stewart
London, UK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 4:14 pm 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
With some more tests I found that I also get this:
Code:
org.hibernate.HibernateException: Found shared references to a collection
   at org.hibernate.engine.Collections.processReachableCollection(Collections.java:130)

when trying to pull countries back by their value (such as "GB" or "US"):
Code:
List beans = session.createCriteria(Country.class).add(Restrictions.like("value", values[i])).list();

where values[] is an array of values such that each will match exactly one row....

_________________
Stewart
London, UK


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 29, 2005 5:47 pm 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
This conversation thread has answered the above for me:

http://forums.hibernate.org/viewtopic.php?t=944335

I will solve this by providing a static method of some kind.

However, I'm curious as to why shared references to collections are a problem? Is there something inherent in DB relations that make it impossible, or is it a design decision by the Hibernate team to encourage best practice?

I can think of several examples of object model where you might want different entities to reference collections that end up the same. An example might be product SKUs in stock (on the warehouse object) and product SKUs manufactured by ACME (on the manufacturer object) where all that's left in stock are ACME products.

How does hibernate cope with this - or have I missed something in understanding mapped associations?

Thanks,

_________________
Stewart
London, UK


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.