【问题标题】:Using @IdClass for storing entity with composite primary key, but fails to persist使用@IdClass 存储具有复合主键的实体,但无法持久化
【发布时间】:2013-03-16 11:23:03
【问题描述】:

我的id类如下,

public class EmployeeId implements Serializable{
    public EmployeeId(){}
    public EmployeeId(Integer id, String country){
        this.id = id;
        this.country = country;
    }

    private Integer id;
    private String country;

    @Override
    public int hashCode(){        
        return this.getCountry().hashCode() + getId();
    }

    @Override
    public boolean equals(Object o){
        boolean flag = false;
        EmployeeId myId = (EmployeeId) o;

        if((o instanceof EmployeeId) 
                && (this.getCountry().equals(myId.getCountry()))
                && (this.id == myId.getId())){
            flag = true;
        }
        return flag;
    }
// rest of the code with getters only
}

以下是我使用的实体

@Entity
@IdClass(EmployeeId.class)
@Table(name="TBL_EMPLOYEE_FOUR")
public class EmployeeEntityTwo {

    public EmployeeEntityTwo(){}
    public EmployeeEntityTwo(Integer id,String country, String empName){
        this.country = country;
        this.employeeId = id;
        this.empName = empName;
    }

    @Id
    @Column(name="ID")
    private Integer employeeId;

    @Id
    @Column(name="COUNTRY",length=50)
    private String country;

    @Column(name="NAME",length=50)
    private String empName;
// getters and setters
}

这是我的桌子

create table TBL_EMPLOYEE_FOUR(
    ID integer, 
    COUNTRY varchar(50),
    NAME varchar(50),
    constraint PK_EMP_00239 primary key(ID,COUNTRY)
)

这就是我想要运行的东西

 private static void idClassStore(EntityManager em) throws Exception{
     List<EmployeeEntityTwo> employees = Arrays.asList(new EmployeeEntityTwo(12, "KENYA", "Ridushi Ogambe"),
                                                       new EmployeeEntityTwo(13, "GHANA", "Mikila Hanza"),
                                                       new EmployeeEntityTwo(14, "EGYPT", "Abdul Hameed Fagdaul"),
                                                       new EmployeeEntityTwo(15, "MOROCCO", "Jamil Mahmoud"),
                                                       new EmployeeEntityTwo(16, "LIBERIA", "Robert Damus"));

     for(EmployeeEntityTwo employee : employees){
         em.persist(employee);            
       }
}

但我得到一个例外

Caused by: org.hibernate.AnnotationException: Property of @IdClass not found in entity com.entities.EmployeeEntityTwo: id

我使用 JPA 和 Hibernate 作为持久性提供程序,

但我使用的@IdClass 是 import javax.persistence.IdClass;

那么哪里出了问题,

【问题讨论】:

    标签: java jpa jpa-2.0


    【解决方案1】:

    我找到了解决办法:

    EmployeeEntityTwoEmployeeId 类中的“id”字段应该相同。

    // EmployeeId.java;
    private Integer id;
    

    应该是

    // EmployeeId.java;
    private Integer employeeId;
    

    我分别调整了getter,效果很好。


    来自javax.persistence.IdClassJavaDocs:

    主键类中的字段或属性的名称与实体的主键字段或属性的名称必须对应,并且它们的类型必须相同。

    【讨论】:

    • 点击您的答案旁边的复选标记,以便清楚地回答这个问题。
    • 我不能,它说我可以在两天后回答我自己的问题
    • 我遇到了同样的错误。错误消息与实际问题不符。非常模棱两可。
    • 另一个答案给出了一些提示。问题来自注释。 stackoverflow.com/a/3774245/10809764
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2019-02-01
    • 2021-07-30
    • 1970-01-01
    相关资源
    最近更新 更多