jean
jean

Reputation: 2990

hibernate use other class Id as this class Id

@Entity
class A {
    @Id
    int id;
}

@Entity
class B {
    @Id 
    @OneToOne
    A a;
}

B object never refer to same A object, so I want a.id to be the id of B.

How to write the annotation?

Upvotes: 2

Views: 451

Answers (1)

Blokje5
Blokje5

Reputation: 5003

Change the B object to the following:

@Entity
class B {
    @Id
    int id;

    @OneToOne 
    @MapsId
    A a
}

See the hibernate documentation on OneToOne.

Upvotes: 3

Related Questions