No, your statement is not correct. You should not have to explicity state the join in this case. Assuming that Department to Employees is a one-to-many, ideally, you would have something like this
public class Department {
private long id;
private Set employees;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Set getEmployees() {
return employees;
}
public void setEmployees(Set employees) {
this.employees = employees;
}
}
public class Employee {
private Department department;
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
}
Here are the mapping files (assuming you are using sequences for id's)
Department.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="Department" table="DEPARTMENTS">
<id name="id" type="long" column="DEPARTMENT_ID">
<generator class="sequence">
<param name="sequence">DEPARTMENT_SEQUENCE</param>
</generator>
</id>
<set name="employees"
table="EMPLOYEES"
cascade="save-update"
lazy="true"
<key column="DEPARTMENT_ID" not-null="true"/>
<one-to-many class="Employee"/>
</set>
</class>
</hibernate-mapping>
Employee.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="Employee" table="EMPLOYEES">
<id name="id" type="long" column="EMPLOYEE_ID">
<generator class="sequence">
<param name="sequence">EMPLOYEE_SEQUENCE</param>
</generator>
</id>
<many-to-one name="department" column="DEPARTMENT_ID" not-null="true"/>
</class>
</hibernate-mapping>
This should give you a good starting place. To get the employees for a department, you can either
1) load the department by id
-- session.get(Department.class, id)
-- and access the employees in the session or turn off lazy loading (lazy="false") in the Department.hbm.xml
2) load the employees by department id (but previous might work better)
-- read the reference guide to explain more details about this since i use
-- criteria for all my queries and I am a little rusty on HQL
|