Jpa Playframework如何将query.ResultList转换为VO

Jpa Playframework如何将query.ResultList转换为VO,jpa,playframework,transform,Jpa,Playframework,Transform,Playframework的版本是1.2.x,我想将query.ResultList转换为VO 我创建了一个零件实体bean,如下所示: @Entity @Table(name="evaluation_part") public class Part extends Model { public String name; public String collegeName; public int peopleNum; } 数据: id name

Playframework的版本是1.2.x,我想将query.ResultList转换为VO

我创建了一个零件实体bean,如下所示:

@Entity
@Table(name="evaluation_part")
public class Part extends Model {

    public String name;  

    public String collegeName;

    public int peopleNum;
}
数据:

id     name     collegeName      peopleNum

1      Jsj1     JJJJ              32
2      Jsj2     JJJJ              23
3      Jsj3     JJJJ              32
4      Tjb1     TTTT              11
5      Tjb2     TTTT              14
6      Tjb3     TTTT              16
我的值对象类:

public class PartVO {

    public String collegeName;

    public int peopleNum;

}
我想使用本机查询来获得结果:

String sql="select collegeName,SUM(peopleNum) as peopleNum from evaluation_part group by collegeName";
查询结果为:

      collegeName      peopleNum

        TTTT              41
        JJJJ              87
我试过:

String sql="select collegeName,SUM(peopleNum) as peopleNum from evaluation_part group by collegeName";

Query query =JPA.em().createNativeQuery(sql);

List<PartVO> partVOs = query.getResultList();
for(int i=0;i<partVOs.size();i++) {      
    System.out.println(partVOs.get(i).collegeName);
}

您不必使用原始sql来实现这一点。使用hql,您可以使用新操作符创建VO,请参见

必须在partVO类中定义一个双参数构造函数,然后才能执行以下操作

select new package.PartVO(collegeName, SUM(peopleNum)) from Part group by collegeName

您可以使用createNativeQuery的版本。。。方法,该方法还接受结果实例的类作为参数:

,java.lang.Class


但是,请确保这确实有效,因为Play Framework在其API实现中没有实现JPA的所有功能。

解决方案1:仅使用HQL中Part类中定义的“select new Part”构造函数,您可以将对象转换为Part。Hibernate使用反射自动注入您需要的所有字段

解决方案2:这里返回的结果类型必须是Object[],这样就可以通过数组的索引得到从数据库中取出的记录的每个字段

solution1和solution2之间的区别:前一个在查询中使用构造函数,后一个将记录转换为对象[]

在您的情况下,忽略实体之间的复杂关系,上面的解决方案会起作用

此处引用的代码:

package controllers;

import play.*;
import play.db.jpa.JPA;
import play.mvc.*;
import java.util.*;

import models.*;

/**
* This demo is intended for fetching data from MYSQL.
* @author dhl@oopsplay.org
*/

public class Application extends Controller {

public static void index() {
    render();
}

/**
 * Prepare some data to test.
 */
public static void addPart() {

    //Add a part record to database.
    Part newPart=new Part("software","zjut",8).save();
    if(newPart.isPersistent()){
        renderText("Add successfully,there are %s records in the \'evaluation_part\' table.For convenience,please click the back button in the browser to go back previous page.",Part.count());
    }
}

/**
 * Fetch part entities from database;
 */
public static void fetchPart() {

    //-------------------Solution 1-------------------
    //[Pay attention]:Only use 'select new Part()'(constructor defined in the Part class) in the query that u can  convert object to Part.
    //Hibernate use reflection to automatically inject all the fields u need.
    List<Part> parts1=JPA.em().createQuery("select new Part(name,collegeName,peopleNum) from Part").getResultList();

    //For convenience, i output the detail in the console, focus on the change there.
    Logger.info("The name of first record is :%s", parts1.get(0).name);

    //-------------------Solution 2-------------------
    //[Pay attention]:Here the returned type of result must be Object[],so that u can got every field of the record fetched from database;
    List<Object[]> parts2=JPA.em().createNativeQuery("select name,collegeName,peopleNum from evaluation_part").getResultList();
    Logger.info("The name of first record is :%s", parts2.get(0)[0]);

    for(int i=0;i<parts2.size();i++){

        //The difference between solution1 and solution2:the previous use constructor in the query and the later transform a record into Object[].
        Logger.info("Name from parts1 is: %s", parts1.get(i).name);
        Logger.info("Name from parts2 is: %s", parts2.get(i)[0]);
    }

    renderText("There are %s record in the \'evaluation_part\' table",parts2.size());
}

}

我不认为您可以将HQL Hibernate查询语言与使用JPA的Play Framework一起使用。当然可以,Play 1.2.x在底层使用Hibernate。我在整个应用程序中都使用了这种技术,没有任何问题
package controllers;

import play.*;
import play.db.jpa.JPA;
import play.mvc.*;
import java.util.*;

import models.*;

/**
* This demo is intended for fetching data from MYSQL.
* @author dhl@oopsplay.org
*/

public class Application extends Controller {

public static void index() {
    render();
}

/**
 * Prepare some data to test.
 */
public static void addPart() {

    //Add a part record to database.
    Part newPart=new Part("software","zjut",8).save();
    if(newPart.isPersistent()){
        renderText("Add successfully,there are %s records in the \'evaluation_part\' table.For convenience,please click the back button in the browser to go back previous page.",Part.count());
    }
}

/**
 * Fetch part entities from database;
 */
public static void fetchPart() {

    //-------------------Solution 1-------------------
    //[Pay attention]:Only use 'select new Part()'(constructor defined in the Part class) in the query that u can  convert object to Part.
    //Hibernate use reflection to automatically inject all the fields u need.
    List<Part> parts1=JPA.em().createQuery("select new Part(name,collegeName,peopleNum) from Part").getResultList();

    //For convenience, i output the detail in the console, focus on the change there.
    Logger.info("The name of first record is :%s", parts1.get(0).name);

    //-------------------Solution 2-------------------
    //[Pay attention]:Here the returned type of result must be Object[],so that u can got every field of the record fetched from database;
    List<Object[]> parts2=JPA.em().createNativeQuery("select name,collegeName,peopleNum from evaluation_part").getResultList();
    Logger.info("The name of first record is :%s", parts2.get(0)[0]);

    for(int i=0;i<parts2.size();i++){

        //The difference between solution1 and solution2:the previous use constructor in the query and the later transform a record into Object[].
        Logger.info("Name from parts1 is: %s", parts1.get(i).name);
        Logger.info("Name from parts2 is: %s", parts2.get(i)[0]);
    }

    renderText("There are %s record in the \'evaluation_part\' table",parts2.size());
}