0

I'm new to this and I have read documentation and in my opinion it doesn't quite show how this can be done from a beginner's perspective.The examples shown only show examples from a single or two tables at most. Any help would be most appreciated.

SELECT EMPP.empid, EMPP.firstName, EMPP.middleName, EMPP.lastName, EMPJ.jobTitle, 
       EMPJ.status, EMPJ.department, EMPR.manager 
FROM   Emp_PersonalDetails EMPP 
JOIN   Emp_JobDetails EMPJ 
ON     EMPP.EMPID = EMPJ.EJDID 
JOIN   Emp_Reporting EMPR 
ON     EMPP.EMPID = EMPR.RDID 
WHERE  EMPP.firstName LIKE :name 
OR     EMPP.empid LIKE:id

1 Answer 1

0

Assuming you have an Emp_PersonalDetails class with an association named 'reporting' that references an instance of Emp_Reporting class, then something like the following should work. Note that I've used nameVar and empIdVar for your like conditions. Also note that this will return an instance of Emp_PersonalDetails, but will fetch the Emp_Reporting at the same time. You'll have to access the Emp_Reporting through the instance of Emp_PersonalDetails returned.

session.createCriteria( Emp_PersonalDetails.class )
       .setFetchMode( "reporting", FetchMode.JOIN )
       .setFetchMode( "details", FetchMode.JOIN )
       .add( Restrictions.like( "firstName", nameVar ))
       .add( Restrictions.like( "empid", empIdVar ))

Your Hibernate mapping could look something like:

<class name="example.Emp_PersonalDetails" table="...">
  <id name="empid" column="empid" ...>
  <property name="firstName" ...>
  ...
  <one-to-one name="reporting" class="example.Emp_Reporting" lazy="proxy"...>
  <one-to-one name="details"  class="example.Emp_JobDetails" lazy="proxy" ...>
</class>

<class name="example.Emp_Reporting" ...>
  <id name="id" ...>
    <column="rdid" />
    <generator class="foreign">
      <param name="property">emp</param>
    </generator>
  </id>
  <one-to-one name="emp" class="example.Emp_PersonalDetails" constrained="true" />
  <property name="manager" ...>
  ...
</class>

<class name="example.Emp_JobDetails"...>
  <id name="id" ...>
    <column="rdid" />
    <generator class="foreign">
      <param name="property">emp</param>
    </generator>
  </id>
  <one-to-one name="emp" class="example.Emp_PersonalDetails" constrained="true" />
  <property name="jobTitle" ...>
  ...
</class>
7
  • There are three tables that I need to access in this query. The third one is "Emp_JobDetails". I tried this and I got the following exception: Caused by: org.hibernate.QueryException: could not resolve property: empid of: org.charles.periodic.metals.beans.Emp_PersonalDetails Dec 23, 2014 at 16:35
  • I didn't see your last edit. Just add another setFetchMode(), which will tell Hibernate to fetch the JobDetails when it retrieves the results. This assumes the JobDetails is available through a 'details' relationship.
    – wbdarby
    Dec 23, 2014 at 16:38
  • Ok Thanks. The tables are joined by primary key in one-to-one relationships. The database is an HR database and stores employees details across 8 tables each table with 8 to 12 columns. Each table has only one record in the next table. I've managed to set Hibernate to automatically create the table for and join them via primary key. So should I replace the "reporting" association you mentioned with the ID of the next table and so on.....? Thanks Dec 23, 2014 at 16:50
  • What is the Hibernate mapping? I'll edit the response with a possible mapping for primary keys.
    – wbdarby
    Dec 23, 2014 at 16:53
  • code @OneToOne(mappedBy = "details", cascade = CascadeType.ALL) private Emp_ContactDetails contactDetails; @OneToOne(mappedBy = "details", cascade = CascadeType.ALL) private Emp_EmergencyContacts emgContactDetails; code Dec 23, 2014 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.