Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 休眠条件联接_Java_Hibernate_Criteria - Fatal编程技术网

Java 休眠条件联接

Java 休眠条件联接,java,hibernate,criteria,Java,Hibernate,Criteria,我有两门课,例如: @Entity public class Customer{ @Id private String id; private String name; @Column(nullable=false, unique=true) private String refId; } @Entity public class Account{ @Id private String id; @Column(nullable=

我有两门课,例如:

@Entity
public class Customer{
    @Id
    private String id;
    private String name;
    @Column(nullable=false, unique=true)
    private String refId;
}


@Entity
public class Account{
    @Id
    private String id;
    @Column(nullable=false, unique=true)
    private String accountNr;
    @Column(nullable=false)
    private String accountType;
    @Column(nullable=false, unique=true)
    private String refId;
}
我想在refId上加入这两个类,以便在帐户字段上进行订购,例如:

select c.* from Customer as c inner join Account as a on a.refId=c.refId orderBy a.accountType

有没有办法在条件中进行此类查询

您应该使用表之间的关系,例如
@OneToOne
@ManyToOne

之后,编写
HQL


您可以阅读更多信息。

Hibernate通常不允许您通过SQL查询中的
条件进行“按需”连接

但是如果您按照Alex的建议在
实体
类中映射关联,您可以创建一个更简单的
标准
,就像这样,因为
客户
将有一个
帐户
对象已经“映射”到它:

要进行关联,可以在
实体
类中用相关对象替换
refId
字段。因此,在
Customer
类中,您可以有一个
Account
对象关联,如下所示(反之亦然):


假设这是一对一的关系

但在我的例子中,类并不是相互参照的,这是一个一对一的关系,但不是在主键上。给定链接(DetachedCriteria)按预期工作,refId是所需的,我别无选择,只能这样继续。无论如何,谢谢你。
Criteria criteria = session.createCriteria(Customer.class)
        .createCriteria("account")
        .addOrder(Order.asc("accountType"));
@Entity
public class Customer {
    // Instead of "refId" field:
    @OneToOne(mappedBy = "customer")
    private Account account;
}

@Entity
public class Account {
    // Instead of "refId" field:
    @OneToOne
    @JoinColumn(name = "refIdColName")
    private Customer customer;
}