-->
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.  [ 2 posts ] 
Author Message
 Post subject: List Collection has +1 size begining with null value
PostPosted: Mon May 14, 2007 8:43 am 
Newbie

Joined: Mon May 14, 2007 8:23 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.2.2

Mapping documents:
Code:
<hibernate-mapping ..>
   <list name="comments" table="comment" cascade="all>
      <key column="a_id" />
      <list-index column="id" />
      <one-to-many class="Comment" />
   </list>
</hibernate-mapping>


Hi, I'm newbie studying hibernate.
I'm trying to make a simple web application. There are articles with comments. As I think that one article should have multiple comments, I use list collection.

in my Database(Mysql 5) there are only two tables.
one is Article
another is Comment

field "a_id" of Comment is references of field "id" of Article

when my WAS responses to show article

Code:
public static Article getArticle(int id) {
    Session ses = HibernateUtil.currentSession();
    Article a = (Article)ses.get(Article.class, id);
    return a;
}


this code invokes setter of comments in Article class
Code:
public void setComments(List<Comment> comments) {
    this.comments = comment;
}


but I saw a NullPointerException, using Article.getComment();

so I wanna find why?

in the list(Article.comments), there should be 2 objects because there are 2 comments in the database!!

but it has 3 objects, and the first one is null; 2nd, 3rd are correct.

How can I correct this problem?
is it bug? or not?


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 14, 2007 5:03 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
By default list indices start with 0. If you want the index to start with 1, do <list-index column="id" base="1"/>.

However, I'm thinking that "id" is your primary key. That is not what a list mapping is for. A list mapping requires a column that solely represents a list index. I believe that you would actually be better of with a set mapping.

http://www.hibernate.org/hib_docs/v3/re ... ns-indexed


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.