Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
XML
SQLQuery query = session.createSQLQuery("select id,name from  College");
       query.addEntity(College.class);

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

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
Posted
Updated 4-Feb-15 0:22am
v3

1 solution

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
Java
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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900