【发布时间】: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();
-
我使用了使用数据表的组列表,这是选定的组