icurtis16
icurtis16

Reputation: 11

Proper mapping ( JPA ) composite key

For example i have entities

@Entity
public class A{
@Id
Long Id;
...
}
@Entity
public class B{
@Id
Long Id;
...
}
@Entity
@IdClass(ABId.class).
public class AB{
@Id
@ManyToOne
private A a;
@Id
@ManyToOne
private B b;
private boolean state;

}
Class for composite primary key:
public ABId implements Serializable{
Long a;
Long b;
.........
}

and i want to get from class A something like this select * from AB ab where ab.a_id=1; ( id from A object)

i did such mapping in class A

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumns({
        @JoinColumn(name="A_id", referencedColumnName="id"),
        @JoinColumn(name="B_id", referencedColumnName="id")
    })
List<AB> listAB;

but it does nothing i alawys get empty list.

Upvotes: 0

Views: 136

Answers (1)

icurtis16
icurtis16

Reputation: 11

Solved,

I did mapping

@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
    @JoinColumn(name="A_id", referencedColumnName="id")
})
List<AB> listAB;

and it works like it should.

Upvotes: 1

Related Questions