Hi there. I have two tables related together using one-to-many relation : employee -> department : and relation through department_id in the employee table.
I use hibernate : and my hibernate mapping files are:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping default-lazy="false">
<class catalog="moi"
name="com.ebla.moi.correspondence.model.entity.user.User" table="user">
<id name="id" type="java.lang.Long">
<column name="id"/>
<generator class="identity"/>
</id>
<many-to-one
class="com.ebla.moi.correspondence.model.entity.department.Department"
fetch="select" name="department">
<column name="department_id"/>
</many-to-one>
<property generated="never" lazy="false" name="name" type="java.lang.String">
<column length="128" name="name" not-null="true"/>
</property>
<property generated="never" lazy="false" name="email" type="java.lang.String">
<column length="128" name="email" not-null="true" unique="true"/>
</property>
<property generated="never" lazy="false" name="maritalStatus" type="java.lang.Short">
<column name="marital_status" not-null="true"/>
</property>
<property generated="never" lazy="false" name="hireDate" type="java.lang.String">
<column length="64" name="hire_date"/>
</property>
</class>
</hibernate-mapping>
and the second mapping file is :
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping default-lazy="false">
<class catalog="moi"
name="com.ebla.moi.correspondence.model.entity.department.Department" table="department">
<id name="id" type="java.lang.Long">
<column name="id"/>
<generator class="identity"/>
</id>
<property generated="never" lazy="false" name="name" type="java.lang.String">
<column length="256" name="name" unique="true"/>
</property>
<set inverse="true" name="users" sort="unsorted">
<key>
<column name="department_id"/>
</key>
<one-to-many class="com.ebla.moi.correspondence.model.entity.user.User"/>
</set>
</class>
</hibernate-mapping>
My problem is : sometimes i need to get the employee with his department, and other times i need only the employee information with out department information..... and the same thing with department with employee.... using the mapping file above the hibernate bring the department and it's users if i need the employee or not... how to define my needs to hibernate to fetch just what i need...
thank you