Reputation: 4371
I've got two database entities: Forum
and Topic
.
Topic
has protected long forumId
data member, which indicates of course the Topic
's forum.
My question is what annotation to use for this data member?
Upvotes: 42
Views: 145673
Reputation: 15489
@ManyToOne
As the annotation implies - you have many topics per forum
Upvotes: 1
Reputation: 4137
As others have answered -
You should use the ManyToOne
, and JoinColumn
annotations.
Bare in mind , that since JPA is about ORM - Object relational mapping,
You should reference another object as you would have done "naturally" in Java - i.e via an object and not via its identifier (which is forumId) in your case),
This was one of the design consideration between the relations at JPA and Hibernate (previously to JPA).
Upvotes: 6
Reputation: 23916
As Forum has many topics, and a topic belongs to one and only Forum, you probably want to go with a Forum type attribute annotated with @ManyToOne
:
@ManyToOne
@JoinColumn(name = "forumId")
private Forum forum;
See more:
ManyToOne and JPA mapping
Upvotes: 55