-->
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: Persistence by reachability causes primary key un-populated
PostPosted: Tue May 22, 2007 2:05 pm 
Newbie

Joined: Tue May 22, 2007 12:57 pm
Posts: 1
I'm using Spring + Hibernate. I'm writing a test for Blog and BlogEntry class. A Blog has a List of BlogEntry object. The mapping of the classes are shown below.

Blog blog = new Blog();
getHibernateTemplate().saveOrUpdate(blog);
// persisted the blog... verified it by checking the Blog table... ID=12

BlogEntry blogEntry = new BlogEntry();
blog.addBlogEntry(blogEntry);
getHibernateTemplate().saveOrUpdate(blog);
// persisted the blogEntry... verified by checking the BlogEntry table... ID=45

Blog retrievedBlog = getHibernateTemplate().get(Blog.class, 12);
BlogEntry retrievedBlogEntry = retrievedBlog.getBlogEntries().get(0);
System.out.println(retrievedBlogEntry.getBlogEntryId());
// getBlogEntryId returns -1 ... which is wrongly populated
// other properties such as Title, and Content is correctly populated

BlogEntry retrievedBlogEntry2 = getHibernateTemplate().get(BlogEntry.class, 45);
System.out.println(retrievedBlogEntry2.getBlogEntryId());
// getBlogEntryId() return 45 ... which is right


So what is wrong here? Why does the blogEntryId not populated from the Database when i access it through the Blog.blogEntries list... but blogEntryId is correctly populated when i directly use the get(class, id) method?

please advice...

Hibernate version:
3.2

Mapping documents:
The mapping for Blog class
<id name="blogId" column="BLOG_ID" type="long" unsaved-value="-1">
<generator class="native"></generator>
</id>
<list name="blogEntries" cascade="all" >
<key>
<column name="BLOG_ID" ></column>
</key>
<list-index column="BLOG_ENTRY_POSITION"></list-index>
<one-to-many class="BlogEntry" />
</list>

The mapping for BlogEntry class
<id name="blogEntryId" column="BLOG_ENTRY_ID" type="long" unsaved-value="-1" >
<generator class="native"></generator>
</id>
<property name="title" column="BLOG_ENTRY_TITLE"/>
<property name="content" column="BLOG_ENTRY_CONTENT"/>

Classes:
public class Blog {
private long blogId = -1;
private List<BlogEntry> blogEntries;

//getters and setters here...
}

public class BlogEntry {
private long blogEntryId = -1;
private String title;
private String content;

// getters and setters here..
}


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.