-->
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.  [ 4 posts ] 
Author Message
 Post subject: [Newbie] Mapping a Map with a foreign key from a third table
PostPosted: Tue Apr 19, 2005 10:20 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

I am fairly new to complex mappings in hibernate, and I can't find a good example that does what I am attempting to do. I have a set of 3 tables as follows:

Code:
KYCCHECK (
   ID                     int
);

QUERY_RESULT (
   ID                   int,
   QSCOMPONENT_ID       int not null,
   CHECK_ID             int not null,
   RESULT               LONGVARCHAR
);

QSCOMPONENT (
   ID               int not null,
   NAME         varchar(100) not null,
);


Where QUERY_RESULT.CHECK_ID is the foreign key of KYCCHECK.ID and QUERY_RESULT.QSCOMPONENT_ID is the foreign key of QSCOMPONENT.ID.

As you can see from my mapping below, i have a map called resultMap which contains the QSCOMPONENT.ID field as the key and the QUERY_RESULT.RESULT as the value. I'd like to have the foreign key be used in the map so that the key value is actually the string value of QSCOMPONENT.NAME instead of the ID.

Is this possible?


Thanks,
Jason

Hibernate version:
3.0.1

Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
          PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
   

<hibernate-mapping>
 
  <class name="com.semagix.apps.ciras.model.KYCCheck" table="KYCCheck">
   
    <id name="id" column="ID">
      <generator class="native"/>
    </id>
       
    <map name="resultMap" table="QUERY_RESULT" lazy="false" order-by="QSCOMPONENT_ID asc">
      <key column="CHECK_ID"/>
      <map-key type="string" column="QSCOMPONENT_ID"/>
      <element type="string" column="RESULT"/>
    </map>
       
  </class>
 
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
session.get(KYCCheck.class, new Long(964));


Name and version of the database you are using:
HSQLDB 1.7.2

The generated SQL (show_sql=true):
Code:
Hibernate: select kyccheck0_.ID as ID0_, kyccheck0_.TIMESTAMP as TIMESTAMP12_0_, kyccheck0_.LAST_ID as LAST3_12_0_, kyccheck0_.REVIEW_COMPLETED as REVIEW4_12_0_ from KYCCheck kyccheck0_ where kyccheck0_.ID=?
Hibernate: select input0_.CHECK_ID as CHECK1___, input0_.VALUE as VALUE__, input0_.PARAM as PARAM__ from KYCINPUT input0_ where input0_.CHECK_ID=?
Hibernate: select resultmap0_.CHECK_ID as CHECK4___, resultmap0_.RESULT as RESULT__, resultmap0_.QSCOMPONENT_ID as QSCOMPON3___ from QUERY_RESULT resultmap0_ where resultmap0_.CHECK_ID=? order by resultmap0_.QSCOMPONENT_ID asc
Hibernate: select results0_.CHECK_ID as CHECK4___, results0_.ID as ID__, results0_.ID as ID0_, results0_.result as result13_0_, results0_.QSCOMPONENT_ID as QSCOMPON3_13_0_ from QUERY_RESULT results0_ where results0_.CHECK_ID=?
Hibernate: select history0_.CHECK_ID as CHECK6___, history0_.ID as ID__, history0_.ID as ID0_, history0_.ACTION as ACTION15_0_, history0_.COMMENTS as COMMENTS15_0_, history0_.STATUS as STATUS15_0_, history0_.TIMESTAMP as TIMESTAMP15_0_ from KYCCHECKINFO history0_ where history0_.CHECK_ID=? order by history0_.id


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 10:54 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
I kinda cheated before and posted pruned table defs and mappings. Here's the sql output if my code matches what i pasted:

Code:
Hibernate: select kyccheck0_.ID as ID0_ from KYCCheck kyccheck0_ where kyccheck0_.ID=?
Hibernate: select resultmap0_.CHECK_ID as CHECK4___, resultmap0_.RESULT as RESULT__, resultmap0_.QSCOMPONENT_ID as QSCOMPON3___ from QUERY_RESULT resultmap0_ where resultmap0_.CHECK_ID=? order by resultmap0_.QSCOMPONENT_ID asc


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 19, 2005 12:11 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
I've gotten closer by Mapping the QSCOMPONENT table as a persisted class (i'll need it for another similar map). Right now the object is the key of the map.

Is there anyway to get the string property "name" as the key instead using something similar to what i have below?

New Mapping
Code:
    <map name="resultMap" table="QUERY_RESULT" order-by="QSCOMPONENT_ID asc" lazy="false">
      <key column="CHECK_ID"/>
      <map-key-many-to-many column="QSCOMPONENT_ID" class="com.semagix.apps.ciras.model.QsComponent"/>
      <element type="string" column="RESULT"/>
    </map>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 20, 2005 5:30 pm 
Newbie

Joined: Wed Apr 20, 2005 5:05 pm
Posts: 12
I think this is a bit of a hack, but I am having the same problem and for the lack of anything better, I did this.

I would create a class for query_result something like:

Code:
<class name="com.semagix.apps.ciras.model.QueryResults" table="QUERY_RESULT">
   
    <id name="id" column="ID">
      <generator class="native"/>
    </id>
   
    <property name="result" column="name" type="java.lang.String" not-null="true" />

    <join table="QSComponent" fetch="join" inverse="false" optional="false">         
      <key>
          <column name="ID"/>
      </key>                        
      <property name="name" column="name" type="java.lang.String" not-null="true" />
    </join>   
       
</class>


and then...this is the hack...for your mapping

Code:
<map name="resultMap" table="QUERY_RESULT" order-by="QSCOMPONENT_ID asc" lazy="false">
      <key column="CHECK_ID"/>
      <map-key formula="resultmap0_1_.name" type="java.lang.String"/>
      <element type="java.lang.String" column="RESULT"/>
</map>


Notice the formula, its referencing the table you joined on in the class above. It works for me. You might need to doublecheck the column prefix from your sql output. Just cust and paste the generated table header and replace resultmap0_1_ with what your output is, i bet this is right though

This cant be the right way to do this. It works for me presently but I have it on a list of things to revisit.

Also, if you figure out the right way to do this, please post it,

Hope this helps.
LT


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