-->
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.  [ 4 posts ] 
Author Message
 Post subject: loading problem
PostPosted: Mon Feb 16, 2004 1:09 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:27 am
Posts: 33
(parent and child one to one relation)

Parent pa=new Parent();
pa.setName("laojiang");
Child ca=new Child();
ca.setName("xiaojiang");
pa.setMychild(ca);
String pid=pa.getId();

Session sess1=sessions.openSession();
tx=sess1.beginTransaction();
sess1.save(pa);
sess1.flush();
tx.commit();
sess1.close();

Session sess2=sessions.openSession();
tx=sess2.beginTransaction();
! here---> Parent pb=(Parent)sess2.load(com.dnt.test.Parent.class,pid);
if(pb==null){System.out.println("load return null");}
else{System.out.println("parent:"+pb.getName()
+"and his child:"+pb.getMychild().getName());
}
tx.commit();
sess2.close();

problem:

Hibernate: insert into CHILDS (NAME, id) values (?, ?)
Hibernate: insert into PARENTS (NAME, child_id, id) values (?, ?, ?)
Exception in thread "main" java.lang.NullPointerException: null is not a valid i
dentifier
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1883)
at com.dnt.test.Test.main(Test.java:36)

anyone kown what is wrong with that?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 3:29 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Quote:
String pid=pa.getId();

At the time that this statement executes, pa is transient and has a null id.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 4:30 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 6:27 am
Posts: 33
so that means it's not usually that case that we load some persisitent entity by it's primary key ,cause primary key generated by the generator don't have real meaning ,instead most ofter find it not load it;
so load() method is not that usefull , is that right?????
in what situation the load() can be made grate use of ???


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 9:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Execute the getId() after you have saved the object.


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

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.