Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So 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.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? Have more functions?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.