-->
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: Mapping one-to-many relation
PostPosted: Mon Jun 15, 2009 6:12 am 
Newbie

Joined: Sun Apr 05, 2009 6:56 am
Posts: 5
Hi,
The table TPS16_CONFIGURATION has one-to-may relation TPS25_CURRENCIES_DIFFMIN.
The table TPS25_CURRENCIES_DIFFMIN has this primary key:
TPS16_ID, TPS25_ISO_CURRENCY
where TPS16_ID is the field TPS16_NAME of the table TPS16_CONFIGURATION in particular this field is the key of TPS16_CONFIGURATION.
TPS25_ISO_CURRENCY is another field of TPS25_CURRENCIES_DIFFMIN. The pair TPS16_ID, TPS25_ISO_CURRENCY is the key of the table TPS25_CURRENCIES_DIFFMIN.
Now there are my problems how can I map the key in the xml file that maps the TPS25_CURRENCIES_DIFFMIN table?
In the file that maps the table TPS16_CONFIGURATION I inserted this code:
Code:
<list name="currenciesDiffMin" lazy="false">
           <key column=""/>
           <one-to-many class="ECurrenciesDiffmin"/>
        </list>


In key column what do I have to insert?
Actually the file that maps the table TPS25_CURRENCIES_DIFFMIN is empty.
Please could someone help me?
Thanks, bye bye.


Top
 Profile  
 
 Post subject: Re: Mapping one-to-many relation
PostPosted: Mon Jun 15, 2009 7:49 am 
Newbie

Joined: Sun Apr 05, 2009 6:56 am
Posts: 5
Hi,
I created this mapping file for the table TPS25_CURRENCIES_DIFFMIN:

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.intesasanpaolo.tps.model.ECurrenciesDiffmin"
      table="TPS25_CURRENCIES_DIFFMIN" catalog="@CATALOG@" mutable="true">
      <comment>Oggetto hibernate che mappa la tabella TPS25_CURRENCIES_DIFFMIN</comment>
      
      <composite-id>
         <key-property name="confId" type="string">
            <column name="TPS16_ID"></column> <!-- This is the foreign key. It refers to TPS16_CONFIGURATION.TPS16_NAME field -->
          </key-property>
          <key-property name="currency" type="string">
             <column name="TPS25_ISO_CURRENCY"></column>
          </key-property>
      </composite-id>
      
      <property name="diffMin" type="double">
         <column name="TPS25_DIFF_MIN" not-null="false" />
      </property>
      
</class>      
</hibernate-mapping>


The java class code is:
Code:
package com.intesasanpaolo.tps.model;

public class ECurrenciesDiffmin {
   
   private String confId;
   private String currency;
   private double diffMin;
   
   public ECurrenciesDiffmin() {
      super();
   }
   
   public ECurrenciesDiffmin(String confId, String currency, double diffMin) {
      super();
      this.confId = confId;
      this.currency = currency;
      this.diffMin = diffMin;
   }
   public String getConfId() {
      return confId;
   }
   public void setConfId(String confId) {
      this.confId = confId;
   }
   public String getCurrency() {
      return currency;
   }
   public void setCurrency(String currency) {
      this.currency = currency;
   }
   public double getDiffMin() {
      return diffMin;
   }
   public void setDiffMin(double diffMin) {
      this.diffMin = diffMin;
   }

}



Now I have to insert the list of the previous object in the class that maps TPS16_CONFIGURATION.
Actually I have written this:

Code:
<list name="currenciesDiffMin" lazy="false">
           <key column="confId"/>
           <one-to-many class="ECurrenciesDiffmin"/>
        </list>


This code is wrogn how can I solve?
The Object that maps TPS16_CONFIGURATION can have one or more ECurrenciesDiffmin objects. How can I map this relation?
Thanks, bye bye.


Top
 Profile  
 
 Post subject: Re: Mapping one-to-many relation
PostPosted: Wed Jun 17, 2009 8:14 am 
Newbie

Joined: Fri May 29, 2009 2:21 am
Posts: 12
It seems u have below configuration:

DB side:

Since u need to represent 2 tables in which 1 table (TPS25_CURRENCIES_DIFFMIN) exhibits many-to-1 relation with TPS16_CONFIGURATION, we will have PK of TPS16_CONFIGURATION as FK of TPS25... table

Code:
Table: TPS16_CONFIGURATION

Column:
TPS16_CONF_ID (PK)
TPS16_CONF_NAME
..........

Table: TPS25_CURRENCIES_DIFFMIN

Column:
TPS16_CONF_ID
TPS25_ISO_CURRENCY

P.K - Both Columns


POJO Side

Code:
public class TPS16 {

private List Class_For_TPS25; (Represents 1-to-many )
.............
}

public class Class_For_TPS25 {

private TPS16 tps; (Represents many-to-1)
private String TPS25_ISO_CURRENCY;
.........
}


Hibernate Mapping

Now, suppose u instantiated TPS16 object and while only persisting this TPS16 instance, Hibernate would persist it in table TPS16_CONFIGURATION.

When u instantiate Class_For_TPS25 and try persisting it, since this object conatins a reference to object of type TPS16, Hibernate would make an entry in TPS16_CONFIGURATION and then populate TPS25_CURRENCIES_DIFFMIN (bcoz this table needs TPS16_ID as F.K).


HB Mapping for TPS16
Code:

<class name="TPS16" table="TPS16_CONFIGURATION ">

<id name="TPS16_CONF_ID" type="long" column="TPS16_CONF_ID">
  <generator class="native"></generator> (Populate as per ur need)
</id>

<list name="Class_For_TPS25" inverse="false"  cascade="none">
  <key column="TPS16_CONF_ID"  />         
  <list-index column="COL_USED_FOR_SORTING_WHEN_POPULATING_LIST_OBJECT" />                     
  <one-to-many class="Class_For_TPS25" />           
</list>

else u can also use below (list vs bag tag works for java.util.List types)

<bag name="Class_For_TPS25" inverse="false"  cascade="none">
   <key column="TPS16_CONF_ID"  />                         
   <one-to-many class="Application" />           
</bag>



HB Mapping for Class_For_TPS25

Code:
<class name="Class_For_TPS25" table="TPS25_CURRENCIES_DIFFMIN">

<composite-id>
        <key-property name="TPS25_ISO_CURRENCY"/>
        <key-many-to-one name="TPS16_CONF_ID"/>
</composite-id>
.............
.............


cheers,
Nitesh


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.