-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to one (composite)
PostPosted: Mon Nov 06, 2006 8:38 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 6:15 am
Posts: 21
hello,

I have the following case:

Table A
--------------------
id | key | field b | field c




Table B
--------------------
id| key | field d | field e


id and key are a composite key. How can i now write the mapping file? i found many examples on one-to-one on a primary key but none of a composite key...

could someone show an example please?


Top
 Profile  
 
 Post subject: Composite key
PostPosted: Mon Nov 06, 2006 9:11 am 
Newbie

Joined: Thu Nov 02, 2006 6:59 pm
Posts: 8
Location: London
See here

http://www.hibernate.org/hib_docs/v3/reference/en/html/mapping.html#mapping-declaration-compositeid

Grant


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 10:45 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 6:15 am
Posts: 21
i already read this part but mostly its about one table and 2 classes which are connected through a composit key..


what i want to archieve is 2 tables...and 2 pojo's ..

in each of the pojo's i have equals() and hashCode() implemented and also retrieve the other object by accessors (getTableA() get tableB() etc.)..my only problem is this mapping file that doesnt want to work...

Can someone make a short example with 2 tables...2 pojo's and a composite id that connects these two tables/beans?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 06, 2006 11:39 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 6:15 am
Posts: 21
to make it more clear, i just post what i have atm:

Shipment.java
Code:

import java.math.BigDecimal;
import java.util.Date;

public class Shipment implements java.io.Serializable {

   // private ShipmentId id;

   private Shipmentgeo shipmentgeo;

   private String shipref;

   private String traffictype;

   private String shipper;

   private String consignee;

   private String lastupdby;

   // Constructors

   /** default constructor */
   public Shipment() {
   }

   /** full constructor */
   public Shipment(String shipper, String consignee) {

      this.shipper = shipper;
      this.consignee = consignee;

   }

   public String getShipref() {
      return shipref;
   }

   public void setShipref(String shipref) {
      this.shipref = shipref;
   }

   public String getTraffictype() {
      return traffictype;
   }

   public void setTraffictype(String traffictype) {
      this.traffictype = traffictype;
   }

   public String getShipper() {
      return this.shipper;
   }

   public void setShipper(String shipper) {
      this.shipper = shipper;
   }

   public String getConsignee() {
      return this.consignee;
   }

   public void setConsignee(String consignee) {
      this.consignee = consignee;
   }

   public void setLastupdby(String lastupdby) {
      this.lastupdby = lastupdby;
   }

   public Shipmentgeo getShipmentgeo() {
      return shipmentgeo;
   }

   public void setShipmentgeo(Shipmentgeo shipmentgeo) {
      this.shipmentgeo = shipmentgeo;
   }

   public boolean equals(Object that) {
      if (!(that instanceof Shipment))
         return false;
      Shipment ship = (Shipment) that;
      return ship.getShipref().equals(shipref)
            && ship.getShipref().equals(traffictype);
   }

   public int hashCode() {
      return shipref.hashCode() + traffictype.hashCode();
   }

}


Shipment.hbm.xml
Shipmentgeo.hbm.xml
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="Shipment" table="shipment"
      schema="informix">
      <composite-id>
         <key-property name="shipref" type="string">
            <column name="shipref" length="15" />
         </key-property>
         <key-property name="traffictype" type="string">
            <column name="traffictype"/>
         </key-property>
      </composite-id>
      <property name="shipper" type="string">
         <column name="shipper" length="3" />
      </property>
      <property name="consignee" type="string">
         <column name="consignee" length="3" />
      </property>
   </class>

</hibernate-mapping>



Shipmentgeo.java
Code:

import java.util.Date;

public class Shipmentgeo implements java.io.Serializable {

   private String shipref;

   private String traffictype;

   private Shipment shipment;

   private String departure;

   private String arrival;

   // Constructors

   /** default constructor */
   public Shipmentgeo() {
   }

   /** minimal constructor */

   /** full constructor */
   public Shipmentgeo(String departure, String arrival) {

      this.departure = departure;
      this.arrival = arrival;
   }

   public String getShipref() {
      return shipref;
   }

   public void setShipref(String shipref) {
      this.shipref = shipref;
   }

   public String getTraffictype() {
      return traffictype;
   }

   public void setTraffictype(String traffictype) {
      this.traffictype = traffictype;
   }

   public String getDeparture() {
      return this.departure;
   }

   public void setDeparture(String departure) {
      this.departure = departure;
   }

   public String getArrival() {
      return this.arrival;
   }

   public Shipment getShipment() {
      return shipment;
   }

   public void setShipment(Shipment shipment) {
      this.shipment = shipment;
   }

   public boolean equals(Object that) {
      if (!(that instanceof Shipmentgeo))
         return false;
      Shipmentgeo shipgeo = (Shipmentgeo) that;
      return shipgeo.getShipref().equals(shipref)
            && shipgeo.getShipref().equals(traffictype);
   }

   public int hashCode() {
      return shipref.hashCode() + traffictype.hashCode();
   }

}




Shipmentgeo.hbm.xml
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="Shipmentgeo" table="shipmentgeo"
      schema="informix">
      <composite-id>
         <key-property name="shipref" type="string">
            <column name="shipref" length="15" />
         </key-property>
         <key-property name="traffictype" type="string">
            <column name="traffictype" />
         </key-property>
      </composite-id>
        <property name="departure" type="string">
            <column name="departure" length="3" />
        </property>
        <property name="arrival" type="string">
            <column name="arrival" length="3" />
        </property>
     </class>

</hibernate-mapping>




testcode

Code:

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;



import org.hibernate.*;
import org.hibernate.cfg.*;


import java.sql.*;

public class DetailDao {

   Transaction ht = null;

   private static Logger logger = Logger.getLogger(DetailDao.class);

   public DetailDao() {
      HibernateUtil.beginTransaction();
   }

   public void fetchShipmentList(String shipref) {

      Session s = HibernateUtil.openSession();
      s.beginTransaction();
      Shipment ship = new Shipment();
      ship.setShipref(shipref);
      ship.setTraffictype("1");
      
      ship =  (Shipment) s.load(Shipment.class,ship);
      
      
      
      System.out.println("shipment.shipref= "+ship.getShipmentgeo().getArrival());

   }

}


now ship.getShipmentgeo().getArrival() doesnt work as it is not associated with the Shipment class yet...how can i archieve this?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 8:27 am 
Beginner
Beginner

Joined: Fri Nov 03, 2006 6:15 am
Posts: 21
can please someone help me on this? I've spend several days and it's just not working....

i read the documentation over and over but i just cant find the solution to my problem


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 10, 2006 1:14 pm 
Newbie

Joined: Fri Nov 10, 2006 12:36 pm
Posts: 2
Location: Toronto
I have exactly the same problem. Can anybody help us out here, please???

Adptating chuanito's mapping files to the solution I tried, I've come up with the mappings below. But they don't seem to work.

Shipment.hbm.xml
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="Shipment" table="shipment"
      schema="informix">
      <composite-id>
         <key-property name="shipref" type="string">
            <column name="shipref" length="15" />
         </key-property>
         <key-property name="traffictype" type="string">
            <column name="traffictype"/>
         </key-property>
      </composite-id>
<one-to-one name="shipmentgeo" class="Shipmentgeo" cascade="save-update">
   <column name="shipref" />
   <column name="traffictype" />
</one-to-one>
      <property name="shipper" type="string">
         <column name="shipper" length="3" />
      </property>
      <property name="consignee" type="string">
         <column name="consignee" length="3" />
      </property>
   </class>

</hibernate-mapping>


Shipmentgeo.hbm.xml
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="Shipmentgeo" table="shipmentgeo"
      schema="informix">
      <composite-id>
         <key-property name="shipref" type="string">
            <column name="shipref" length="15" />
         </key-property>
         <key-property name="traffictype" type="string">
            <column name="traffictype" />
         </key-property>
      </composite-id>
<one-to-one name="shipment" class="Shipment" constrained="true">
   <column name="shipref" />
   <column name="traffictype" />
</one-to-one>
        <property name="departure" type="string">
            <column name="departure" length="3" />
        </property>
        <property name="arrival" type="string">
            <column name="arrival" length="3" />
        </property>
     </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: Solution to one-to-one relation on two tables having composi
PostPosted: Thu Nov 16, 2006 11:51 pm 
Newbie

Joined: Thu Nov 16, 2006 6:28 am
Posts: 1
import java.math.BigDecimal;
import java.util.Date;

public class Shipment {

private Shipmentgeo shipmentgeo;

private ShipmentPK pk;
// implement getter and setter methods of pk

private String shipper;

private String consignee;

private String lastupdby;

// Constructors

/** default constructor */
public Shipment() {
}

/** full constructor */
public Shipment(String shipper, String consignee) {

this.shipper = shipper;
this.consignee = consignee;

}

public String getShipper() {
return this.shipper;
}

public void setShipper(String shipper) {
this.shipper = shipper;
}

public String getConsignee() {
return this.consignee;
}

public void setConsignee(String consignee) {
this.consignee = consignee;
}

public void setLastupdby(String lastupdby) {
this.lastupdby = lastupdby;
}

public Shipmentgeo getShipmentgeo() {
return shipmentgeo;
}

public void setShipmentgeo(Shipmentgeo shipmentgeo) {
this.shipmentgeo = shipmentgeo;
}

}

public class ShipmentPK implements Serializable {

private String shipref;

private String traffictype;
public String getShipref() {
return shipref;
}

public void setShipref(String shipref) {
this.shipref = shipref;
}

public String getTraffictype() {
return traffictype;
}

public void setTraffictype(String traffictype) {
this.traffictype = traffictype;
}

public boolean equals(Object that) {
if (!(that instanceof ShipmentPK))
return false;
ShipmentPK ship = (ShipmentPK) that;
return ship.getShipref().equals(shipref)
&& ship.getShipref().equals(traffictype);
}

public int hashCode() {
return shipref.hashCode() + traffictype.hashCode();
}

}

import java.util.Date;

public class Shipmentgeo implements java.io.Serializable {

private ShipmentPK pk;
// implement getter and setter methods of pk

private Shipment shipment;

private String departure;

private String arrival;

// Constructors

/** default constructor */
public Shipmentgeo() {
}

/** minimal constructor */

/** full constructor */
public Shipmentgeo(String departure, String arrival) {

this.departure = departure;
this.arrival = arrival;
}

public String getDeparture() {
return this.departure;
}

public void setDeparture(String departure) {
this.departure = departure;
}

public String getArrival() {
return this.arrival;
}

public Shipment getShipment() {
return shipment;
}

public void setShipment(Shipment shipment) {
this.shipment = shipment;
}

}

Shipment.hbm.xml

<?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="Shipment" table="shipment"
schema="informix">
<composite-id name="pk" class="ShipmentPK">
<key-property name="shipref" type="string">
<column name="shipref" length="15" />
</key-property>
<key-property name="traffictype" type="string">
<column name="traffictype"/>
</key-property>
</composite-id>
<property name="shipper" type="string">
<column name="shipper" length="3" />
</property>
<property name="consignee" type="string">
<column name="consignee" length="3" />
</property>

<one-to-one name="shipmentgeo" class="Shipmentgeo" />
</class>

</hibernate-mapping>


///////////
Shipmentgeo.hbm.xml

<?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="Shipmentgeo" table="shipmentgeo"
schema="informix">
<composite-id name="pk" class="ShipmentPK">
<key-property name="shipref" type="string">
<column name="shipref" length="15" />
</key-property>
<key-property name="traffictype" type="string">
<column name="traffictype" />
</key-property>
</composite-id>
<property name="departure" type="string">
<column name="departure" length="3" />
</property>
<property name="arrival" type="string">
<column name="arrival" length="3" />
</property>
</class>
<one-to-one name="shipment" class="Shipment" />
</class>
</hibernate-mapping>



Hi Guys, this is the solution of one-to-one relation between two tables having composite key on both the tables. This is working well.

The approach to use same persitence class as a composite-id is not good performance. use another class for composite-id as shown above.

For any clarification you can contact me on rajavijaykumar@gmail.com
Hope this will help a lot you, All the best :-)

_________________
Thanks & Regards
Raja Vijay Kumar


Top
 Profile  
 
 Post subject: populte composit keys for two diff POJOS & two diiff Ta
PostPosted: Fri Nov 17, 2006 6:46 am 
Beginner
Beginner

Joined: Wed Nov 15, 2006 4:25 am
Posts: 21
Hai

I am doing exactly what you have said.But in my program I am using Contact instead of Shipment and DetailContact intstead of ShipmentGeo .But i exactlye dont know how to populate the primary key values where they are email and id in my case.So i wrote the code as



contact.getPk().setEmail("some@some.com");
contact.getPk().getId(2);

But it is displaying the msg that

Could not find a getter for id in class Contact .

So how to populate the primary key value exactly in this case pease tell me.

Thanks in advance.


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