[英]Hibernate one-to-many to fetch only primary key column of the target entity
I have following two JPA entities: Company and Employee which follow one-to-many relationship:我有以下两个 JPA 实体:遵循一对多关系的公司和员工:
Employee
---------
eId | eName | company_id
Company
--------
cId | cName
I have been trying to fetch Employee IDs list in Company entity.我一直在尝试获取公司实体中的员工 ID 列表。 I tried with two ways - employees1 and employees2 as shown below.
我尝试了两种方法 - employees1 和 employees2 如下所示。 Both are not working .
两者都不工作。 Note that I want List of Employee IDs and not the List of Employees.
请注意,我想要的是员工 ID 列表,而不是员工列表。
@Entity
public class Employee {
@Id
String eId;
String eName;
}
@Entity
public class Company{
@Id
String cId;
String cName;
// both of the below are not working
@OneToMany(fetch = EAGER, targtEntity = Employee.class )
@JoinColumn(name = "company_id", referencedColumn = "cId")
@Column(name = "eId")
Set<String> employees1 = new HashSet<>();
@OneToMany(fetch = EAGER)
@JoinColumn(name = "company_id", tableName = "Employee", referencedColumn = "cId")
@Column(name = "eId")
Set<String> employees2 = new HashSet<>();
}
For employees1, I am getting: org.hibernate.property.access.spi.PropertyAccessException: Error accessing field.对于 employees1,我得到:org.hibernate.property.access.spi.PropertyAccessException:访问字段时出错。
For employees2: org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table.对于 employees2:org.hibernate.cfg.NotYetImplementedException:Collections 在辅助表中有 FK。
Please help!请帮忙!
Check out this article by Vlad Mihalcea , it explains everything in great detail: The optimal mapping would look like this:查看Vlad Mihalcea 的这篇文章,它非常详细地解释了所有内容:最佳映射如下所示:
@Entity
public class Employee {
@Id
private String eId;
private String eName;
@ManyToOne(fetch = FetchType.LAZY)
private Company refundRequest;
void setRefundRequest(Company refundRequest) {
this.refundRequest = refundRequest;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(eId, employee.eId);
}
@Override
public int hashCode() {
return Objects.hash(eId);
}
}
@Entity
public class Company {
@Id
private String cId;
private String cName;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "company", orphanRemoval = true)
private List<Employee> employees;
public void addEmployee(final Employee employee) {
employee.setRefundRequest(this);
employees.add(employee);
}
public void removeEmployee(final Employee employee) {
employee.setRefundRequest(null);
employees.remove(employee);
}
}
Make sure to read the above mentioned article to understand why this mapping is optimal!请务必阅读上面提到的文章以了解为什么此映射是最佳的!
In order to fetch it, you can use a repository:为了获取它,您可以使用存储库:
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, String> {
@Query("select e.eId from Employee e where e.company = :company")
List<String> findEmployeeIdsByCompany(@Param("company") Company company);
}
By the way, eager fetching is a massive code smell and should be avoided!顺便说一下,eager fetching 是一种巨大的代码味道,应该避免!
@ElementCollection
is the annotation you need to use in this case. @ElementCollection
是您在这种情况下需要使用的注释。 @CollectionTable
defines in which table you want to fetch the value from as well as on which column the join happens. @CollectionTable
定义您要从哪个表中获取值以及连接发生在哪个列上。 @Column
defines the column name you want to fetch from the @CollectionTable
@Column
定义要从@CollectionTable
中获取的列名
@Entity
class Company {
@Id
String cId;
String cName;
@ElementCollection
@CollectionTable(name = "Employee", joinColumns = @JoinColumn(name = "company_id"))
@Column(name = "eId")
Set<String> employeeIds = new HashSet();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.