Define Mapping relationship with JPA
I am having difficulties to figure out what's proper w开发者_StackOverflow社区ay of defining the following relationships with JPA2 mapping.
create table TableA {
id int primary key
name varchar(255) not null
);
create table TableB {
x_id int not null REFERENCES TableA(id),
y_id int not null REFERENCES TableA(id),
PRIMARY KEY (x_id, y_id)
);
Since there is a composite key involved, I know I need to define a serializable class such as FooPK
.
@Embeddable
public class FooPK {
@Column(name = "x_id", insertable=true, nullable=false)
private long x_id;
@Column(name = "y_id", insertable=true, nullable=false)
private long y_id;
...
}
But I am lost on where to define @OneToMany and @ManyToOne relationship and how to properly write down the @JoinColumn() in this case.
Any help is much appreciated.
Oliver
Try to use @ManyToMany annotation.
You don't need to define the intermediate class.
More info: http://download.oracle.com/javaee/5/api/javax/persistence/ManyToMany.html
精彩评论