使用@IdClass存储具有复合主键的实体,但无法持久 [英] Using @IdClass for storing entity with composite primary key, but fails to persist

查看:293
本文介绍了使用@IdClass存储具有复合主键的实体,但无法持久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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一起用作持久性提供程序,

I am using JPA with Hibernate as persistence provider,

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

But @IdClass which i have used is of import javax.persistence.IdClass;

所以哪里出了问题

推荐答案

我发现了解决方法:

EmployeeEntityTwoEmployeeId中的"id"字段应该相同.

The 'id' field in the classes EmployeeEntityTwo and EmployeeId should be same.

// EmployeeId.java;
private Integer id;

应该是

// EmployeeId.java;
private Integer employeeId;

我分别调整了吸气剂,它起作用了.

I adjusted the getter respectively, and it worked.

javax.persistence.IdClass JavaDocs:

From javax.persistence.IdClass JavaDocs:

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

The names of the fields or properties in the primary key class and the primary key fields or properties of the entity must correspond and their types must be the same.

这篇关于使用@IdClass存储具有复合主键的实体,但无法持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆