gpt4 book ai didi

java - 使用继承的 JPA OneToMany

转载 作者:行者123 更新时间:2023-12-01 14:41:07 31 4
gpt4 key购买 nike

我有以下类(class):

硬件.java

    @Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Hardware{
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private long id;
private String serialnumber;

@OneToMany (mappedBy="hardware")
private List<Equipment> equipments;
}

计算机.java

    @Entity
public class Computer extends Hardware {

private String hostname;
private String macAdress;
private String ipNumber;

public Computer(){
super();
}

public Computer(String serialnumber, String hostname,
String macAdress, String ipNumber) {
super(serialnumber);
this.hostname = hostname;
this.macAdress = macAdress;
this.ipNumber = ipNumber;
}
}

NetworkDeivce.java

    @Entity
public class NetworkDevice extends Hardware {

private String hostname;
private String ipAdress;

public NetworkDevice(){
super();
}

public NetworkDevice(String serialnumber, String hostname,
String ipAdress) {
super(serialnumber);
this.hostname = hostname;
this.ipAdress = ipAdress;
}
}

现在映射到硬件类的类:

设备.java

@Entity
public class Equipment {

public Equipment(){
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String serialnumber;
private String info;

@ManyToOne (fetch=FetchType.EAGER)
@JoinColumn(name="HW_ID")
private Hardware hardware;
}

现在,如果我将设备添加到计算机,一切都会正常工作,但如果我尝试将设备添加到网络设备,则会收到此错误:内部异常:

内部异常:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`inventorytool_beta`.`EQUIPMENT`, CONSTRAINT `FK_EQUIPMENT_HW_ID` FOREIGN KEY (`HW_ID`) REFERENCES `COMPUTER` (`ID`))
Error Code: 1452
Call: INSERT INTO EQUIPMENT (INFO, SERIALNUMBER, HW_ID) VALUES (?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(domain.hardware.Equipment@600a620d)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
at main.dbTest(main.java:74)
at main.main(main.java:18)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.4.1.v20121003-ad44345): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`inventorytool_beta`.`EQUIPMENT`, CONSTRAINT `FK_EQUIPMENT_HW_ID` FOREIGN KEY (`HW_ID`) REFERENCES `COMPUTER` (`ID`))
Error Code: 1452
Call: INSERT INTO EQUIPMENT (INFO, SERIALNUMBER, HW_ID) VALUES (?, ?, ?)
bind => [3 parameters bound]
Query: InsertObjectQuery(domain.hardware.Equipment@600a620d)
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:324)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:851)
at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:913)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`inventorytool_beta`.`EQUIPMENT`, CONSTRAINT `FK_EQUIPMENT_HW_ID` FOREIGN KEY (`HW_ID`) REFERENCES `COMPUTER` (`ID`))
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

所以我认为这是继承的问题...我不太擅长JPA..

提前致谢。

最佳答案

查看生成的架构。由于您的继承策略,您应该拥有三个不同的硬件表:一张用于 Hardware,一张用于 Computer,一张用于 NetworkDevice。问题是 HW_ID 只能引用一张表。在这里,您的 JPA 提供程序选择了 Computer,可能是因为它按字母顺序排列在第一个,但它无法处理所有三个类。

考虑使用另一种继承策略,例如JOIN

关于java - 使用继承的 JPA OneToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15948452/

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