Search code examples
javaspringhibernateannotationsmany-to-many

Hibernate's ManyToMany simple use


I've two entities : CollabEntity and TechnoEntity.

  • A collab can use several technos.
  • A techno can be used by several collabs.

I would like to know all technos used by a specific collab.

My DB looks like:

CREATE TABLE collabs(
    co_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    co_firstname VARCHAR(25) NOT NULL,
    co_lastname VARCHAR(30) NOT NULL,
);

CREATE TABLE technos(
    te_id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
    te_tech VARCHAR(20)
);

CREATE TABLE cote( //Joins Collab & Techno
    co_id INT NOT NULL,
    te_id INT NOT NULL,
    CONSTRAINT fk_cote_co_id FOREIGN KEY(co_id) REFERENCES collabs(co_id),
    CONSTRAINT fk_cote_te_id FOREIGN KEY(te_id) REFERENCES technos(te_id),
    PRIMARY KEY(co_id, te_id)
);

CollabEntity.java

@Entity
@Table(name = "collabs")
public class CollabEntity {
    @Id
    @GeneratedValue
    @Column(name = "co_id")
    private long id;

    @Column(name = "co_firstname", nullable = false)
    private String firstname;

    @Column(name = "co_lastname", nullable = false)
    private String lastname;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "technos", 
               joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
               inverseJoinColumns = @JoinColumn(name = "te_id")
    )
    private Set<TechnoEntity> technos = new HashSet<TechnoEntity>();

    protected CollabEntity() {
    }

    public CollabEntity(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public long getId() {
        return id;
    }

    public String getFirstname() {
        return firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public Set<TechnoEntity> getTechnos() {
        return technos;
    }
}

TechnoEntity.java

@Entity
@Table(name = "technos")
public class TechnoEntity {
    @Id
    @GeneratedValue
    @Column(name = "te_id")
    private long id;

    @Column(name = "te_tech")
    private String tech;

    @ManyToMany(mappedBy="technos")
    private Set<CollabEntity> collabs = new HashSet<CollabEntity>();

    protected TechnoEntity() {}

    public TechnoEntity(String techno) {
        this.tech = techno;
    }

    public long getId() {
        return id;
    }

    public String getTech() {
        return tech;
    }
}

It's been a few hours I try things without finding a solution. This is my error :

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [fr/xxx/dao/DatabaseConfig.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

Precisely :

Caused by: org.hibernate.MappingException: Foreign key (FK_pc2r8qfxqv9jpgsfxrs61kfql:technos [te_id])) must have same number of columns as the referenced primary key (technos [co_id,te_id])


Solution

  • Change many to many mapping in CollabEntity. You need to change join table name. i.e. name from technos to some other name.

        @ManyToMany(cascade = CascadeType.ALL)
        @JoinTable(name = "collab_technos", 
                   joinColumns = {@JoinColumn(name = "co_id", nullable = false, updatable = false)},
                   inverseJoinColumns = @JoinColumn(name = "te_id")
        )