当前位置: 首页 > 知识库问答 >
问题:

JPA中@ManyToone的复合主键与链接类

赵同
2023-03-14
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 

在关于如何使用复合主键的情况下

共有1个答案

荣轶
2023-03-14

您的代码的这一部分不正确:

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

加班@idclass不是Employee@idclass应如下所示:

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

overtimeID定义如下:

public class OvertimeId {
    String overtime_day;
    int empId;
}
 类似资料:
  • EmbeddedId或IdClass注释用于表示复合主键。如何在没有(EmbeddedId或IdClass)的情况下使用复合主键? 如果可以在没有(EmbeddedId或IdClass)的情况下使用复合主键,那么如何使用EntityManager.find(Entity Class,Object primaryKey)方法在复合主键(Multiple primaryKey)的情况下查找实体(因为没

  • 问题内容: 我的JPA模型中有以下类(省略了getters,setters和无关字段): 我需要定义一个类,使得当从所述类生成DDL时,相应的表的主键被由密钥和。我尝试了以下方法: 但这会为表生成以下内容: 请注意,和都是可为空的,当我尝试将DDL加载到SQL Server时会导致以下错误 无法在表“ PRICE”中的可为空的列上定义PRIMARY KEY约束 我不明白为什么这些可以为空,因为在域

  • 我有两个实体的订单和项目。在Order entity中,id是OrderId、UserId的复合主键,但在Items中,外键仅为Order id。 如何使用订单Id获取项目列表 我尝试使用mappedBy,如上所述,无论在哪种情况下,我都得到了错误。 任何帮助都是非常感谢的。

  • 我很难在Hibernate中映射复合主键连接。下面是我的模式的简化图片。请注意,我不能更改架构。 简化模式 引用com.corp.activity.tree的referencedColumnNames(id)未映射到单个属性 如何在Hibernate中构建这样的关系?我必须映射到单个属性(id),尽管主键是复合的。

  • 用户详细信息 有人能看出我做错了什么吗?

  • 我有一个复合主键,其中一个键为空。实体中必须存在哪些注释,以便我可以使用此类JPA获取数据: 我尝试使用IdClass Annotation和EmbeddedId Annotation,但出现异常: 例如,使用了数据,但在实际项目中使用了类似的数据,因此无法用值替换null。