gpt4 book ai didi

java - JPA 2 : Implicit column reference in the @MapsId mapping fails

转载 作者:行者123 更新时间:2023-11-30 09:27:15 25 4
gpt4 key购买 nike

我正在尝试使用多个映射属性。但是当我尝试运行代码时,代码会生成异常,此示例基于 Pro JPA 2 第 10 章一书。有以下代码:

DeptID.java

public class DeptId implements Serializable{

private static final long serialVersionUID = 5177373337405448966L;
private int number;
private String country;
.....................

ProjectId.java

public class ProjectId implements Serializable{

private static final long serialVersionUID = 4239980609226293562L;
private String name;
private DeptId deptId;
.....................

Department.java

@Entity
@IdClass(DeptId.class)
public class Department implements Serializable {

private static final long serialVersionUID = 1L;
@Id
private int number;
@Id
private String country;
private String name;

@OneToMany(mappedBy="deptId")
List<Project> projects;
......................

Project.java

@Entity
@IdClass(ProjectId.class)
public class Project implements Serializable {

private static final long serialVersionUID = 1L;
@Id
private String name;

@Id
@ManyToOne
@JoinColumn(name="dept_id")
private Department deptId;
........................

这段代码会产生下面的异常

Caused by: org.hibernate.AnnotationException: Implicit column reference in the @MapsId mapping fails, try to use explicit referenceColumnNames: example.domain.Department

最佳答案

DeptID 有两个主键,所以你不能有像 @JoinColumn(name="dept_id") 这样的东西。相反,使用这个:

@JoinColumns({
@JoinColumn(name="dept_number", referencedColumnName="number"),
@JoinColumn(name="dept_country", referencedColumnName="country")
})

如果这仍然不起作用:在主键类(DeptID 和 ProjectId)中尝试切换到公共(public)或 protected 私有(private)属性。还要检查它们是否具有不带参数的构造函数并实现了 equals 和 hashcode 方法。

关于java - JPA 2 : Implicit column reference in the @MapsId mapping fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14642227/

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