I have 2 classes that are connected with a foreign key which is a many to one relationship in mapping. Problem is when i want to make a criteria based on the entity of the connected table. First class is the employee class and the 2nd one is the department they are bound to. when i query a criteria with restriction on a employee entity its working fine and i can get the department object in the resulting employee objects and can filter them manually but i'd rather to add another restriction to make it easier but could not find any good tutorials or examples regarding this issue or i could not recognize the sample i need since i am not a pro on hibernate and mappings and criterias thats why i decided to make a post on these forum since i am stuck for a while on this. I ll write the mappings class codes but since they are in my native language they might not be so informative feel free to ask for info about them. I ll add comments into codes to make it more clear. The employee table has a column holds department id which is the foreign key and primary key in departments table. The criteria i want to make is getting some employees with some restirictions.eq where some entities are from employee class but 1 is the department name which is on departments table.
Code:
public class Personel {
private long personelId;
private String ad;
private String soyad;
private Double saatlikUcret;
private String unvan;
private String tag;
private Departman dept; //this is the related table
private Timestamp girisTarih;
//...other code here contructor getters setters etc.
}
Code:
<?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 package="personel">
<class name="Personel" table="personel">
<id name="personelId" type="long">
<column name ="personelId" />
</id>
<property name="ad" type="string" length="100">
<column name="ad"/>
</property>
<property name="soyad" type="string" length="100">
<column name="soyad"/>
</property>
<property name="saatlikUcret" type="double">
<column name="saatlikUcret"/>
</property>
<property name="unvan" type="string" length="45">
<column name="unvan"/>
</property>
<property name="tag" type="string" length="100">
<column name="tag"/>
</property>
<property name="girisTarih" type="java.sql.Timestamp">
<column name="girisTarih"/>
</property>
<many-to-one name="dept"
class="Departman" column="departmanId"
cascade="all" not-null="true" />
</class>
</hibernate-mapping>
Code:
public class Departman {
private int id; //this is the foreign key in the employee table
private String departmanAdi;
}