6
我的ID級別如下,使用@IdClass用於存儲實體組合大主鍵,但未能堅持
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是進口javax.persistence.IdClass的;
那麼,是我走錯了,
點擊答案旁邊的複選標記,以清楚地看到這個問題得到解答。 – sharakan 2013-03-26 15:53:34
我不能,它說我兩天後可以回答我自己的問題 – 2013-03-26 15:54:47
我得到了同樣的錯誤。錯誤消息與實際問題不符。非常含糊。 – 2017-04-21 11:19:00