HQL Hibernate INNER JOIN [英] HQL Hibernate INNER JOIN

查看:692
本文介绍了HQL Hibernate INNER JOIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在Hibernate中执行这个SQL语句?我想用Hibernate来创建查询,而不是创建数据库。

How can I do this SQL sentence in Hibernate? I would like to use Hibernate to create queries, not create database.

SELECT * FROM Employee e INNER JOIN Team t ON e.Id_team=t.Id_team

我在SQLServer2008中创建实体类,

I created entity classes in SQLServer2008,

@Entity
@Table(name="EMPLOYEE")
public class Employee
{
    @Id @GeneratedValue
    @Column(name="ID_EMPLOYEE")
    private int id_employee;
    @Column(name="SURNAME")
    private String surname;
    @Column(name="FIRSTNAME")
    private String firstname;
    @Column(name="ID_PROFESSION")
    private int id_profession;
    @Column(name="ID_BOSS")
    private int id_boss;
    @Column(name="HIRED_DATE")
    private Date hired;
    @Column(name="SALARY")
    private double salary;
    @Column(name="SALARY_ADD")
    private double salary_add;
    @Column(name="ID_TEAM")
    private int id_team;
//set and get

@Entity
@Table(name="TEAM")
public class Team
{
    @Id @GeneratedValue
    @Column(name="ID_TEAM")
    private int id_team;
    @Column(name="TEAMNAME")
    private String teamname;
    @Column(name="ADDRESS")
    private String address;
//set and get

我试图用很多方式构建工作选择查询,但它仍然不起作用。

I tried to build working select query in many ways but it still doesn't work.

            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            Session session = sessionFactory.openSession();                 
            session.beginTransaction();
            String select = "FROM Employee e INNER JOIN Team t ON e.Id_team=t.Id_team";
            Query query = session.createQuery(select);
            List elist = query.list();
            session.getTransaction().commit();
            session.close();    

可能某物对实体是错误的吗?

Maybe sth is wrong with entities?

推荐答案

连接只能在实体之间存在关联时使用。您的Employee实体不应该有一个名为 id_team ,类型 int 的字段映射到列。它应该与Team实体有一个ManyToOne关联,映射为JoinColumn:

Joins can only be used when there is an association between entities. Your Employee entity should not have a field named id_team, of type int, mapped to a column. It should have a ManyToOne association with the Team entity, mapped as a JoinColumn:

@ManyToOne
@JoinColumn(name="ID_TEAM")
private Team team;

然后,以下查询可以完美地工作:

Then, the following query will work flawlessly:

select e from Employee e inner join e.team

这将加载所有员工,除了那些不与任何团队关联的员工。

Which will load all the employees, except those that aren't associated to any team.

所有其他字段都是相同的,这些字段是其他字段的外键table当然是映射成一个实体( id_boss id_profession )。

The same goes for all the other fields which are a foreign key to some other table mapped as an entity, of course (id_boss, id_profession).

现在是您阅读Hibernate文档的时候了,因为您错过了它的一个非常重要的部分以及它是如何工作的。

It's time for you to read the Hibernate documentation, because you missed an extremely important part of what it is and how it works.

这篇关于HQL Hibernate INNER JOIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆