I have three classes one of the name is User and this user have other classes instances. Like this;
public class User{
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
public List<APost> aPosts;
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL)
public List<BPost> bPosts;
}
public class BPost extends Post {
@ManyToOne(fetch=FetchType.LAZY)
public User user;
}
public class APost extends Post {
@ManyToOne(fetch=FetchType.LAZY)
public User user;
}
it's working like this but generates emty tables in db. Which have to contains foreign keys. When I tried to use mappedBy and JoinColumn annotains I got failed. How can I resolve this?
Extra information:
When I changed with;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
public User user;
and
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id")
public List<APost> aPosts;
I'm getting
A JPA error occurred (Unable to build EntityManagerFactory): Repeated column in mapping for entity: models.post.APost column: id (should be mapped with insert="false" update="false")
Final Edit: Finally, I was totaly wrong about jpa annotaions. :( When i change
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="id")
to
@OneToMany(fetch=FetchType.LAZY, cascade = CascadeType.ALL, mappedBy="user")
and
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="user_id")
everthing works ok. :)
As I explained in this article and in my book, High-Performance Java Persistence, you should never use the unidirectional
@OneToMany
annotation because:Now, in your first example, both sides are owning the association, and this is bad.
While the
@JoinColumn
would let the@OneToMany
side in charge of the association, it's definitely not the best choice. Therefore, always use themappedBy
attribute on the@OneToMany
side.I am not really sure about your question (the meaning of "empty table" etc, or how
mappedBy
andJoinColumn
were not working).I think you were trying to do a bi-directional relationships.
First, you need to decide which side "owns" the relationship. Hibernate is going to setup the relationship base on that side. For example, assume I make the
Post
side own the relationship (I am simplifying your example, just to keep things in point), the mapping will look like:(Wish the syntax is correct. I am writing them just by memory. However the idea should be fine)
By doing so, the table for
Post
will have a columnuser_id
which store the relationship. Hibernate is getting the relationship by theuser
inPost
(Instead ofposts
inUser
. You will notice the difference if you havePost
'suser
but missingUser
'sposts
).You have mentioned
mappedBy
andJoinColumn
is not working. However, I believe this is in fact the correct way. Please tell if this approach is not working for you, and give us a bit more info on the problem. I believe the problem is due to something else.Edit:
Just a bit extra information on the use of
mappedBy
as it is usually confusing at first. InmappedBy
, we put the "property name" in the opposite side of the bidirectional relationship, not table column name.Since you will be saving APost and BPost directly, this means APost and BPost will be relationship owner. Since user doesn't owns the relationship, there will be an attribute mappedBy in @OneToMany annotation.
@JoinColumn annotation will be responsible for adding foreign key in Apost and BPost.