简体   繁体   English

Hibernate JPA没有在ManyToMany关联上创建联接表

[英]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. 我正在尝试与休眠(与JPA)建立多对多关联,但未创建我的关联表(在Film和Actor之间)。

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. 除关联表外,所有表均在我的hsql数据库中创建。 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. 在任何给定的类中,映射注释必须全部在getter上或全部在字段上。 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). 如果您尝试将它们混合在一起,有些在getter上,有些在字段上,则Hibernate将选择getters或字段,而忽略其他(我不确定是否指定了Hibernate如何选择,如果您未指定)。

Your @Id annotation in each class is on a getter, and that's making Hibernate use your getter-based annotations. 每个类中的@Id注释都在getter上,这使Hibernate使用基于getter的注释。 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. @Id@GeneratedValue批注移动到每个类的id字段中,或者为演员/电影关系定义一个吸气剂(和设置器),然后将@ManyToMany@JoinTable批注移动到新的吸气剂。

With the annotations on the fields, Hibernate will bypass your getters/setters and access the entity internal fields directly. 使用字段上的注释,Hibernate将绕过您的getter / setter方法并直接访问实体内部字段。 With them on the getters, Hibernate will call your getters and setters. 将它们放置在吸气剂上后,Hibernate会称呼您的吸气剂和吸气剂。

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 在Actor实体中执行相同的操作,但反向joinColumns和inverseJoinColumns

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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