0

您将如何仅使用 FOO_ID 将 bar 对象列表映射到 Foo 对象?

@Entity
@Table(name="FOO")
class Foo {
    @EmbeddedId
    private FooPK primaryKey;

    @OneToMany
    @JoinColumn(name="FOO_ID", referencedColumnName="FOO_ID") //does not work
    private List<Bar> bars;
}

@Embeddable
class FooPK {
    @Column(name="FOO_ID")
    private String id;

    @Column(name="FOO_SUB_ID")
    private String subId;
}

@Entity
@Table(name="BAR")
class Bar {
    @EmbeddedId
    private BarPK primaryKey;

    //other attributes and methods
}

@Embeddable
class BarPK {
    @Column(name="FOO_ID")
    private String id;

    @Column(name="BAR_ID")
    private String barId;

    @Column(name="BAR_SUB_ID")
    private String barSubId;
}

我试过:@JoinColumn(如上)和@JoinTable 在另一个包含 FOO_ID 作为主键的表上。两者都不起作用。

4

1 回答 1

0

按照此处所述尝试: http ://stackoverflow.com/questions/5305687/join-entity-with-composite-key

Foo不做任何特殊的映射:

class Foo {
    @EmbeddedId
    private FooPK primaryKey;

    @OneToMany(mappedBy="foo")
    private List<Bar> bars;
}

所以在另一边映射它:

class Bar {
    @EmbeddedId
    private BarPK primaryKey;

    @ManyToOne
    @MapsId("id")
    private Foo foo;
}
于 2013-03-22T16:39:01.400 回答