I have three classes one of the names is User and this user has 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 empty tables in DB. Which have to contain foreign keys. When I tried to use mappedBy
and JoinColumn
annotations 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 totally wrong about JPA annotations. :( 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")
everything works ok. :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…