Hi again,
Here's the many-to-many mapping:
Employee.java:
Code:
public class Employee implements Serializable {
private List<Group> groups;
...
}
Group.java:
Code:
public class Group {
private List<Employee> employees;
...
}
employee.hbm.xml:
Code:
<hibernate-mapping>
<class name="Employee" table="en_employee">
<id name="empId" column= "empId" type="int">
<generator class ="increment"/>
</id>
<property name="empName"/>
<property name="empDOB" type="date"/>
<property name="empAddress"/>
<property name="empTel" type="int"/>
<property name="empEmail"/>
<property name="empLastName"/>
<set name="groups" table="en_group_employee">
<key column="empId"/>
<many-to-many column="gpId"
class="Group"/>
</set>
</class>
<query name="findAllEmployees">
<![CDATA[
SELECT distinct emp
FROM Employee emp
LEFT JOIN FETCH emp.groups
]]>
</query>
</hibernate-mapping>
group.hbm.xml:
Code:
<hibernate-mapping>
<class name="Group" table="en_group">
<id name="gpId" column= "gpId" type="int">
<generator class ="increment"/>
</id>
<set name="employees" inverse="true" table="en_group_employee">
<key column="gpId"/>
<many-to-many column="empId"
class="Employee"/>
</set>
<property name="gpName"/>
<property name="gpDesc"/>
</class>
<query name="findAllGroups">
<![CDATA[
SELECT distinct grp
FROM Group grp
LEFT JOIN FETCH grp.employees
]]>
</query>
</hibernate-mapping>
EmployeeDao.java:
Code:
public class EmployeeDao {
private Session session;
private Transaction transaction;
public EmployeeDao() {}
public void create(Employee emp) {
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
session.save(emp);
transaction.commit();
//session.close();
}
@SuppressWarnings(value="unchecked")
public List<Employee> findAll() {
session = HibernateUtil.getSessionFactory().getCurrentSession();
transaction = session.beginTransaction();
Query query = session.getNamedQuery("findAllEmployees");
return (List<Employee>)query.list();
}
}
and my test class:
Code:
public class EmployeeAndGroupTest extends TestCase {
public void testCreateEmp() {
Employee emp = new Employee();
emp.setEmpName("Bugs");
emp.setEmpLastName("Bunny");
EmployeeDao empDao = new EmployeeDao();
empDao.create(emp);
Group engGroup = new Group();
engGroup.setGpName("Engineering");
List<Employee> engineers = new ArrayList<Employee>();
engineers.add(emp);
engGroup.setEmployees(engineers);
GroupDao groupDao = new GroupDao();
groupDao.create(engGroup);
List<Employee> allEmployees = empDao.findAll();
for (Employee employee: allEmployees) {
System.out.println("Employee First Name = " + employee.getEmpName());
System.out.println("Employee Last Name = " + employee.getEmpLastName());
System.out.println("Employee member of Groups = " + employee.getGroups());
}
}
}
I didn't put GroupDao.java because it's almost exactly the same. I didn't have enough time to work it all out (I have a little problem with Hibernate), but it's not far from working. I'm used to using Annotations and Spring, so trying to get Hibernate to work all by itself it proving a little too time-consuming for me.
The mapping should be correct though. You see, you don't need to explicitly work with the relation table at all!