I got two classes: Forum and Topic.
I previously told I should have Forum attribute (in Topic), something like that:
@ManyToOne
@JoinColumn(name = “forum”)
protected Forum forum;
instead of just keeping the id of the forum.
I saw that in postgresql the Forum attribute saved as “bigint” (the forum id)
so what’s the point of holding the Forum reference anyway?
This is exactly why you use a object relational mapping (ORM) solution (here JPA). The problem is: you can’t really put references into your database, but in your code, it’s so much better to have references than plain ids. JPA does this for you. You can create a topic, set it’s forum attribute, to a forum object and JPA takes care of the insertion to the database (you don’t have to get the id attribute of the forum, then insert it by hand with an sql statement).
Consider this:
Simply you can navigate backwards from topic to the forum, and call the forum’s method. This reference is better than having an id there:
Here you had to find the forum associated with the topic as you only had an id. Much worse.
Simply put: you can easily navigate backwards (from topic to forum) in your java code, so you have a reference in your Topic class, but this can only be represented in sql as a foreign key.
ps: to be more precise JPA is an interface, the implementation used by you (eg. Hibernate, EclipseLink) is the ORM solution which does the work.