Java JPA中@ManytoOne中带有链接类的复合主键

Java JPA中@ManytoOne中带有链接类的复合主键,java,eclipse,spring,jpa,Java,Eclipse,Spring,Jpa,在以下情况下如何管理复合主键。我有两个班,一个是员工,一个是加班,一个是夫妻关系 package com.gorakh.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; i

在以下情况下如何管理复合主键。我有两个班,一个是员工,一个是加班,一个是夫妻关系

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; 
}
测试班

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: sun.misc.Launcher$AppClassLoader@631d75b9
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 

在关于如何使用复合主键的情况下,这部分代码不正确:

@Entity
@IdClass(Employee.class)
public class Overtime {
加班
@IdClass
不是
员工
@IdClass
的指定方式如下:

@Entity
@IdClass(OvertimeId.class)
public class Overtime {
public class OvertimeId {
    String overtime_day;
    int empId;
}
OvertimeId
定义如下:

@Entity
@IdClass(OvertimeId.class)
public class Overtime {
public class OvertimeId {
    String overtime_day;
    int empId;
}

这是正确的,IdClass不能是您的实体classI也是通过这种方式尝试的,但我需要从Employee链接empId。它应该是外键和复合键。在[JPA规范]()中讨论了这种情况。见第2.4.1节。