hibernate查询返回DTO对象,DTO封装了多个pojo对象的属性
DTO-数据传输对象;pojo-最纯粹的java对象与数据库中表的字段一一对应。
简单讲:DTO起到业务数据的传递作用,数据转换成JAVA对象。
1.查询语句
publicvoid testSqlOneToMany(){
String hql="select f.id, f.machinenum,f.types_id,t.id as typeId,t.name from gjp_fault f,gjp_type t where f.types_id = t.id";
SQLQuery query = getSession().createSQLQuery(hql);
List<ManyToVo> list = query.setResultTransformer(Transformers.aliasToBean(ManyToVo.class)).list();
System.out.println(list.size());
}
2.数据封装成对象(DTO)
package bh.test.vo;
import java.math.BigDecimal;
publicclass ManyToVo {
private BigDecimal ID;
private String MACHINENUM; //machinenum;
private BigDecimal TYPES_ID;
private BigDecimal TYPEID;
private String NAME;
public String getNAME() {
returnNAME;
}
publicvoid setNAME(String name) {
NAME = name;
}
public BigDecimal getID() {
returnID;
}
publicvoid setID(BigDecimal id) {
ID = id;
}
public BigDecimal getTYPEID() {
returnTYPEID;
}
publicvoid setTYPEID(BigDecimal typeid) {
TYPEID = typeid;
}
public BigDecimal getTYPES_ID() {
returnTYPES_ID;
}
publicvoid setTYPES_ID(BigDecimal types_id) {
TYPES_ID = types_id;
}
public String getMACHINENUM() {
returnMACHINENUM;
}
publicvoid setMACHINENUM(String machinenum) {
MACHINENUM = machinenum;
}
}
3.数据表结构
createtable GJP_FAULT
(
ID NUMBER(10) notnull,
DATETIME TIMESTAMP(6),
MACHINENUM VARCHAR2(255CHAR),
TYPES_ID NUMBER(10)
)
createtable GJP_TYPE
(
ID NUMBER(10) notnull,
NAMEVARCHAR2(255CHAR)
)