Reputation: 2259
I am new to hibernate.
I have the following situation,
@Entity(name="A")
Class A {
@Id
int id;
Set<B> listB;
}
@Entity(name="B")
Class B {
@Id
CompositebPK bPk;
}
Class CompositebPK {
@Column(name="id")
int aId;
@Column(name="user_name")
String uname;
}
@Entity (name = "user")
Class User {
@Column (name ="user_name")
String uname;
}
table Structures is as follows,
Table A:
int id
Table B:
int id
String uname
User:
String uname
String password
A to B is 1 to many relation. B to User is 1 to many.
I want to have the list of B's in A.
What is the best approach to solve the situation?
In the net when I search I am just getting information for inverse join for simple columns, if I try that its not working out, so is there a special way to achieve this?
Upvotes: 1
Views: 1699
Reputation: 72884
Try the following mapping configuration:
In the A
entity class:
@Entity(name="A")
class A {
@Id
int id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "bPk.a")
@Cascade(value = { CascadeType.DELETE })
Set<B> listB;
}
In CompositebPk
:
class CompositebPK {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(referencedColumnName = "id", nullable = false) })
A a;
@Column(name="user_name")
String uname;
}
Upvotes: 1