java - Mapping Object Hibernate -


i'm trying insert hibernate in project have problem mapping. db contains: - id ->int - question -> varchar - result -> varchar - id_left -> int - id_right -> int

here, code of bean

@entity @table (name = "node") public class nodes {     @id     @generatedvalue (strategy = generationtype.identity)     @column(name="id")     private int id;      @column (name = "question")     private string question;      @column ( name = "result")     private string result;      @onetoone(fetch= fetchtype.lazy)     private nodes left;      @onetoone(fetch= fetchtype.lazy)     private nodes right;         public nodes(int id, string question, string result, nodes left, nodes right)     {            this.id=id;            this.question=question;            this.result=result;            this.left=left;            this.right=right;     }      public nodes()     {      }          public nodes getleftnodes()     {         return left;     }      public void setleftnodes(nodes left)     {         this.left=left;     }      public nodes getrightnodes()     {         return right;     }      public void setrifhtnodes(nodes right)     {         this.right=right;     } 

and here nodes.hbm.xml

<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>     <class name="beans.nodes" table="node">         <id name="id" type="int" access="field">             <column name="id" />             <generator class="assigned" />         </id>         <property name="question" type="java.lang.string">             <column name="question" />         </property>         <property name="result" type="java.lang.string">             <column name="result" />         </property>         <one-to-one name="left" class="beans.nodes" access="field"></one-to-one>         <one-to-one name="right" class="beans.nodes" access="field"></one-to-one>     </class> </hibernate-mapping> 

and display

public nodes select (httpservletrequest request) throws dao_exception     {         list <nodes> nodelist = new arraylist<nodes>();         nodelist = em.createquery("select n nodes n").getresultlist();          return nodelist.get(0);     } 

in fact, make 2 connections one-to-one showed earlier 2 items left / right of bean.

why? because want build binary tree of game result of question, if answer "yes", left, otherwise right.

my problem (in opinion) mapping of class. indeed, when run program, eclipse makes me big eyes , leaves me exception

call: select id, question, result, left_id, right_id node query: readallquery(referenceclass=nodes sql="select id, question, result, left_id, right_id node") 

i understand application malfunctioning because in database have no left / right_id. how can tell program id_left / right corresponds 2 items left / right , therefore become request.

call: select id, question, result, id_left, id_right node 

try using < many-to-one > mapping instead of < one-to-one >. foreign key column can specified in < many-to-one >. e.g.,

< many-to-one column="id_left" name="left" class="beans.nodes">< /many-to-one>


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -