The hibernate query returns DTO objects, which encapsulate the properties of multiple pojo objects

 

 

 

DTO - data transfer object; pojo - the purest java object has a one-to-one correspondence with the fields of the table in the database.

 

To put it simply: DTO plays a role in the transmission of business data, and the data is converted into JAVA objects.

 

 

 

1. Query statement

 

public void 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. Data is encapsulated into objects ( DTO )

 

package b.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. Data table structure

 

createtable GJP_FAULT

 

(

 

  IDNUMBER(10) notnull,        

 

  DATETIME   TIMESTAMP(6),

 

  MACHINENUM VARCHAR2(255CHAR),

 

  TYPES_ID   NUMBER(10)

 

)

 

 

 

createtable GJP_TYPE

 

(

 

  ID   NUMBER(10) notnull,

 

  NAMEVARCHAR2(255CHAR)

 

)

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326695356&siteId=291194637