gudii9
asked on
Hibernate HQL Map
Hi,
I am trying to retrieve object as list using HQL as below.
Problem I am facing is I am not able to retrieve user object as map. Please advies
MapHQL1.jpg
MapHQL2.jpg
I am trying to retrieve object as list using HQL as below.
hibernate config looks as below
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_cl ass">org.p ostgresql. Driver</pr operty>
<property name="hibernate.connection.url">jdbc :postgresq l://localh ost:5432/h ibernatedb </property >
<property name="hibernate.connection.username" >postgres< /property>
<property name="hibernate.connection.password" >root</pro perty>
<property name="hibersnate.connection.pool_siz e">10</pro perty>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect .PostgreSQ LDialect</ property>
<property name="hibernate.hbm2ddl.auto">update </property >
<mapping class="org.gp.gpr.dto.UserDetails"/>
<mapping class="org.gp.gpr.dto.Vehicle"/>
<mapping class="org.gp.gpr.dto.TwoWheeler"/>
<mapping class="org.gp.gpr.dto.FourWheeler"/>
</session-factory>
</hibernate-configuration>
hibernate test hql java looks as below
package org.gp.gpr.hibernate;
import java.util.Date;
import java.util.List;
import org.gp.gpr.dto.Address;
import org.gp.gpr.dto.FourWheeler;
import org.gp.gpr.dto.TwoWheeler;
import org.gp.gpr.dto.UserDetails;
import org.gp.gpr.dto.Vehicle;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTestHQL {
public static void main(String[] args){
SessionFactory sessionFactory = new Configuration().configure().buildSes sionFactor y();
Session session=sessionFactory.openSession() ;
session.beginTransaction();
//Query query=session.createQuery("select userName from UserDetails");
Query query=session.createQuery("select new map(userId,userName) from UserDetails");
//Query query=session.createQuery("from UserDetails where userid>0");
// List user=query.list();
query.setFirstResult(5);
query.setMaxResults(4);
//List<UserDetails> user=(List<UserDetails>)query.list() ;
List<String> userNames=(List<String>)query.list() ;
session.getTransaction().commit();
session.close();
// System.out.println("size of list"+user.size());
for(String u: userNames)
System.out.println(u);
}
}
UserDetails.java looks as below
package org.gp.gpr.dto;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides ;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.Collection Id;
import org.hibernate.annotations.GenericGen erator;
import org.hibernate.annotations.Type;
//import org.hibernate.annotations.Entity;
@Entity
@org.hibernate.annotations.Entity(se lectBefore Update=tru e)
public class UserDetails {
@Id @GeneratedValue(strategy=GenerationT ype.AUTO)
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName+"from get";
}
public void setUserName(String userName) {
this.userName = userName;
}
}
Problem I am facing is I am not able to retrieve user object as map. Please advies
MapHQL1.jpg
MapHQL2.jpg
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
http://stackoverflow.com/questions/9291343/how-to-get-jpahibernate-query-resultlist-as-hashmap