Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: puzzled! help me
PostPosted: Thu Aug 28, 2003 11:24 pm 
Newbie

Joined: Thu Aug 28, 2003 11:19 pm
Posts: 17
three objects : tracker , topic ,message

relations of tracker and topic is one-to-many
relations of topic and message is one-to-many

now i need add a topic to tracker ,and at the same time add a message to topic

Code:
tk =(Tracker)session.load(Tracker.class,trackerid);

Topic tp=new Topic();
tp.setTopicId(topicid);
tp.setTopicOwner(owner);
tp.setTitle(title);
tp.setTracker(tk);
tp.setState(-1);
tp.setCreatedBy("paul");
tp.setCreatedTime(new Date());
tp.setLastUpdate(new Date());

Message ms=new Message();
ms.setMessageId(new Integer(0));
ms.setCreatedTime(new Date());
ms.setCreatedBy("paul");
ms.setBody(message);
ms.setTopic(tp);

tp.getMessages().add(ms);
tk.getTopics().add(tp);

session.save(tp);



and run the code tp.getMessages().add(ms) throw a exception:java.lang.NullPointerException
at com.beaconsystem.catseye3.actions.SaveTopicAction.execute(SaveTopicAction.java:101)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:465)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1422)

how do i resolve it


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 8:17 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1812
Location: Austin, TX
Not nearly enough information here, but I'll give it a shot...

You are trying to access tp.getMessages(), where tp is a variable you just previously set to be = new Topic(). I'd be willing to bet that inside the Topic class you are not initializing the messages collection. Thus messages would be null and tp.getMessages() would return null. You can't call methods on a null reference...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2003 8:38 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If possible (this should be 99%), initialize your collections at declaration:

class Person {
String name;
Set addresses = new HashSet();
...
}

[/list]

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 1:01 am 
Newbie

Joined: Thu Aug 28, 2003 11:19 pm
Posts: 17
yes

tp.setMessage(new HashSet())
tp.getMessage().add(ms)

there is no error, but according to this way, only topic is inserted into database, message not!

why?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 31, 2003 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1812
Location: Austin, TX
Still not enough information here. How about posting your mappings, especially for how tracker, topic, and message relate to each other.

I would guess you have not appropriately set up cascading to acheive what you are looking for.


Top
 Profile  
 
 Post subject: Cascade setting and unsave-value
PostPosted: Mon Sep 01, 2003 10:17 pm 
Newbie

Joined: Tue Aug 26, 2003 10:17 pm
Posts: 5
Hi,

I guess you should check for your cascade setting in the 1-n mapping in your tracker and topic mapping. Hibernate will only save the message for you in cascade="all" or cascade="save-update" with appropriate unsaved-value attribute set to message.

Clarence


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 12:15 am 
Newbie

Joined: Thu Aug 28, 2003 11:19 pm
Posts: 17
ok i resolve it thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 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.