简体   繁体   English

通过一对多关联在两个表上执行休眠联接

[英]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: 我的employee.hbm.xml文件如下所示:

    <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: 我的table.hbm.xml文件如下所示:

    <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. 我有必要的类与映射,即getter和setter方法。 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: 为了收集返回数据,我创建了一个名为EmployeeTableModel的新类,它看起来像:

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. 我在表中添加了一个Employee变量,我更正了查询,使用了HQL,但是我仍然无法弄清楚出了什么问题。 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. 使用JPA / Hibernate时,请尽可能避免在各列中进行思考。 Think of entities and let the framework do the change. 考虑实体,让框架进行更改。

Your query is requesting just a list of EmployeeModel , because you do 您的查询仅请求EmployeeModel的列表,因为您这样做

   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. 这就是您得到的(尝试将其强制转换为List<EmployeeTableModel>的事实可能会导致编译时警告和运行时错误。

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. 注释似乎也位于错误的位置,并且您没有定义ID。我建议您再次阅读Hibernate文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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