-->
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: How to best design association table that stores infos?
PostPosted: Fri Feb 27, 2004 6:44 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
how to best design the mapping for the following problem:

I've got 2 tables:

TRAINING_SESSION
---------------------------
ID1
INFO

and

STATE
---------------------------
ID2
NAME


I need to store the amount of people getting a training per State.
I've got an association table (say EFFECTIVES) that's reference both table and that store the amount of people participating to the training per state:

EFFECTIVES
---------------------------
ID1
ID2
QUANTITY

I've already successfully mapped such index tables thnks to <set> attributes. It works great and seemlessly.
But since i've an info here on the index (QUANTITY) I'm wondering how to best implement that.
I think the best for me would be to have a Collection of (STATE_NAME, QUANTITY) object.
Is there anyway to define an object structure composed of various field from various table and associtae it to a mapping?
<bag> or <set> could probably help me here but I'm not familiar with those.

Any help would be welcome!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 7:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The easiest thing would be to use <composite-element> inside of the collection mapping. Take a look here http://www.hibernate.org/hib_docs/reference/html/collections.html#collections-s1-5


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 12:44 pm 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
OK, I found out some something that seems to work but it is quite far fetched compare to a simple set (just to add a property field)

I still got some weird behavior with it:

I added this to my TRAINING mapping:
Code:
<list name="stateEffectif" lazy="false" table="STATE_EFFECTIVE_INDEX">
                <key column="TRAINING_ID_FK"/>
                <index column="STATE_CODE_FK"/>
                <composite-element class="StateEffective">
                    <property name="quantity" column="QUANTITY" type="java.lang.Integer" not-null="true"/>
                </composite-element>
            </list>


Then I had to create a mapping for the STATE_EFFECTIVE_INDEX table (I wish I could avoid it)
Code:
    <class name="" table="STATE_EFFECTIVE_INDEX">
        <composite-id>
            <key-property name="trainingIdFk" column="TRAINING_ID_FK" type="java.lang.Long"/>
            <key-property name="stateCodeFk" column="STATE_CODE_FK" type="java.lang.String"/>
        </composite-id>
        <property name="quantity" column="QUANTITY" type="java.lang.Integer" not-null="true"/>
    </class>


It seems to work (I only been testing loading though), but when I get my stateEffcetive list the quantity field is filled up but not the 2 other keys (composite-id).

Is there any simpler way to achieve this or is it the right way?

thanks lot for answering my newbie questions....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 1:03 pm 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
I tried with <bag> instead of list, removing the index, but still get null values for the identifiers. I would ideally like to get the STATE name and not even the stateCode...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 1:34 pm 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
I did an attempt to add a

<many-to-one name="stateCode" column="STATE_EFFECTIVE_CODE_FK" class="State" not-null=true/>

in order to actually get a State object.
As expected I received the following error: "Repeated column in mapping..."
so had to add insert="false" update="false" and am still unable to get anything else than the QUANTITY...

Help!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Use something like this:

Code:
<class name="State">
     <set name="trainings">
              <key column="state_id">
              <composite-element class="StateTrainings">
                       <property name="participants" column="quantity"/>
                       <many-to-one name="item" class="Training" column="training_id"/>
              </composite-element>
      </set>
</class>

Or map the thing as a map, with the related class as an index, and the quantity as value:

Code:
<class name="State">
      <map name="trainingsToNumbers">
               <key column="state_id"/>
               <index-many-to-many class="Training" column="training_id"/>
               <element column="quantity" type="java.lang.Integer"/>
      </map>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 5:00 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
thanks a lot for responding, I was really looking forward some help here.

So I tried the first solution, but the other way around since I need the State effective in the Training class.

It looks like this:

Code:
<class name="training">
          <set name="stateTraining" lazy="false" table="STATE_TRAINING">
                <key column="TRAINING_ID_FK"/>
                <composite-element class="StateTraining">
                    <property name="particpant" column="QUANTITY" type="java.lang.Short" not-null="true"/>
                    <many-to-one name="stateCodeFk" class="State" column="STATE_CODE_FK"/>
                </composite-element>
            </set>
</class>

<class name="StateTraining" table="STATE_TRAINING">
        <composite-id>
            <key-property name="trainingIdFk" column="TRAINING_ID_FK" type="java.lang.Long"/>
            <key-property name="stateCodeFk" column="STATE_CODE_FK" type="java.lang.String"/>
        </composite-id>
        <property name="participant" column="QUANTITY" type="java.lang.Integer" not-null="true"/>
    </class>


But I got a net.sf.hibernate.PropertyAccessException when trying to load a Training object:
Code:
net.sf.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of StateTraining.setStateCodeFk


It's probably because of the <composite-id>. Is it possible to get rid of it since I don't want to make explicit use of this mapping?

I'm gonna try to work on the second option too.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 5:02 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You do not need to map StateTraining if you use it as composite-element.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 5:34 am 
Regular
Regular

Joined: Fri Nov 07, 2003 6:31 am
Posts: 104
Location: Beijing, China
Ok, I 'm really glad it finally works. I had to regenerate the class from the mapping of my Training class.

What are the pros & cons against the second option?

Is it possible to get the STATE_NAME as a property into the <composit-element> so that the developper doesn't even have to know about the State object, but just get the info it needs, i-e collection of participantQuantity, StateName pairs ???


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.