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.
|