-->
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.  [ 6 posts ] 
Author Message
 Post subject: Mapping a join relation
PostPosted: Mon Jun 16, 2008 1:55 pm 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
Hello.
I have problems writing my mapping file. Here is my situation :
I have a table T_CONTRACT and a table T_CLIENT. T_CONTRACT is containing an id of T_CLIENT (representing the subsriber).
I would like to make a join between those 2 table so that i can get something like
Code:
Contract ct = ContractDAO.findById(1);
Client cl = ct.getFkSubscriber();

so far, i get to write this :
Contract.hbm.xml
Code:
<hibernate-mapping package="dao.person">
    <class name="Contract" table="contract">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="identity" />
        </id>
        <property name="idSubscriber" type="int">
            <column name="ID_CLIENT" not-null="true" />
        </property>       
     
        <join table="CLIENT" inverse="true" optional="true">
           <key column="ID" unique="true"/>
           <many-to-one name="fkSubscriber" column="ID_CLIENT"
                class="dao.client.Client" not-null="true" unique="true"/>
        </join>
    </class>
</hibernate-mapping>

Client.hbm.xml
Code:
<hibernate-mapping package="dao.client">
    <class name="Cient" table="client">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="identity" />
        </id>
     ...
    </class>
</hibernate-mapping>

But it doesn't work at all (a sql syntax exception is raised)
Can someone help me please ?


Top
 Profile  
 
 Post subject: Mapping a join relation
PostPosted: Mon Jun 16, 2008 3:10 pm 
Newbie

Joined: Mon Feb 21, 2005 2:15 pm
Posts: 9
Could you share the Exception ?

-Gayatri


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 5:30 pm 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
javax.servlet.ServletException: org.hibernate.exception.SQLGrammarException: could not execute query

I dont think it's really relevant of my problem. Does anybody have a <join> example to gave me ? :(
The ones of the hibernate documentation are not so helpy to me

I have been trying this :
Code:
       <join table="PERSON"
           inverse="true"
           optional="true" >
          <key column="ID" />
           <many-to-one name="fkSubscriber" class="dao.person.Person"
           column="CUSTOMER" not-null="true"/>
       </join>

I obtain this sql :
Code:
select
   contract0_.ID as ID4_,
   contract0_.ID_PERSON as ID2_4_,
   contract0_.ISSUED_AT as ISSUED5_4_,
   contract0_.NAME as NAME4_,
   contract0_1_.FIRSTNAME as FIRSTNAME5_
from myDB.contract contract0_
left outer join PERSON contract0_1_
   on contract0_.ID=contract0_1_.ID

It's almost it, but it seems that Hibernate join automatically the id of the curent table (T_CONTRACT) but i need to join my table from an another id named ID_PERSON, so I could get :
Code:
left outer join PERSON contract0_1_
   on contract0_.ID_PERSON=contract0_1_.ID

How can i set that ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 7:10 pm 
Newbie

Joined: Tue Apr 29, 2008 1:42 pm
Posts: 12
Hi. I think you misunderstood the join-table part. It is the table(other than client and contract) which maps the contract ids to client ids. For example:

Contract.java

public class Contract {
private int id;
private Client fkSubscriber;
public void setId(int id){
this.id = id;
}
public void setFkSubscriber(Client fkSubscriber){
this.fkSubscriber = fkSubscriber;
}
public int getId(){
return id;
}
public Client getFkSubscriber(){
return fkSubscriber;
}
}

The xml mapping file for Contract(Contract.hbm.xml) will be:

<?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="Contract" table="CONTRACT">
<id name="id" column="ID" type="int" >
<generator class="increment"/>
</id>

<join table="CONT_CLIENT" optional="true" >
<key column="ID" unique="true"/>
<many-to-one name="fkSubscriber" column="CLIENT" not-null="true"/>
</join>
</class>
</hibernate-mapping>

The name of the table given in<join table...> is the name of the new table that maps contracts to clients.

here are the other files:
Client.java

public class Client {
private int id;
private String fullName;
public void setId(int id){
this.id = id;
}
public void setFullName(String fullName){
this.fullName = fullName;
}
public int getId(){
return id;
}
public String getFullName(){
return fullName;
}
}

Client.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="Client" table="CLIENT">
<id name="id" type="int">
<column name="ID" />
<generator class="increment" />
</id>
<property name="fullName" type="java.lang.String" column="FNAME"/>
</class>
</hibernate-mapping>

A file called "Loader.java" that inserts and retrieves the list of contracts.


import java.util.*;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class Loader {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = cfg.buildSessionFactory();

Session session1 = sessionFactory.getCurrentSession();
session1.beginTransaction();

Contract con1 = new Contract();
Contract con2 = new Contract();
Contract con3 = new Contract();
Client clnt1 = new Client();
Client clnt2 = new Client();
clnt1.setFullName("client 1");
clnt2.setFullName("client 2");
session1.save(clnt1);
session1.save(clnt2);
con1.setFkSubscriber(clnt1);
con2.setFkSubscriber(clnt1);
con3.setFkSubscriber(clnt2);

session1.save(con1);
session1.save(con2);
session1.save(con3);


session1.getTransaction().commit();
Session session2 = sessionFactory.getCurrentSession();
session2.beginTransaction();
List<Contract> conts = session2.createQuery("from Contract").list();
System.out.println("the size of the list of contracts is " + conts.size());
for(Object m : conts){
Contract loadedContract = (Contract) m;
System.out.println(loadedContract.getId() +"\t" + loadedContract.getFkSubscriber().getFullName());

}
session2.getTransaction().commit();

}
}

For SQl part:
create table contract(id int not null);
create table client(id int not null, fname char(255));
create table cont_client(id int not null, client int not null);

I hope you got ideas now. If there's any question feel free to post it.

Thanks.

Do not forget to rate the post!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 8:02 pm 
Newbie

Joined: Thu Jun 12, 2008 10:52 am
Posts: 16
Hi,
Thanks for your reply. I think i got what you try to explain to me.
But the example you gave me do not what i'm expecting.
I would like to join my table contract with a person, with respectively the id CONTRACT.ID_PERSON and PERSON.ID. So that hibernate create a object of type Person named fkSubscriber each time it fetch a row of Contract.
I hope it's more clear now.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 9:22 pm 
Newbie

Joined: Tue Apr 29, 2008 1:42 pm
Posts: 12
I think I got your problem. But just to make sure can you please specify what your classes are and what constitutes these classes?

Try using <key property-ref="...."...>

This link might help you.

http://www.redhat.com/docs/manuals/jboss/jboss-eap-4.2/doc/hibernate/Hibernate_Reference_Guide/Mapping_declaration-key.html

Thanks.


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