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.  [ 9 posts ] 
Author Message
 Post subject: Loosely coupled many-to-many help
PostPosted: Wed Jan 11, 2006 10:42 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Hibernate version: 3

Name and version of the database you are using: MySQL 4

I am writing an interface to a legacy database that cannot be changed and have a scenario that I'd appreciate ideas on regarding mappings.

This is simplified but there are 2 tables

Code:
+-------------------------+
|    Scientific_Name      |
+-------------------------+
| Field             | Key |
+-------------------+-----+
| record_id         | PRI |
| name              |     |
| name_code         |     |
+-------------------+-----+

+-------------------------+
|    Common_Name          |
+-------------------------+
| Field             | Key |
+-------------------+-----+
| record_id         | PRI |
| name              |     |
| name_code         |     |
+-------------------+-----+


The join between these tables is the name_code, and thus is a many-to-many join without join tables.

I would like to model this as bidirectional - e.g. CommonName has a Set of ScientificNames and ScientificNames has a Set of CommonNames.

Can anyone help with the mapping for this - can you do joins without using the primary keys?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 4:59 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Maybe I can make this simpler - can I have an object ScientificName that has a Set of CommonName inside it where common name is joined on something other than the ScientificName Primary Key?

If so, how please?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 6:41 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
In Hibernate 2.0 I read on the forum I could have used this in the Scientific name so that it didn't use the primary key.

Code:
<set name="commonNames" property-ref="name_code">
   <key column ="name_code"/>
   <one-to-many class="org.gbif.col2005.model.CommonName"/>
</set>


But in 3.0 property-ref is not valid.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:20 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
How about
Code:
<set name="commonNames">
   <key column ="name_code" property-ref="name_code"/>
   <one-to-many class="org.gbif.col2005.model.CommonName"/>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 7:26 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
I was just about to post that after finding it out and spotted your reply - thanks for looking.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:04 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Does anyone know why I get "collection is not associated with any session" with the above method? I'm not sure what collections it is referring to as the test I am running does not have any elements in it's collection...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:07 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
oops - read the rules

[junit] collection is not associated with any session
[junit] org.hibernate.HibernateException: collection is not associated with any session
[junit] at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:449)
[junit] at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:791)
[junit] at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:228)
[junit] at org.hibernate.loader.Loader.doList(Loader.java:2147)
[junit] at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2026)
[junit] at org.hibernate.loader.Loader.list(Loader.java:2021)
[junit] at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
[junit] at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1483)
[junit] at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
[junit] at org.gbif.col2005.dao.impl.hibernate.TaxaDAOImpl.findByRankAndFuzzyName(Unknown Source)
[junit] at org.gbif.col2005.dao.TaxaDAOTest.testGetTaxonByRankAndFuzzyName(Unknown Source)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:20 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Can you post the relevant code and corresponding mappings ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 10:41 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Scientific name is actually Taxa now...

The query

Code:
from Taxa where taxon=? and lower(name) like lower(?) and recordId>=? order by recordId


The Taxa mapping

Code:
<hibernate-mapping>
   <class name="org.gbif.col2005.model.Taxa" table="taxa">
   
      <id name="recordId" column="record_id" type="long">
         <generator class="native">
         </generator>
      </id>
      
      <property name="taxon" type="java.lang.String" update="true" insert="true"
         column="taxon"/>
      
      <property name="name" type="java.lang.String" update="true" insert="true"
         column="name"/>
      
      <property name="nameCode" type="java.lang.String" update="true"
         insert="true" column="name_code"/>
      
      <set name="commonNames" lazy="false">
         <key column="name_code" property-ref="nameCode"/>
         <one-to-many class="org.gbif.col2005.model.CommonName"/>
      </set>      
   </class>   
</hibernate-mapping>


The common name mapping (The composite key was just because I was playing with another idea)

Code:
<hibernate-mapping>
   <class name="org.gbif.col2005.model.CommonName" table="common_names">
      <composite-id name="key" class="org.gbif.col2005.model.CompositeKey">
         <key-property name="recordId" type="long" column="record_id"/>
         <key-property name="nameCode" type="java.lang.String"
            column="name_code"/>
      </composite-id>
      <property name="commonName" type="java.lang.String" update="true"
         insert="true" column="common_name"/>
      <property name="country" type="java.lang.String" update="true" insert="true"
         column="country"/>
      <property name="language" type="java.lang.String" update="true"
         insert="true" column="language"/>
   </class>
</hibernate-mapping>


This all works for when there is a name_code in the Taxa, but when that is an empty String (legacy DB that can't be changed), there are no common names for this scenario, but that's when I get the exception trace above.

There should be no results to be associated without a session anyways.

Thanks for looking


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.