日期:2014-05-17  浏览次数:20691 次

Hibernate持久化类配置问题
下面这个类是一个树结构的节点,我想用Hibernate的OneToMany来实现树的结构,报错信息在后面,实在弄不明白了,各位帮帮忙...

Java code

package model;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.GenericGenerator;

import com.sun.istack.internal.NotNull;


@Entity
public class Node {
    private int nodeid;//主键
    private String nodename;//节点名
    private int nodelevel;//节点层级
//    private int nodeSerialNo;//节点序号
    private Node parentnode;//父节点
    private Set<Node> childnodes;//子节点
    public Node(){
        
    }
    @Id
    @GeneratedValue()
    @GenericGenerator(name="EAM",strategy="native")
    public int getNodeid() {
        return nodeid;
    }
    public void setNodeid(int nodeid) {
        this.nodeid = nodeid;
    }
    @NotNull
    public String getNodename() {
        return nodename;
    }
    public void setNodename(String nodename) {
        this.nodename = nodename;
    }
    @NotNull
    public int getNodelevel() {
        return nodelevel;
    }
    public void setNodelevel(int nodelevel) {
        this.nodelevel = nodelevel;
    }
    @OneToMany(mappedBy="parentnode",fetch=FetchType.EAGER)
    public Node getParentnode() {
        return parentnode;
    }
    public void setParentnode(Node parentnode) {
        this.parentnode = parentnode;
    }
    @ManyToOne
    public Set<Node> getChildnodes() {
        return childnodes;
    }
    public void setChildnodes(Set<Node> childnodes) {
        this.childnodes = childnodes;
    }
}


报错信息:
org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: model.Node.parentnode

  org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:324)
  org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1723)
  org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:796)
  org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:707)
  org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:4035)
  org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3989)
  org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1398)
  org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
  controller.TheStructure.enter(TheStructure.java:51)
  sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
  java.lang.reflect.Method.invoke(Method.java:597)
  com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:452)
  com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:291)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:254)
  com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocat