Java JPA @OneToMany neededs to reciprocate @ManyToOne?
Posted
by bguiz
on Stack Overflow
See other posts from Stack Overflow
or by bguiz
Published on 2010-05-10T07:33:40Z
Indexed on
2010/05/10
8:04 UTC
Read the original article
Hit count: 239
Create Table A (
ID varchar(8),
Primary Key(ID)
);
Create Table B (
ID varchar(8),
A_ID varchar(8),
Primary Key(ID),
Foreign Key(A_ID) References A(ID)
);
Given that I have created two tables using the SQL statements above, and I want to create Entity
classes for them, for the class B
, I have these member attributes:
@Id
@Column(name = "ID", nullable = false, length = 8)
private String id;
@JoinColumn(name = "A_ID", referencedColumnName = "ID", nullable = false)
@ManyToOne(optional = false)
private A AId;
In class A
, do I need to reciprocate the many-to-one relationship?
@Id
@Column(name = "ID", nullable = false, length = 8)
private String id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "AId")
private List<B> BList; //<-- Is this attribute necessary?
Is it a necessary or a good idea to have a reciprocal @OneToMany
for the @ManyToOne
? If I make the design decision to leave out the @OneToMany
annotated attribute now, will come back to bite me further down.
© Stack Overflow or respective owner