【发布时间】:2011-07-01 12:50:01
【问题描述】:
我的代码如下所示。为什么我在setChildren() 中的名字是null??
public class Node {
private String id, name, parentName;
private Set<Node> children = new HashSet<Node>();
private Node parent;
private Set<String> childrenNames = new HashSet<String>();
@Id @GeneratedValue public String getId() { return id; }
public String getName() {return name;}
@ManyToOne @JoinColumn(name="PARENT_ID") public Node getParent(){retrun parent;}
@OneToMany(cascade=CascadeType.ALL, mappedBy="parent", fetch=FetchType.EAGER)
public Set<Client> getChildren(){return Collections.unmodifiableSet(children)}
@Transient public String getParentName() { return parentName;}
@Transient public Set<String> getChildrenNames() {return childrenNames;}
// PROBLEM here ------------
public void setChildren(Set<Node> children) {
this.children = children;
for(Node child : children) {
childrenNames.add(child.getName()); //Adds NULL !!!!!!
}
}
// Other Setters
}
我是 n00b,所以请帮忙。
【问题讨论】:
-
如果您调试此代码并在 setChildren(...) 中设置断点,是否所有子节点对象上的字段(可能除了 @Id)都设置为 null?
-
正确。除
@Id之外的所有字段均为null。请帮忙 -
请参阅stackoverflow.com/questions/6217096/…,了解与集合类似的问题。