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..
}
|