These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria Restriction on many-to-one foreign key
PostPosted: Thu Feb 11, 2010 5:30 am 
Newbie

Joined: Thu Feb 11, 2010 5:12 am
Posts: 2
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;

}


Top
 Profile  
 
 Post subject: Re: Criteria Restriction on many-to-one foreign key
PostPosted: Thu Feb 11, 2010 8:17 am 
Newbie

Joined: Thu Feb 11, 2010 5:12 am
Posts: 2
found something is working atm want to share it in case if someone else will need this.

Code:
Criteria criteria = hbmsession.createCriteria(Personel.class);
criteria = criteria.add(Restrictions.eq("ad", ad));
criteria = criteria.add(Restrictions.eq("soyad", soyad));
criteria.createCriteria("dept").add(Restrictions.eq("departmanAdi", departman));


this does look for "ad","soyad" and "departmanAdi" (in departman table) equalities.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.