首先我使用:
-Eclipse Kepler
-JPA 2.1
-MySql 5.1
-MySql连接器5.1
-EclipseLink 2.5
涉及4个班级:
- 涉及多对多关系的正常类(我不会发布此代码) -an抽象类(它只包含一个thisId字段) - 继承自前一个的普通类 - 继承自其他普通类
的另一个普通类这是最后两个类的代码
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class MyObject extends AbstractObject implements Serializable {
@Column(nullable = false)
private String name;
private String descr;
private static final long serialVersionUID = 1L;
@ManyToMany
@JoinTable(name = "relation",
joinColumns =
@JoinColumn(name = "thisId", referencedColumnName = "thisId"),
inverseJoinColumns =
@JoinColumn(name = "otherId", referencedColumnName = "otherId"))
private List<OtherClass> listHoldingTheRelationship;
// Getters and setters, other methods
}
多对多只有拥有方,因此所涉及的OtherClass中没有特殊的注释 这是最后一节课的代码:
public class MyObjectPers extends MyObject implements Serializable {
private static final long serialVersionUID = 1L;
private int field;
//getters setter, other methods
}
当我尝试创建MyObjectPers时,我得到了这个:
12:02:33 INSERT INTO `myDB`.`relation` (`otherId`, `thisId`) VALUES ("209",
"888") Error Code: 1452. Cannot add or update a child row: a foreign key
constraint fails (`mydb`.`relation`, CONSTRAINT `FK_PKG_ESC_thisId` FOREIGN KEY
(`thisId`) REFERENCES `MyObjectPers` (`thisId`)) 0.015 sec
我哪里出错了?