首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Hibernate持久化类配置有关问题

2012-01-26 
Hibernate持久化类配置问题下面这个类是一个树结构的节点,我想用Hibernate的OneToMany来实现树的结构,报错

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;@Entitypublic 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(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:263)
  org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)


  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:133)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:207)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:142)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:166)
  com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:190)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
  com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
  org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)


  org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:485)
  org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
  org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
  org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
  org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
  org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
  org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
  org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
  org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
  org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
  org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
  org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
  org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
  java.lang.Thread.run(Thread.java:619)
   



[解决办法]
Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: model.Node.parentnode

以上错误信息,找不到model.Node.parentnode
onetomany至少要有两个对象进行连接才行,一个对象和自己连接估计就会报这个错误,树不需要做onetomany,只需要private List<Node> childnodes;就可以了,一个节点包含所有子节点

热点排行