简体   繁体   English

如何从HQL表单中的两个连接表查询中选择*?

[英]How to make this select * from two joined tables query in HQL form?

I had two hibernate entity here with annotation: 我在这里有两个带有注释的hibernate实体:

@Entity
@Table(name = "CLIENT")
public class Client {

    private Long pkClient;
    private String name;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="PK_CLIENT")
    public Long getPkClient() {
        return pkClient;
    }
    public void setPkClient(Long pkClient) {
        this.pkClient = pkClient;
    }

    @Column(name="NAME")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    ...
}

@Entity
@Table(name="ACCOUNT")
public class Account {

    private Long pkClientAccount;
    private Long fkClient;
    private String accountNo;

    @Id
    @Column(name="PK_CLIENT_ACCOUNT")
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long getPkClientAccount() {
        return pkClientAccount;
    }
    public void setPkClientAccount(Long pkClientAccount) {
        this.pkClientAccount = pkClientAccount;
    }

    @Column(name="FK_CLIENT")
    public Long getFkClient() {
        return fkClient;
    }
    public void setFkClient(Long fkClient) {
        this.fkClient = fkClient;
    }

    @Column(name="ACCOUNT_NO")
    public String getAccountNo() {
        return accountNo;
    }
    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    ...
}

The relationship is one-to-many which a Client has many Account. 这种关系是一对多,客户有很多帐户。 Table ACCOUNT has foreign key (FK_CLIENT) to table CLIENT's primary key (PK_CLIENT). 表ACCOUNT具有到表CLIENT的主键(PK_CLIENT)的外键(FK_CLIENT)。

I want to perform this query in HQL form: 我想以HQL格式执行此查询:

select * from ACCOUNT a inner join CLIENT b on a.FK_CLIENT = b.PK_CLIENT

This mean, all properties from Account and Client entity will be selected. 这意味着,将选择来自帐户和客户实体的所有属性。

Anyone know how to make that query in HQL form? 任何人都知道如何以HQL形式进行查询?

AFAIK, in HQL we can only select one entity. AFAIK,在HQL中我们只能选择一个实体。

Note: 注意:
I cannot use @ManyToOne mapping in Account entity because there is already fkClient property and I can't change this because the get/setFkClient has already been used in other places. 我不能在Account实体中使用@ManyToOne映射,因为已经有fkClient属性而且我无法更改它,因为get / setFkClient已经在其他地方使用过。

The code above has been simplified by removing unrelated parts to make easier to read. 通过删除不相关的部分来简化上面的代码,使其更易于阅读。 If you find a typo, please let me know in the comment section since I typed the code manually. 如果您发现拼写错误,请在评论部分告诉我,因为我手动输入了代码。

Yes, you can select several entities with HQL. 是的,您可以使用HQL选择多个实体。 Hibernate will return an array of type Object[]. Hibernate将返回Object[].类型的数组Object[].

select 
    account, 
    client 
from Account account, Client client 
where account.fkClient = client.pkClient

With Hibernate 5.1 , it's now possible to join entities even if the mapping doesn't mirror the database table relationship. 使用Hibernate 5.1 ,即使映射不镜像数据库表关系,现在也可以加入实体。

So, this HQL query is valid from Hibernate 5.1: 所以,这个HQL查询在Hibernate 5.1中是有效的:

select 
    account, 
    client 
from Account account 
join Client client on account.fkClient = client.pkClient

Try following HQL 尝试使用HQL

select account from Account account, Client client where account.fkClient = client.pkClient

For more details please refer Hibernate reference documentation http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html#queryhql-where 有关更多详细信息,请参阅Hibernate参考文档http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/queryhql.html#queryhql-where

You can use: 您可以使用:

select * from  TB_1 as a 
left join  TB_2 as b 
on a.ID_TB_1 = b.ID_TB_2 and b.ID_TB_2 is null 

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

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