Hibernate - ManyToOne & Inheritance / JOINED / mappedBy Hibernate - ManyToOne & Inheritance / JOINED / mappedBy mysql mysql

Hibernate - ManyToOne & Inheritance / JOINED / mappedBy


I don't think you can achieve this by mapping the ManyToOne association to User generically in the UserActivity entity. That's probably too confusing for the JPA provider (Hibernate).

Instead, I think you need to map the association to User in each of the Question, Answer and Comment entities. Yes, I know that would be duplicated code, but it looks like the only way you will then be able to qualify the OneToMany mappings in User using the mappedBy reference.

For instance, your Question entity would have an association defined as:

@ManyToOne(cascade = { MERGE, PERSIST }, fetch = LAZY)@JoinColumn(name = "ua_user_id")private User questionUser;

Depending on how clever (or not) Hibernate is about the above association, you may need to specify the table="USER_ACTIVITY" in the JoinColumn annotation.

Then the User would have the OneToMany as:

@OneToMany(mappedBy="questionUser", cascade = REMOVE)private List<Question> questions = new ArrayList<>();

Similarly for each of Answer and Comment.

Of course, I haven't tried this, so I could be wrong.


It's probably happening because when you set the @OneToMany mapping then the hibernate will create an auxiliary table that will store the id from the entities on the relationship.

In this case you should try the following:

@OneToMany(cascade = REMOVE)@JoinColumn(name = "answer_id")private List<Answer> answers = new ArrayList<>();

The @JoinColumn annotation will map the relationship without the creation of the auxiliary table, so it's pretty likely this solution will help you in this situation.


Try this mapping, this should work as you expect according to section 2.2.5.3.1.1 of the documentation:

@Entitypublic class User {    @OneToMany(cascade = REMOVE)     @JoinColumn(name="user_fk") //we need to duplicate the physical information    private List<Question> questions = new ArrayList<>();        ...}@Entitypublic class Question {    @ManyToOne    @JoinColumn(name="user_fk", insertable=false, updatable=false)    private User user;    ...}

The reason why the auxiliary association is created, is that there is no way for Hibernate to know that the Many side of the relation (for example Question) has a foreign key back to User that corresponds to the exact same relation as User.questions.

The association Question.user could be a completely different association, for example User.questionCreator or User.previousSuccessfulAnswerer.

Just by looking at Question.user, there is no way for Hibernate to know that it's the same association as User.questions.

So without the mappedBy indicating that the relation is the same, or @JoinColumn to indicate that there is no join table (but only a join column), Hibernate will trigger the generic one-to-many association mapping solution that consists in creating an auxiliary mapping table.

The schema misses such association tables, which causes the error that can be solved with the mapping above.