-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: insert id with foreign key
PostPosted: Wed Apr 30, 2014 9:11 am 
Newbie

Joined: Tue Apr 22, 2014 7:21 am
Posts: 2
Hi,

I have a db with five data and two foreign key into the same table:
- id (int, PK)
- question (varchar)
- result (varchar)
- left_id (int, FK)
- right_id(int, FK)

Here my nodes_bean
Code:
public class Nodes
{
   @Id
   @GeneratedValue (strategy = GenerationType.IDENTITY)
   @Column(name="id")
   @NotNull
   private int id;
   
   @Column (name = "question")
   private String question;
   
   @Column ( name = "result")
   private String result;
   
   @OneToOne(fetch= FetchType.LAZY)
   @JoinColumn(name="left_id", referencedColumnName="id")
   private Nodes left;
   
   @OneToOne(fetch= FetchType.LAZY)
   @JoinColumn(name="right_id", referencedColumnName="id")
   private Nodes right;   
}


without to adding left/right_id integer, my project works.

For example the first line is like this
id:1
question: "are you a man ?"
result: null
left_id: 2
right_id: 3

I can insert a new question and result, but i can't insert left or right_id !

Here my method for to insert new data
Code:
   public void insert_question(String question, int left ) throws HibernateException
   {
      try
      {
         Session session = getSession();
         Transaction tx = session.beginTransaction();
         
         Nodes node = new Nodes();
         
         node.setQuestion(question);
         node.setLeftNodes(node.getLeftNodes().getId_node());
         
         session.saveOrUpdate(node);
         tx.commit();
         session.close();
      }
      catch (HibernateException e)
      {
         e.printStackTrace();
      }
   }


The errors is on
Quote:
node.setLeftNodes(left.getLeftNodes().getId_node());

The message is : "Cannot invoke getLeftNodes() on the primitive type int".
This is normal beacause i did not left/right_id integer .

My question is : how i can left/right_id when there are a foreign key ?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.