Try using < many-to-one > mapping instead of < one-to-one >. The foreign key column can be specified in < many-to-one >. e.g.,
< many-to-one column="id_left" name="left" class="beans.Nodes">< /many-to-one>
Question
I'm trying to insert hibernate in my project but i have a problem with the mapping. My db contains: - id ->int - question -> varchar - result -> varchar - id_left -> int - id_right -> int
here, the code of my 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 my 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 my display
public Nodes select (HttpServletRequest request) throws Dao_exception
{
List <Nodes> nodeList = new ArrayList<Nodes>();
nodeList = em.createQuery("SELECT n FROM Nodes n").getResultList();
return nodeList.get(0);
}
In fact, it would make 2 connections one-to-one I showed you earlier with my 2 items left / right of my bean.
Why? Because I want to build a binary tree of my game This is a result of a question, if I answer "yes", I left, otherwise right.
My problem is (in my opinion) the mapping of my class. Indeed, when I run the program, eclipse makes me the big eyes and leaves me this exception
Call: SELECT id, question, result, LEFT_id, RIGHT_id FROM node
Query: ReadAllQuery(referenceClass=Nodes sql="SELECT id, question, result, LEFT_id, RIGHT_id FROM node")
I understand that the application is malfunctioning because in my database I have no left / right_id. But how can I tell the program that id_left / right corresponds to 2 items left / right and therefore become my request.
Call: SELECT id, question, result, id_left, id_right FROM node
Solution
Try using < many-to-one > mapping instead of < one-to-one >. The foreign key column can be specified in < many-to-one >. e.g.,
< many-to-one column="id_left" name="left" class="beans.Nodes">< /many-to-one>