开发者

Hibernate noob - retrieve partial object

Greetings!

I have the following class and at times need to retrieve only title and subTitle eleme开发者_JAVA百科nts:

public class Article implements Serializable {

    private static final long serialVersionUID;
    private int id;
    private String title;
    private String subTitle;
    private String body;
    ....
}

What's the best way to facilitate this? I thought about making title and subTitle stand alone objects but I honestly don't know what I'm doing yet.

I have the luxury of changing the entire composition of Article, but asking first is the most sensible thing for me at the moment.

Thanks in advance.


You can annotate every simple property with LAZY -> just like this @Basic(fetch = FetchType.LAZY)

So, it will be loaded only if you need


Provided this is for displaying purpose, you could also rely on a ResultTransformer.

See this SO answer which deals with similar problem and limits the retrieved properties.


I think you can solve this by creating a specific class to hold the information you want, then you import it in your mapping. See 5.1.19.import in the manual. I believe you can use the imported class in your HQL, like this:

"select new Titles(a.title, a.subTitle) from Article a where a.id = :id"

Here's a blog post I found on this subject: http://gustavoringel.blogspot.com/2009/02/creating-dto-in-nhibernate-hql-using.html.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜