简体   繁体   中英

Hibernate JPA No join table created on ManyToMany association

I'm trying to make a many to many association with hibernate (with JPA) but my association table (between Film and Actor) is not created.

My actor class :

@Entity
public class Actor {

    private Long id;
    private String firstname;
    private String lastname;
    private int age;

    public Actor(){};

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

    @ManyToMany
    @JoinTable(name="actor_films", joinColumns=@JoinColumn(name="actor_id"), inverseJoinColumns=@JoinColumn(name="film_id"))
    private Set<Film> films=new HashSet<Film>();

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void addFilm(Film film){
        this.films.add(film);
    }

}

My Film class :

@Entity
public class Film {

    private Long id;
    private String title;
    @ManyToMany(mappedBy = "films")
    @JoinTable(name="actor_films", joinColumns=@JoinColumn(name="film_id"), inverseJoinColumns=@JoinColumn(name="actor_id"))
    private Set<Actor> actors=new HashSet<Actor>();


    public Film(){};
    public Film(String title){
        this.title=title;
    };


    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public void addActor(Actor actor){
        this.actors.add(actor);
    }



}

In my main method I do something like:

// begin transaction
Film film=new Film("DBZ");
Actor actor=new Actor("Bob","A");
em.persist(actor);
em.persist(film);
film.addActor(actor);
// do commit

All tables is created in my hsql database except the association table. So if anyone can help me.

You're mixing field and accessor use for your mapping annotations. You can't do that. In any given class, mapping annotations must be either all on the getters or all on the fields. If you try to mix them, some on the getters and some on the fields, Hibernate will pick getters or fields and ignore the other (I'm not sure if its specified how Hibernate chooses if you don't specify).

Your @Id annotation in each class is on a getter, and that's making Hibernate use your getter-based annotations. Either move the @Id and @GeneratedValue annotations to the id field in each class, or define a getter (and setter) for the actor/film relationship and move the @ManyToMany and @JoinTable annotations to the new getter.

With the annotations on the fields, Hibernate will bypass your getters/setters and access the entity internal fields directly. With them on the getters, Hibernate will call your getters and setters.

Help Hibernate a bit by providing it more information regarding your join table.
In your Film entity

 @ManyToMany(mappedBy = "films")
 @JoinTable(name="actor_films", joinColumns=@JoinColumn(name="film_id"), inverseJoinColumns=@JoinColumn(name="actor_id"))  

Do the same in your Actor entity, but reverse joinColumns and inverseJoinColumns

https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html/ch07.html#collections-bidirectional

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM