Hibernate:与@OneToOne单向关系的结果不一致

时间:2014-02-14 19:26:38

标签: java hibernate one-to-one hibernate-annotations

我在Hibernate中使用@OneToOne单向关系得到了不一致的结果。具体来说,我有一个User表,其中包含与其他表的几个关系:FooBar

Foo由用户的User属性(String)映射到uuidBar由用户的User属性(整数)映射到id

我的实体看起来像这样:

@Entity
@Table(name = "User")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "uuid")
    private String uuid;

    @OneToOne
    @JoinColumn(name = "uuid", referencedColumnName = "frn_object_uuid")
    private Foo foo;

    @OneToOne
    @JoinColumn(name = "id", referencedColumnName = "frn_user_id")
    private Bar bar;

    // getters and setters
}

@Entity
@Table(name = "Foo")
public class Foo {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "frn_object_uuid")
    private String objectUuid;

    @Column(name = "foo")
    private String foo;

    // getters and setters
}

@Entity
@Table(name = "Bar")
public class Bar {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "frn_user_id")
    private Integer userId;

    @Column(name = "bar")
    private String bar;

    // getters and setters
}

当我对User对象执行读取操作时,我得到Foo的结果,但Bar的结果为NULL。

我在这里缺少什么?如果我使用Bar mappedBy建立双向关系,我就能得到结果......非常奇怪。

提前致谢!

0 个答案:

没有答案