Hibernate的HQL和原生SQL返回简单对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chuxue1989/article/details/51346582

最近跟了一个技术经理,他有个要求就是所有的查询比结果都必需是对象,不是是map之类的,而且有很多对象的是很多实体相联合组成的对象,所有又专门研究了hibernate返回对象的知识

这次讨论的对象都是组合对象,就是很多实体相关联,取他们一部分字段的组合对象,

Hql

首先扩展的对象

package com.hfview.bean;

import java.util.ArrayList;
import java.util.List;

public class PersonEx {

    private String id;
    private String name;
    private String birthday;
    private String area;
    private List<Menu> menu = new ArrayList<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;
    }


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public List<Menu> getMenu() {
        return menu;
    }

    public void setMenu(List<Menu> menu) {
        this.menu = menu;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }
}

实体Person1

package com.hfview.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Table(name = "t_person",schema="zhw")
@Entity
public class Person1 {

    private String id;
    private String name;
    private String sex;
    private Card card;

    @Id
    @Column(name="ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name="NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="SEX",columnDefinition="varchar2(8)")
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @OneToOne
    @JoinColumn(name="CARDID")
    public Card getCard() {
        return card;
    }

    public void setCard(Card card) {
        this.card = card;
    }


}

实体Card

package com.hfview.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "t_card",schema="zhw")
@Entity
public class Card {

    private String id;
    private String cardId;
    private String area;

    @Id
    @Column(name="ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name="CARDID")
    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    @Column(name="AREA")
    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

}

Person1和card是一对一关系,PersonEx是这两个扩展对象

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

这样就动态的取到了PersonEx的集合。
这里需要注意几点
1、PersonEx里面一定要有对应的构造方法,并且sql里面参数顺序和构造器里面的顺序一致
2、PersonEx可以是没有映射关系的,也可以是实体

原生SQL

类 Person2

package com.hfview.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Table(name = "t_person", schema = "zhw")
@Entity
public class Person2 {
    private String id;
    private int age;
    private Date birthday;
    private List<Menu> menu = new ArrayList<Menu>();

    @Id
    @Column(name="ID")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Column(name="AGE")
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Column(name="BIRTHDAY")
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @OneToMany
    @JoinColumn(name="PERSONID")
    public List<Menu> getMenu() {
        return menu;
    }

    public void setMenu(List<Menu> menu) {
        this.menu = menu;
    }


}

Dao层方法

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方法

public <T> List<T> getForListBean(final String sql,final Map<String,Object> paras,final int page,final int rows,final Class<T> clazz){
        return ht.executeFind(new HibernateCallback<List<HashMap<String,Object>>>() {

            public List<HashMap<String, Object>> doInHibernate(
                    Session session) throws HibernateException, SQLException {
                SQLQuery query = session.createSQLQuery(sql);

                if(paras!=null){
                    for (String key :paras.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));
                return query.list();
            }
        });
    }

这里需要注意的几点
1、构造函数一定要和sql里面的对应
2、oracle中默认把字段都转成大写,这样set方法就会出错,所以要转变成 as “aa” 这种
3、对于PersonEx如果有集合,怎么动态的设置进去,目前我还没找到方法

阅读更多
换一批

没有更多推荐了,返回首页