gpt4 book ai didi

java - JPA 中 @ManytoOne 中的复合主键与链接类

转载 作者:行者123 更新时间:2023-12-02 06:00:10 28 4
gpt4 key购买 nike

以下情况如何管理复合主键。我有两个具有一对多关系的“员工”和“加类”类(class)。

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 {

Overtime@IdClass 不是 Employee@IdClass 应该这样指定:

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

OvertimeId定义如下:

public class OvertimeId {
String overtime_day;
int empId;
}

关于java - JPA 中 @ManytoOne 中的复合主键与链接类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22740745/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com