Some time you often face a situation where a ArrayList contains unknown objects, which can not be easily typecast to its class
Handling Unknown Objects in ArrayList
Some time you often face a situation where a ArrayList contains unknown objects, which can not be easily typecast to its class. Such type of situation is commonly found when we write HQL (Hibernate Query Language) in hibernate.
Query query = session.createQuery("select s.rollNo, s.name, s.address from Student s");
This query will return only the objects specified in select. To Handle such type of query you may do two things. You can either transform your query result to Map and get data as follows.
String sql = "select s.rollNo, s.name, s.address from Student s"; Query query = session.createQuery(sql); query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); List list = query.list(); Iterator iterator = list.iterator(); while(iterator.hasNext()){ Map map=(Map)iterator.next(); System.out.println(map.get("0")); System.out.println(map.get("1")); System.out.println(map.get("2")); }
Or you can get object as
String sql = "select s.rollNo, s.name, s.address from Student s"; Query query = session.createQuery(sql); List list = query.list(); Iterator iterator = list.iterator(); for (Iterator it = query.iterate(); it.hasNext();) { Object[] object = (Object[]) it.next(); System.out.println(object[0]); System.out.println(object[1]); System.out.println(object[2]); }
The full code of above example is given below
Student.java
package net.roseindia.domain; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "student") public class Student { @Id @GeneratedValue @Column(name = "roll_no") private int rollNo; @Column(name = "name") private String name; @Column(name = "course") private String course; @Column(name = "address") private String address; public Student() { } public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Hibernate Connection Util class
HibernateConnectionFactory.java
package net.roseindia.connection; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateConnectionFactory { private static ServiceRegistry serviceRegistry; private static SessionFactory sessionFactory; static { try { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable e) { e.printStackTrace(); } } public SessionFactory getSessionFactory() { return sessionFactory; } }
Hibernate Configuration File
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/data </property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.current_session_context_class">thread</property> <property name="hbm2ddl.auto">create</property> <mapping class="net.roseindia.domain.Student" /> </session-factory> </hibernate-configuration>
Finally Application Class
MyApplication.java
package net.roseindia.application; import java.util.Iterator; import java.util.List; import java.util.Map; import net.roseindia.connection.HibernateConnectionFactory; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; public abstract class MyApplication { public static void main(String[] args) { Session session = null; try { HibernateConnectionFactory connectionFactory = new HibernateConnectionFactory(); session = connectionFactory.getSessionFactory().openSession(); String sql = "select s.roll_no, s.name, s.address from student s"; Query query = session.createSQLQuery(sql); query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP); List list = query.list(); Iterator iterator = list.iterator(); while (iterator.hasNext()) { Map map = (Map) iterator.next(); System.out.println(map.get("roll_no")); System.out.println(map.get("name")); System.out.println(map.get("address")); } } catch (Exception e) { e.getMessage(); e.printStackTrace(); } } }
When you run this application it will display message as shown below:
1 Raj Delhi |
[ 0 ] Comments