-->
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.  [ 1 post ] 
Author Message
 Post subject: How do I map a join between two tables in Hibernate?
PostPosted: Thu Jun 06, 2013 1:01 am 
Newbie

Joined: Thu Jun 06, 2013 12:57 am
Posts: 1
Hi, I'm only 2 days into Hibernate... So please forgive me if this sounds too simple. I have two tables with the following details:

Employee(name, age, superior)
Employer(name, age, sex)

Now my .hbm.xml files look like:
Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="EmployeeModel"
table="Employee">

<id name="name" type="string" column="name">
<generator class="native">
</generator>
</id>

<property name="age"
type="long"
column="age" />
<property name="employer"
type="string"
column="employer" />

Employer.hbm.xml looks like:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="EmployerModel"
table="Employer">

<id name="name" type="string" column="name">
<generator class="native">
</generator>
</id>

<property name="age"
type="long"
column="age" />
<property name="sex"
type="string"
column="sex" />

And my two mapping files are:
Employee:

public class EmployeeModel {
private String name;
private String age;
private String employer;

public employee toEmp(){
emp e =new emp();
e.setName(getName());
e.setAge(getAge());
e.setEmployer(getEmployer());
}
}
//I've got the setters and getters for every single variable.

Employer:
public class EmployerModel {
private String name;
private String age;
private String sex;

public employee toEmp(){
emp e =new emp();
e.setName(getName());
e.setAge(getAge());
e.setSex(getSex());
}

Now, I wish to have a query like:

select emp.name,e.age,emp.name from Employee e Inner Join Employer emp where e.employer=emp.nSo I have two tables with the following details:

Employee(name, age, superior)
Employer(name, age, sex)

Now my .hbm.xml files look like:
Employee.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="EmployeeModel"
table="Employee">

<id name="name" type="string" column="name">
<generator class="native">
</generator>
</id>

<property name="age"
type="long"
column="age" />
<property name="employer"
type="string"
column="employer" />

Employer.hbm.xml looks like:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="EmployerModel"
table="Employer">

<id name="name" type="string" column="name">
<generator class="native">
</generator>
</id>

<property name="age"
type="long"
column="age" />
<property name="sex"
type="string"
column="sex" />

And my two mapping files are:
Employee:

public class EmployeeModel {
private String name;
private String age;
private String employer;

public employee toEmp(){
emp e =new emp();
e.setName(getName());
e.setAge(getAge());
e.setEmployer(getEmployer());
}
}
//I've got the setters and getters for every single variable.

Employer:
public class EmployerModel {
private String name;
private String age;
private String sex;

public employee toEmp(){
emp e =new emp();
e.setName(getName());
e.setAge(getAge());
e.setSex(getSex());
}

Now, I wish to have a query like:

select emp.name,e.age, e.age,emp.name,emp.sex from Employee e Inner Join Employer emp where e.employer=emp.name;

Basically I get back the employer's age and both their names. Now, how will I return this in hibernate? To get it back... I have to specify what type the return list() will be. In this case, I have both Employee and Employer. Hence, calling either of their toEmp() methods would not give me the values I want. Hence, How would I phrase this query so that I can get the required details back from both classes? Do I need to modify the classes? My mapping presently returns me only three values from either class, which is how the database columns are defined. However, I need to return 5 values here. How will my return values be?

Please do help me out.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.