Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JPA @OneToMany needed to reciprocate @ManyToOne?

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, would that come back to bite me further along?

like image 455
bguiz Avatar asked May 10 '10 07:05

bguiz


People also ask

Does OneToMany need ManyToOne?

If you want to use @OneToMany , @ManyToOne is required. However, the inverse is not required: If you only care about the @ManyToOne relationship, you can define it without having @OneToMany on the related entity.

Can a JPA entity have multiple OneToMany associations?

You can have multiple one-to-many associations, as long as only one is EAGER.

What is @OneToMany in Java?

A OneToMany relationship in Java is where the source object has an attribute that stores a collection of target objects and if those target objects had the inverse relationship back to the source object it would be a ManyToOne relationship.

Which of the following annotations will be required to set up a one-to-many bidirectional association?

The @ManyToOne annotation lets us create bidirectional relationships too.


2 Answers

Is it a necessary or a good idea to have a reciprocal @OneToMany for the @ManyToOne?

No, it's not mandatory at all, it's a pure design decision. The whole question is... Do you want this (i.e. an uni-directional association):

uni-directional

Or this (i.e. a bi-directional association):

bi-directional

If you don't need to get Bs from A, then you can skip the bs attribute and the OneToMany on A side.

If I make the design decision to leave out the @OneToMany annotated attribute now, will come back to bite me further down.

No, and you can add it later if you discover that you need it.

like image 115
Pascal Thivent Avatar answered Nov 06 '22 10:11

Pascal Thivent


They are optional. There is no need to add them to your model if you don't want to use them.

I'd sugguest to avoid the reverse mapping at all because such collections may become quite large and most persistance layers don't handle these very good. In many cases you'd have to deal with add/remove of already loaded/managed entities related to these collections yourself. So only add those if they really make things easier for you.

like image 2
Daniel Bleisteiner Avatar answered Nov 06 '22 11:11

Daniel Bleisteiner