Luo
Luo

Reputation: 445

Hibernate connections: @OneToMany, @JoinTable or @SecondaryTable?

I am new to Hibernate connections between entities and tables.

I want to have the following model schema (entities in database):

User    Seminar    SeminarParticipation
-----   -------    --------------------
id      id         seminar_id
name    name       user_id
...     ...        role

I know that the third table with entities coud be created automatically by @ManyToMany, but it has an additional field, so I want to have a full entity to make the access in Java easier.

I also want to retrieve participations in Seminar and User entities.

My question is how to map this entities in Java in the most easy way without possible multiple queries in DAOs in the future?

I've tried this approach:

Seminar class (User is similar):

@Entity
@Table(name = "seminars")
public class Seminar {

    @Id
    @GeneratedValue
    private int id;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<SeminarParticipation> participations;

    ...
}

And SeminarParticipation class:

@Entity
@Table(name = "seminar_participations")
public class SeminarParticipation {

    @Id
    @GeneratedValue
    private int id;

    @ManyToOne
    private Seminar seminar;

    @ManyToOne
    private User user;

    @Column(nullable = false)
    private int role;

    ...
}

But this code tries to create another table, and my purpose is only to match this entities between each other, and it would be grateful if changing List<SeminarParticipation> in Seminar or User will affect entities in SeminarParticipation table as well when working with the SeminarDAO or UserDAO.

I've also found solutions with @JoinTable and @SecondaryTable, but I'am not sure they may help...

Upvotes: 0

Views: 282

Answers (2)

Alan Hay
Alan Hay

Reputation: 23246

I ma not sure exactly what your issue is however @SecondaryTable is definitely not what you would use in this scenario.

If you are having an issue with the mappings you have created, it is most likely related to the fact you have not specified the 'mappedBy' property which is mandatory in the case of bi-directional relationships.

http://docs.oracle.com/javaee/5/api/javax/persistence/OneToMany.html#mappedBy()

Thus, User and Seminar should specify the mappedBy property on their @OneToMany associations:

@OneToMany(mappedBy = "user" ....)
private List<SeminarParticipation> participations;

@OneToMany(mappedBy = "seminar" ....)
private List<SeminarParticipation> participations;

Upvotes: 1

v.ladynev
v.ladynev

Reputation: 19976

You need to have other approach to mapping persistent classes to the schema.

User and Seminar are something like reference items (without @OneToMany). And SeminarParticipation is the same as you already have.

@Entity
@Table(name = "seminars")
public class Seminar {

    @Id
    @GeneratedValue
    private int id;

}

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue
    private int id;

}

@Entity
@Table(name = "seminar_participations")
public class SeminarParticipation {

    @Id
    @GeneratedValue
    private int id;

    @ManyToOne
    private Seminar seminar;

    @ManyToOne
    private User user;

    @Column(nullable = false)
    private int role;

}

Upvotes: 0

Related Questions