2014-03-30 32 views
0

如何在以下情況下管理複合主鍵。我有兩個班Employee和OnetoMany Relationship加班。帶鏈接類的JPA @ManytoOne中的複合主鍵

package com.gorakh.model; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 

@Entity 
public class Employee { 
@Id 
@GeneratedValue(strategy = GenerationType.IDENTITY) 
int empId; 
String first_name; 
String last_name; 
String phone; 
String start_date; 
String street_name; 
String region_name; 
String country_name; 
double salary; 
@ManyToOne 
@JoinColumn(name="dept_id",referencedColumnName="dept_id") 
Department dept_id; 
@ManyToOne 
@JoinColumn(name="type_id",referencedColumnName="type_id") 
Type type; 
public int getEmpId() { 
return empId; 
} 
public void setEmpId(int empId) { 
this.empId = empId; 
} 
public String getFirst_name() { 
return first_name; 
} 
public void setFirst_name(String first_name) { 
this.first_name = first_name; 
} 
public String getLast_name() { 
return last_name; 
} 
public void setLast_name(String last_name) { 
this.last_name = last_name; 
} 
public String getPhone() { 
return phone; 
} 
public void setPhone(String phone) { 
this.phone = phone; 
} 
public String getStart_date() { 
return start_date; 
} 
public void setStart_date(String start_date) { 
this.start_date = start_date; 
} 
public String getStreet_name() { 
return street_name; 
} 
public void setStreet_name(String street_name) { 
this.street_name = street_name; 
} 
public String getRegion_name() { 
return region_name; 
} 
public void setRegion_name(String region_name) { 
this.region_name = region_name; 
} 
public String getCountry_name() { 
return country_name; 
} 
public void setCountry_name(String country_name) { 
this.country_name = country_name; 
}  
public double getSalary() { 
return salary; 
}  
public void setSalary(double salary) { 
this.salary = salary; 
} 
public Department getDept_id() { 
return dept_id; 
} 
public void setDept_id(Department dept_id) { 
this.dept_id = dept_id; 
} 
public Type getType() { 
return type; 
} 
public void setType(Type type) { 
this.type = type; 
} 
} 

加班另類

package com.gorakh.model; 
import java.util.Date; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.IdClass; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 

@Entity 
@IdClass(Employee.class) 
public class Overtime { 
@Id 
@ManyToOne 
@JoinColumn(name="empId",referencedColumnName="empId") 
Employee empId; 

public Employee getEmpId() { 
return empId; 
} 
public void setEmpId(Employee empId) { 
this.empId = empId; 
} 
public String getOvertime_day() { 
return overtime_day; 
} 
public void setOvertime_day(String overtime_day) { 
this.overtime_day = overtime_day; 
} 
public int getOvertime_hour() { 
return overtime_hour; 
} 
public void setOvertime_hour(int overtime_hour) { 
this.overtime_hour = overtime_hour; 
} 
public double getOvertime_hour_pay() { 
return overtime_hour_pay; 
} 
public void setOvertime_hour_pay(double overtime_hour_pay) { 
this.overtime_hour_pay = overtime_hour_pay; 
} 
@Id 
String overtime_day; 
int overtime_hour; 
double overtime_hour_pay; 
} 

Test類

public class Test { 
public static void main(String[] args) { 

Overtime ot=new Overtime(); 
Employee ep=new Employee(); 
ep.setEmpId(11111111); 
ot.setEmpId(ep); 
ot.setOvertime_day("2014/3/30"); 
ot.setOvertime_hour(5); 
ot.setOvertime_hour_pay(500.40); 
EmployeeDB.save(ot); 

} 
} 

錯誤消息

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException 
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: [email protected] 
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Predeployment of PersistenceUnit [payroll] failed. 
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.gorakh.model.Employee] and those of the entity bean class [class com.gorakh.model.Overtime] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127) 
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107) 
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79) 
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) 
at com.gorakh.persistance.EmployeeDB.save(EmployeeDB.java:10) 
at Test.main(Test.java:42) 
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Predeployment of PersistenceUnit [payroll] failed. 
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.gorakh.model.Employee] and those of the entity bean class [class com.gorakh.model.Overtime] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1954) 
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1945) 
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98) 
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:96) 
... 5 more 
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.EntityManagerSetupException 
Exception Description: Predeployment of PersistenceUnit [payroll] failed. 
Internal Exception: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.gorakh.model.Employee] and those of the entity bean class [class com.gorakh.model.Overtime] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 
at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:230) 
... 9 more 
Caused by: Exception [EclipseLink-7150] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException 
Exception Description: Invalid composite primary key specification. The names of the primary key fields or properties in the primary key class [com.gorakh.model.Employee] and those of the entity bean class [class com.gorakh.model.Overtime] must correspond and their types must be the same. Also, ensure that you have specified ID elements for the corresponding attributes in XML and/or an @Id on the corresponding fields or properties of the entity class. 
at org.eclipse.persistence.exceptions.ValidationException.invalidCompositePKSpecification(ValidationException.java:1193) 
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.validatePrimaryKey(EntityAccessor.java:1523) 
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.processDerivedId(EntityAccessor.java:990) 
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processAccessorsWithDerivedIDs(MetadataProject.java:1524) 
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage3(MetadataProject.java:1821) 
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:580) 
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:585) 
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1869) 
... 7 more 

在大約情況如何使用複合主鍵

回答

2

你的這部分代碼是不正確:

@Entity 
@IdClass(Employee.class) 
public class Overtime { 

Overtime@IdClassEmployee。該@IdClass應該這樣指定:

@Entity 
@IdClass(OvertimeId.class) 
public class Overtime { 

OvertimeId這樣定義:

public class OvertimeId { 
    String overtime_day; 
    int empId; 
} 
+0

這是正確的,IdClass不能成爲你的實體類 – Koitoer

+0

我是從這樣嘗試很好,但我需要鏈接員工的empId。它也應該是外鍵和組合鍵。 – Gorakh

+0

在[JPA規範](http://jcp.org/aboutJava/communityprocess/final/jsr338/index.html)中討論了這種情況。參見2.4.1節。 –