-1

I have three tables ( People, Group, person_group ) and I want to get the people of specific group, so I used

selectedGroup.getPeopleCollection();

And this is my entity class:

  @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 + " ]";
    }

}

And when I call selectedGroup.getPeopleCollection(); I always get null, note: I used netbeans to generate my entity classes.

eightShirt
  • 1,457
  • 2
  • 15
  • 29

1 Answers1

0
    @XmlTransient
    @JsonIgnore
    public Collection<People> getPeopleCollection() {
        return peopleCollection;
    }

I removed these two lines from the above code and the problem has been solved.

  @XmlTransient
  @JsonIgnore