-->
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.  [ 5 posts ] 
Author Message
 Post subject: Need Help Mapping Latest Encapsulated Entity
PostPosted: Mon Sep 20, 2010 12:10 pm 
Newbie

Joined: Fri Jul 09, 2010 1:24 pm
Posts: 11
Hi all,

Here's is a situation I am encountering. Let's say I have a Blog entity with a field called latestPosting, which is of type Post. This latest posting field should be a unique entry where the latestPosting is the newest record in the database and I cannot use the date field on the Post. I would like to have Hibernate retrieve this field as part of a JPA or Hibernate annotation. I also need to have this work with lazy loading. In other words, I can't depend on a Hibernate @Filter that is enabled at the session level during retrieval. I found this solution online, but it requires a OneToMany mapping. I'd need a OneToOne. Also, take note that I also have mapped a collection of Posting elements in my Post entity.

http://venky-techspace.blogspot.com/201 ... ilter.html

So my questions are:

  • Is this achievable via an annotations only approach?
  • If not, should I use a read-write view?


I am using Hibernate 3.5.6, Oracle 10g, mixed JPA and Hibernate Annotations, and Java 6.

Here is a brief sample of what the Blog model look likes:

Code:
@Entity
@Table(name="BLOG")
public class Blog{

@Id
@Column(name="id")
private Integer id;

//??? what mapping can we use here
private Post latestPosting;

@OneToMany(mappedBy = "blog")
private List<Post> postings;

......

}


The Post model:

Code:
@Entity
@Table(name="POST")
public class Post{

@Id
@Column(name="id")
private Integer id;

@ManyToOne
@JoinColumn(name="blog_id")
private Blog blog;

......

}


Top
 Profile  
 
 Post subject: Re: Need Help Mapping Latest Encapsulated Entity
PostPosted: Wed Sep 22, 2010 1:46 pm 
Newbie

Joined: Fri Jul 09, 2010 1:24 pm
Posts: 11
bump...anyone have some pointers?


Top
 Profile  
 
 Post subject: Re: Need Help Mapping Latest Encapsulated Entity
PostPosted: Wed Sep 22, 2010 2:56 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
So go use the @OneToOne on Blog entity. What's the problem?

Code:
//??? what mapping can we use here
@OneToOne
@JoinColumn(name="post_id")
private Post latestPosting;


Now when you do that, you just have to update the post in blob entity whenever there is a latest post. A better way probably just use the fact you have a
Code:
List<Post> postings;
, go ahead and order it in whatever way you think it consider latest, then your latest Post IS simply just
Code:
postings.get(0);

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Need Help Mapping Latest Encapsulated Entity
PostPosted: Wed Sep 22, 2010 9:39 pm 
Newbie

Joined: Fri Jul 09, 2010 1:24 pm
Posts: 11
Quote:
So go use the @OneToOne on Blog entity. What's the problem? Now when you do that, you just have to update the post in blob (blog?) entity whenever there is a latest post.

The problem is that this is not an option. I provided this as a sample model. The actual model I am working with doesn't deal with Blogs and Posts. Sorry for the obfuscation, but I was attempting to keep it simple. Regardless, the real issue is that I need Hibernate to be aware of how this property is derived. If Hibernate is aware of it via a JPA/Hibernat annotation, then I can use that property in Criteria queries to order a collection of "Blogs" by their latest Posts. Also, I would be able to query all Blogs with a latest posting date of XYZ. Finally, I wouldn't have to load the entire collection to find the latest posting.

I suppose I could use JPQL to do all of this, but it would be an exception to my framework that I would like to avoid. Currently, the framework doesn't have any special use cases coded within it. It only deals with requests to sort and filter based on property names alone, which are provided to a Criteria for Hibernate to digest. With this in mind, I can't do:

Code:
postings.get(0);


I appreciate the input and welcome any other ideas.


****UPDATE****
It turns out that max(id) will not guarantee me the lastest posting. I actually have to retrieve it by a date field on the post (e.g. max(postDate) )


Top
 Profile  
 
 Post subject: Re: Need Help Mapping Latest Encapsulated Entity
PostPosted: Fri Sep 24, 2010 10:25 pm 
Newbie

Joined: Fri Jul 09, 2010 1:24 pm
Posts: 11
Any thoughts on this? It would seem Hibernate cannot handle my use case, but I am hoping a Hibernate guru can confirm or deny this.

Thanks!


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