简体   繁体   English

多对多关系返回空集合

[英]Many-to-many relation returns empty collection

I am trying to create two entities which have many-to-many relation between them. 我正在尝试创建两个实体,它们之间具有多对多关系。 First entity is Person with PID as primary key, second is Serie with SID as primary key. 第一个实体是将PID作为主键的Person ,第二个是将SID作为主键的Serie In database there is a table TJV_5_SERIE_2_PERSON , which represents many to many relationship between these entities. 在数据库中,有一个表TJV_5_SERIE_2_PERSON ,表示这些实体之间的多对多关系。

tables in database 数据库中的表

The problem is when I retrieve any entity, Collection annotated with @ManyToMany is always empty. 问题是当我检索任何实体时,用@ManyToMany注释的Collection始终为空。 So I assume I've messed up something in my code that explains why my many-to-many relation doesn't work. 因此,我假设我在代码中弄乱了一些内容,以解释为什么我的多对多关系不起作用。

I retrieve these two entities by generating (in Netbeans 9.0) 'Restful Web Services from Entity classes'. 我通过在Netbeans 9.0中生成“来自实体类的Restful Web Services”来检索这两个实体。 This way I can use these services to retrieve all attributes succesfully, except Collection with @ManyToMany annotation is always empty. 这样,我可以使用这些服务成功检索所有属性,但带有@ManyToMany批注的Collection始终为空。

Any idea why it is not woking appreciated. 任何想法,为什么它不被唤醒赞赏。 It is first time trying this, so pardon me for any dumm mistakes. 这是第一次尝试,请原谅任何愚蠢的错误。

Person class: 人类:

@Entity
@Table(name = "TJV_5_PERSON")
@XmlRootElement
public class Person implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "PID")
    private Integer id;

    @Column(name = "PNAME")
    private String name;

    @ManyToMany()
    @JoinTable(
        name = "TJV_5_SERIE_2_PERSON",
        joinColumns = @JoinColumn(name = "PID", referencedColumnName = "PID"),
        inverseJoinColumns = @JoinColumn(name = "SID", referencedColumnName = "SID")
    )
        // always empty
    private Collection<Serie> favourites = new ArrayList<Serie>();

    public Person() {
    }

    public Person(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlTransient
    public Collection<Serie> getFavourites() {
        return favourites;
    }

    public void setFavourites(Collection<Serie> favourites) {
        this.favourites = favourites;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + Objects.hashCode(this.id);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Person{" + "id=" + id + ", name=" + name + ", favourites=" + favourites + '}';
    }

}

Serie class: 意甲课:

@Entity
@Table(name = "TJV_5_SERIE")
@XmlRootElement
public class Serie implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "SID")
    private Integer id;

    @Column(name = "STITLE")
    private String title;

    // always empty
    @ManyToMany(mappedBy = "favourites")
    private Collection<Person> fans = new ArrayList<Person>();

    public Serie() {
    }

    public Serie(Integer id, String title) {
        this.id = id;
        this.title = title;
    }

    public Integer getId() {
        return id;
    }

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

    public String getTitle() {
        return title;
    }

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

    @XmlTransient
    public Collection<Person> getFans() {
        return fans;
    }

    public void setFans(Collection<Person> fans) {
        this.fans = fans;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 67 * hash + Objects.hashCode(this.id);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Serie other = (Serie) obj;
        if (!Objects.equals(this.id, other.id)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Serie{" + "id=" + id + ", title=" + title + ", fans=" + fans + '}';
    }

}

I am not 100% sure, but you may not retrieving any results beacuse of @XMLTransiet annotation above the Serie.class method 我不确定100%,但是您可能无法通过Serie.class方法上方的@XMLTransiet注释检索任何结果

@XmlTransient
public Collection<Person> getFans() {
    return fans;
}

Try to look in documentation https://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlTransient.html or in connected posts Hide an entity variable from xml message - @XmlTransient not working 尝试查看文档https://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlTransient.html或相关文章隐藏xml消息中的实体变量-@XmlTransient不起作用

The other issue is cascading data between two corresponding @ManyToMany tables. 另一个问题是在两个对应的@ManyToMany表之间级联数据。 It means that you have intersection and the data appears in this table automatically when you use some type of cascade but you need send a POST request. 这意味着您具有交集,并且当您使用某种类型的级联但需要发送POST请求时,数据会自动显示在此表中。 It means in your service class layer you can create a method responsible for creating Person and assign a Serie to this Person object which is a foreign key. 这意味着在您的服务类层中,您可以创建一个负责创建Person的方法,并为此外键将此Person对象分配一个Serie。 The article about cascading is here :) https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/ 关于级联的文章在这里:) https://vladmihalcea.com/a-beginners-guide-to-jpa-and-hibernate-cascade-types/

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

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