如何使用hibernate获取包含少量列数据的pojo对象 [英] how do I get a pojo object with few columns data using hibernate

查看:76
本文介绍了如何使用hibernate获取包含少量列数据的pojo对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似的pojo





@Entity

@Table(name =college ,catalog =practice)



公共类学院实现java.io.Serializable

{



private int id;

private String name;

private String caddress;

private String ccode;

}

和getter and setters



现在我想从这个查询得到这个pojo的对象

suppose i have a pojo like


@Entity
@Table(name = "college", catalog = "practice")

public class College implements java.io.Serializable
{

private int id;
private String name;
private String caddress;
private String ccode;
}
and getters and setters

Now i want an object of this pojo from this query

SQLQuery query = session.createSQLQuery("select id,name from  College");
       query.addEntity(College.class);

       List<College> employees = query.list();



但是这个查询会在列表中给出结果(因为我没有提供所有列)< Object []>但我想要一个具有提供列值的pojo对象


怎么做........

请回复

感谢提前


But this query will give the result in list(because i have not provided all columns)<Object[]> but i want a pojo object having the value of provided columns

How to do that........
please reply
thanks in Advance

推荐答案

我们可以采取另一种方法,首先我们将结果取回映射,然后通过使用转换为pojo杰克逊api例如

we can take an alternate approach for that first we will fetch the result in to map and then covert to pojo by using jackson api for example
SQLQuery query = session.createSQLQuery("select id,name from  College");
       query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
 List<map><string,>> list = query.list();

for (Map<string,> map : list)
        {

          
            ObjectMapper m = new ObjectMapper();
           
            College anotherBean = m.convertValue(map, College.class);
            colleges.add(anotherBean);

            System.out.println(anotherBean);

          
        }</map>


这篇关于如何使用hibernate获取包含少量列数据的pojo对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆