我刚刚将 OpenJPA 从 1.2 版更新到 2.2 版,我的许多 JUnit 都失败了。其中大多数是因为 ManyToOne 关系改变了行为。假设我们有两个实体
package org.my;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
@Entity
public class Many {
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private One one;
public One getOne() {
return one;
}
public void setOne(One one) {
this.one = one;
}
}
和
package org.my;
import java.util.List;
@Entity
public class One {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "one")
private List<Many> many;
public List<Many> getMany() {
return many;
}
public void setMany(List<Many> many) {
this.many = many;
}
}
当我创建一个对象并将其设置为多个列表时,比坚持和提交我得到一个像这样的异常
org.apache.openjpa.persistence.InvalidStateException: The field "one" of instance "org.my.Many@4d9cfefb" contained a null value; the metadata for this field specifies that nulls are illegal.
我以为 OpenJPA 猫会找到 One 对象并将其放入 Many.one 字段中,我错了吗?如果没有,我怎么能做到这一点?