【问题标题】:null pointer exception when I want to use many to many with java entities当我想对 java 实体使用多对多时出现空指针异常
【发布时间】:2013-05-30 11:43:41
【问题描述】:

我有三个表( People、Group、person_group ),我想获取特定组的人,所以我使用了

selectedGroup.getPeopleCollection();

这是我的实体类:

  @Entity
@Table(name = "group_u")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "GroupU.findAll", query = "SELECT g FROM GroupU g"),
    @NamedQuery(name = "GroupU.findById", query = "SELECT g FROM GroupU g WHERE g.id = :id"),
    @NamedQuery(name = "GroupU.findByName", query = "SELECT g FROM GroupU g WHERE g.name = :name")})
public class GroupU implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID")
    private Integer id;
    @Size(max = 50)
    @Column(name = "Name")
    private String name;
    @JoinTable(name = "person_group", joinColumns = {
        @JoinColumn(name = "GroupID", referencedColumnName = "ID")}, inverseJoinColumns = {
        @JoinColumn(name = "PerID", referencedColumnName = "ID")})
    @ManyToMany
    private Collection<People> peopleCollection;

    public GroupU() {
    }

    public GroupU(Integer id) {
        this.id = id;
    }

    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
    @JsonIgnore
    public Collection<People> getPeopleCollection() {
        return peopleCollection;
    }

    public void setPeopleCollection(Collection<People> peopleCollection) {
        this.peopleCollection = peopleCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof GroupU)) {
            return false;
        }
        GroupU other = (GroupU) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Entities.GroupU[ id=" + id + " ]";
    }

}

当我打电话给selectedGroup.getPeopleCollection(); 时,我总是得到空值, 注意:我使用 netbeans 来生成我的实体类。

【问题讨论】:

  • 向我们展示您使用的代码。 selectedGroup 来自哪里?
  • 因为,selectedGroup 为空。你是如何构建 selectedGroup 的?
  • @AnkitJain:如果 selectedGroup 为 null,他会得到 NullPointerException。请在您的 cmets 中使用真正的英语单词。
  • GroupU selectedGroup = getGroupModel().getRowData();
  • 我使用了使用数据表的组列表,这是选定的组

标签: java netbeans


【解决方案1】:
    @XmlTransient
    @JsonIgnore
    public Collection<People> getPeopleCollection() {
        return peopleCollection;
    }

我从上面的代码中去掉了这两行,问题已经解决了。

  @XmlTransient
  @JsonIgnore

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多