我有两个对象,ObjectA
class ObjectA
{
Long id;
ObjectB b;
@OneToOne(fetch = FetchType.LAZY, targetEntity = ObjectB.class, mappedBy = "a")
@JoinColumn(name = "OBJECTB_ID")
public ObjectB getB() {
return b;
}
@Override
public void setB(ObjectB b) {
this.b = b;
}
@Override
@Id
@SequenceGenerator(name = "OBJECTA_SEQ", sequenceName = "OBJECTA_SEQ", allocationSize = 100)
@GeneratedValue(generator = "OBJECTA_SEQ")
@Column(name = "ID", nullable = false, unique = false)
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id= id;
}
}
和ObjectB
class ObjectB
{
Long id;
ObjectA a;
@OneToOne(fetch = FetchType.LAZY, optional = false, targetEntity = ObjectA.class)
@JoinColumn(name = "OBJECTA_ID", referencedColumnName = "OBJECTA_ID", nullable = false)
public ObjectA getA() {
return a;
}
@Override
public void setA(ObjectA a) {
this.a = a;
}
@Override
@Id
@SequenceGenerator(name = "OBJECTB_SEQ", sequenceName = "OBJECTB_SEQ", allocationSize = 100)
@GeneratedValue(generator = "OBJECTB_SEQ")
@Column(name = "ID", nullable = false, unique = false)
public Long getId() {
return id;
}
@Override
public void setId(Long id) {
this.id= id;
}
}
当我设置ObjectA.b
和ObjectB.a
并保持不变时,数据库OBJECTB.OBJECTA_ID
中的内容会被设置,但OBJECTA.OBJECTB_ID
未设置,因此{{1} }与ObjectB
无关,但未设置ObjectA
与ObjectA
的关联。查看EclipseLink提供的SQL语句,ObjectB
的更新语句最终看起来像
ObjectA
忽略字段INSERT INTO OBJECTA (ID) VALUES....
,这可以解释为什么它没有被设置。我假设这是由于我如何设置注释。我的注释设置导致这种情况发生的问题是什么?