简体   繁体   中英

Performing a Hibernate Join on two tables with a One to Many Association

I basically have a database with two tables in it with the following schema:

Employee 

|id|Age|Sex|

Table

|Table_id|Emp_id|Location|

My employee.hbm.xml file looks like:

    <property name="id"
          type="integer"
          column="emp_id" />
    <property name="age"
          type="integer"
          column="age" />
    <property name="sex"
          type="string"
          column="sex" />

My table.hbm.xml files looks like:

    <property name="table_id"
          type="integer"
          column="table_id" />
    <property name="emp_id"
          type="integer"
          column="emp_id" />
    <property name="location"
          type="string"
          column="location" />

I have the necessary classes with the mappings ie the getters and setters as well. They look like:

public class EmployeeModel {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="id", referencedColumnName="emp_id")

    private int id;
    private int age;
    private String sex;
 //Getters and setters for all the variables


public class TableModel {
@OneToMany(mappedBy="EmployeeModel")

    private int table_id;
    private int emp_id;
    private String location;

    private int table_id;
    private int emp_Id;
    private String location;
 //Getters and setters for all the variables

To collect the return data, I've created a newer class called EmployeeTableModel which looks like:

public class EmployeeTableModel{
    private int table_id;
    private int emp_id;
    private String location;
    private int table_id;
    private int emp_Id;
    private String location;    
    }

As you can understand, I need to basically go a call that gives me the details of the employee and the table he is sitting in. There is a one to one correlation between them. I understand my code has a many to one correlation, please do correct me on that. Presently, my query is:

List<EmployeeTableModel> data = sess.createCriteria(EmployeeModel.class)
          .setFetchMode("TableModel", FetchMode.JOIN) 
          .add(Restrictions.eq("emp_id", "ANY INPUT"  )).list();

I expect it to give me a query like:

select e.id,e.age,e.sex,t.table_id,t.emp_id,t.location from employee e, table t;

However, it instead gives me:

select e.id,e.age,e.sex from employee e;

I added a variable of Employee in Table, I corrected the queries, used HQL, but I still am unable to figure out what is wrong. Can someone please tell me just what is going wrong with it? Or give an alternate answer?

When using JPA/Hibernate, avoid thinking in columns when possible. Think of entities and let the framework do the change.

Your query is requesting just a list of EmployeeModel , because you do

   sess.createCriteria(EmployeeModel.class)

And that is what you are getting (the fact that you try to cast it to List<EmployeeTableModel> probably causes a compile time warning and a runtime error.

BTW, talking about thinking about entities, your model seems quite misguided. Your attributes should be entities (and not foreign keys); instead of

private int table_id;
private int emp_id;
private String location;

you do

private int table_id;
private Employee employee;
private String location;

Annotations appear to be at the wrong places, too, and you do not define IDs... I advise you to read Hibernate documentation again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM