Eclipselink不会在提交时保留set字段

时间:2016-06-26 02:06:55

标签: java jpa eclipselink

我有两个对象,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.bObjectB.a并保持不变时,数据库OBJECTB.OBJECTA_ID中的内容会被设置,但OBJECTA.OBJECTB_ID未设置,因此{{1} }与ObjectB无关,但未设置ObjectAObjectA的关联。查看EclipseLink提供的SQL语句,ObjectB的更新语句最终看起来像

ObjectA

忽略字段INSERT INTO OBJECTA (ID) VALUES.... ,这可以解释为什么它没有被设置。我假设这是由于我如何设置注释。我的注释设置导致这种情况发生的问题是什么?

0 个答案:

没有答案