0

我有以下课程:

硬件.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;
    }
}

网络设备.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;
}

现在,如果我将设备添加到计算机,一切正常,但是如果我尝试将设备添加到 NetworkDevice,我会收到此错误:内部异常:

内部异常:

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..

提前致谢。

4

3 回答 3

1

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

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

于 2013-04-11T12:53:30.557 回答
0

检查将设备插入 NetworkDevice 的代码。看起来主表(硬件)中不存在 HW_ID。因为它试图在子表(设备)中插入不存在的主表 HW_ID 作为外键。

问候,拉维

于 2013-04-11T12:42:58.800 回答
0

是的,它来自您的继承策略。你不能使用@GeneratedValue(strategy=GenerationType.IDENTITY)with table_per_class

请参阅Java/Hibernate JPA:InheritanceType.TABLE_PER_CLASS 和 ID

于 2013-04-11T14:33:04.317 回答