how to add hibernate query results in to array list - java

my task was to populate prime faces data tables with data values from hibernate.we can fetch all the record in the table by this query
select from employee
will fetch all the records from the table employee ,employee field has two fields name,aage
the hibernate query for converting it to list was
List list =query.list();
i want to convert it in to array list having two fields name,age with all objects from hibernate
then populate the data table with
#{}
can any one explain with example

You can fetch employee table data in this way
Here hibernateSession is the reference variable of Session.
List employees=hibernateSession.createQuery("from Employee").list();
Than through while loop you can get your data
Employee employee;
int x=0;
while(x<employees.size())
{
employee=(Employee)employees.get(x);
String name=employee.getName());
int age=employee.getAge();
x++;
}
Hope this helps if you think that this solves your problem than mark the answer as solution

To put the data into an ArrayList, you can use this ArrayList constructor:
ArrayList list = new ArrayList(query.list());

Related

How to retrieve data from object in hibernate

I want to join two table:
T1:id,name,email
T2:id,address
to join I am doing this:
String hql="select sd.name,tt.name from T1 sd,T2 tt where sd.id=tt.id";
Query q=ss.createQuery(hql);
List l2=q.list();
Object o1=l2.get(0);
System.out.println("-----------");
System.out.println(o1.toString());
System.out.println("-----------");
It is returning object but dont get how to retrieve the values as returned object is not entity object.
Every result is an Object Array.The Array's size is the select columns
List l2=q.list();
Object[] o1=(Object[])l2.get(0);//The size is should be 2
System.out.println(o1[0]);
System.out.println(o1[1]);
You have to cast the Object's in the List to your specific type, Hibernate does not know the type. See What is the "proper" way to cast Hibernate Query.list() to List<Type>? and Hibernate query for selecting multiple values
it will return array of Object which you need to typecast as per your select values like name etc.
List<Object[]> values= (List<Object[]>)q.list();
for(Object[] val: values){
....
//your specific type values
}
Another option is creating a view with your joined tables, then, create hibernate entity base on that view

What is better for unique results : Set or Projection in Hibernate

I know two ways to get unique/distinct data list in hibernate. I can get unique/distinct data list using projection or convert data list to a set in hibernate criteria.
Set<Student> students = new HashSet<Student>(crt.list());
Should I use Set over projection to get unique data list?
Thanks in advance
Hi,
I got the solution, now sharing for all. We should fetch smaller data list from db and it is faster. So I think we should use Projection. But now I implemented this as follows:
Criteria crt = sessionFactory.getCurrentSession().createCriteria(User.class);
crt.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<User> users = crt.list(); // fetch distinct User list
I used Criteria.DISTINCT_ROOT_ENTITY instead of Projection.

Getting a single element from a list of lists in Hibernate

So I have a list of ObjectA in hibernate. ObjectA contains a list of ObjectB.
I want to display ObjectA.title along with ObjectB.rank for a given ObjectB.year.
What I do now, is that I iterate over the first list (ObjectA) and get the title. Then I iterate the second list (ObjectB) to get the correct rank for the specified year.
Because both lists can be quite large, this takes a while. Is there any way to speed up the fetching, by having hibernate only get one row from the second list instead of going through each of them and checking the year?
You could if you have a bi-directional relation from ObjectB back to ObjectA.
See http://docs.jboss.org/hibernate/orm/3.5/reference/en/html/collections.html#collections-bidirectional
Then you could do an HQL:
select b.rank, a.title from ObjectB b join b.a a where b.year=... and a.id=...

How do I create an object for Hibernate's query response?

Using an object that extends HibernateDaoSupport, I ran a query using this right here:
List<Object> trialList2 = getSession().createSQLQuery(trialQuery2).list();
Where trialQuery2 is just some query that returned a single row. I got back a list with one Object on it, which when inspected in Eclipse looks like this:
[some, random, data]
I'd like to create an Object that can accommodate what I got back from the query, but a simple Javabean object that can has those fields doesn't seem to work. Does anyone know what kind of object I would have to make to be able to access those values?
It would be actually Object[] not Object
List<Object[]> trialList2
Based on columns in your select query, you get values from index
Let us say, if your query is select firstname, lastname from employee;
Object[0] would be firstname
Object[1] would be lastname.
As per documentation :
These will return a List of Object arrays (Object[]) with scalar values for each column in the table
U can replace any class name for BEANCLASSNAME
List<BEANCLASSNAME> trialList2 = getSession().createSQLQuery(trialQuery2).setResultTransformer(new AliasToBeanResultTransformer(BEANCLASSNAME.class)).list();

How to recover data from a table, and store that into list?

How can I get ALL the data from a table of my DB (mySQL) using hibernate, and store the output into List of Objects?
I'll use this List, to populate a JTable.
What about:
Query q = session.createQuery("from Foo");
List foos = q.list();
Where Foo is an entity mapped on your table. But maybe I missed something.
If you use findAll(), which should be a part of the GenericDAO, it returns everything as a List.

Categories

Resources