'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.
Solution 1:[1]
Solved,
I did mapping
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="A_id", referencedColumnName="id")
})
List<AB> listAB;
and it works like it should.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andronicus |
