gpt4 book ai didi

java - Hibernate:在 HQL 中使用共享主键

转载 作者:行者123 更新时间:2023-12-01 09:22:04 25 4
gpt4 key购买 nike

当我使用实体映射共享主键时,我遇到了 Hibernate 创建的 sql 问题。我正在使用 JPA 2.1 和 Hibernate 5.2.2

这是我的实体:

@Entity
@Column(name = "employee_table")
public class EmployeeEntity {

@Id
@Column(name = "id")
@SequenceGenerator
@GeneratedValue
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private EmployeeDetailsEntity employeeDetailsEntity;
}

@Entity
@Table(name = "employee_details")
public class EmployeeDetailsEntity {

@Id
@Column(name = "id")
private Long id;

// additional attributes

}

我想选择所有有详细信息的员工:

select e from EmployeeEntity e where e.employeeDetailsEntity is not null;

Hibernate 生成的选择是:

select employeeen0_.id from employee_table employeeen0_ where employeeen0_.id is not null;

您能解释一下我做错了什么并帮助解决这个问题吗?

最佳答案

看起来您缺少 Employee -> EmployeeDetails 关系映射的“另一面”:

员工实体:

private EmployeeDetails employeeDetails;

@OneToOne(mappedBy = "employee", fetch = FetchType.LAZY)
public EmployeeDetails getEmployeeDetails() {
return employeeDetails;
}

EmployeeDetails 实体:

private Employee employee;

@OneToOne(optional = false)
@JoinColumn(name = "EMPLOYEE_ID")
public Employee getEmployee() {
return employee;
}

关于java - Hibernate:在 HQL 中使用共享主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40109498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com