Hibernate's hql and native SQL return simple objects

Source: Internet
Author: User

Recently with a technical manager, he has a requirement that all queries are required to be objects than results, not maps, and that many objects are composed of many entities, all of which specialize in the knowledge of hibernate returning objects.

The object of this discussion is a combination of objects, which are associated with a number of entities, and take a part of their field,

Hql

Objects that are expanded first

 PackageCom.hfview.bean;ImportJava.util.ArrayList;ImportJava.util.List; Public  class Personex {    PrivateString ID;PrivateString name;PrivateString birthday;PrivateString area;PrivateList<menu> Menu =NewArraylist<menu> (); Public Personex(){    } Public Personex(string name, string area) {Super(); This. name = name; This. area = Area; } Public Personex(string ID, string name, string birthday) {Super(); This. id = ID; This. name = name; This. Birthday = Birthday; } PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } PublicStringGetbirthday() {returnBirthday } Public void Setbirthday(String birthday) { This. Birthday = Birthday; } PublicList<menu>GetMenu() {returnMenu } Public void SetMenu(list<menu> Menu) { This. menu = menu; } PublicStringGetarea() {returnArea } Public void Setarea(String area) { This. area = Area; }}

Entity Person1

 PackageCom.hfview.bean;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.OneToOne;Importjavax.persistence.Table;@Table(name ="T_person", schema="ZHW")@Entity Public  class Person1 {    PrivateString ID;PrivateString name;PrivateString sex;PrivateCard card;@Id    @Column(name="ID") PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; }@Column(name="NAME") PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; }@Column(name="SEX", columndefinition="VARCHAR2 (8)") PublicStringGetsex() {returnSex } Public void Setsex(String Sex) { This. sex = sex; }@OneToOne    @JoinColumn(name="CARDID") PublicCardGetcard() {returnCard } Public void Setcard(card) { This. card = Card; }}

Entity Card

 PackageCom.hfview.bean;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.Id;Importjavax.persistence.Table;@Table(name ="T_card", schema="ZHW")@Entity Public  class Card {    PrivateString ID;PrivateString cardId;PrivateString area;@Id    @Column(name="ID") PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; }@Column(name="CARDID") PublicStringGetcardid() {returnCardId; } Public void Setcardid(String cardId) { This. cardId = CardId; }@Column(name="Area") PublicStringGetarea() {returnArea } Public void Setarea(String area) { This. area = Area; }}

Person1 and card are a pair of relationships, Personex is the two extension objects

publicgetListPersonEx1(){        String hql=" select new com.hfview.bean.PersonEx(a.name,b.area) from Person1 a left join a.card b ";        return getHt().find(hql);    }

In this way, the collection of Personex is dynamically taken.
Here are a few things to note
1, Personex must have a corresponding construction method, and SQL inside the parameter sequence and the order of the constructor within the same
2, Personex can be no mapping relationship, can also be an entity

Native SQL

Class Person2

 PackageCom.hfview.bean;ImportJava.util.ArrayList;ImportJava.util.Date;ImportJava.util.List;ImportJavax.persistence.Column;Importjavax.persistence.Entity;ImportJavax.persistence.Id;ImportJavax.persistence.JoinColumn;ImportJavax.persistence.OneToMany;Importjavax.persistence.Table;@Table(name ="T_person", schema ="ZHW")@Entity Public  class Person2 {    PrivateString ID;Private intAgePrivateDate birthday;PrivateList<menu> Menu =NewArraylist<menu> ();@Id    @Column(name="ID") PublicStringgetId() {returnId } Public void setId(String ID) { This. id = ID; }@Column(name="Age") Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; }@Column(name="BIRTHDAY") PublicDateGetbirthday() {returnBirthday } Public void Setbirthday(Date birthday) { This. Birthday = Birthday; }@OneToMany    @JoinColumn(name="PERSONID") PublicList<menu>GetMenu() {returnMenu } Public void SetMenu(list<menu> Menu) { This. menu = menu; }}

DAO Layer Method

public List<PersonEx> getListPersonEx2(){        String sql="select a.id as \"id\",a.name as \"name\",to_char(a.birthday,‘yyyy-mm-dd‘) as \"birthday\"  " +                "from t_person a  ";        return getForListBean(sql, PersonEx.class);    }

Getforlistbean method

 Public <T> List<T>Getforlistbean (finalStringSql,finalMap<String, Object>paras,final int page,final introws, Final Class<T>Clazz) {returnHt.Executefind (NewHibernatecallback<List<HashMap<String, Object>>>() { Public List<HashMap<String, Object>>Doinhibernate (Session session) throws Hibernateexception, SQLException {sqlquery query=Session.Createsqlquery (SQL);if(paras!=NULL) {for (StringKey:p Aras.KeySet ()) {query.Setparameter (Key, paras.Get (key)); }                }if(page!=0&&rows!=0) {int start=Page*rows-rows+1; Query.Setfirstresult (Start).Setmaxresults (rows); } query.Setresulttransformer (Transformers.Aliastobean (Clazz));returnQuery.List();    }        }); }

here are some points to note
1, the constructor must be and SQL inside the corresponding
2, the default in Oracle to turn the field into uppercase, so that the set method will be wrong, so to be converted to as "AA" this
3, for the Personex if there is a set, how the dynamic setting in, at present I have not found a way

Hibernate's hql and native SQL return simple objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.