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
Related
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());
I use hql to query from database. first query returns a list of objects like this
see image:
as you can see, the field names of retrieved objects are obvious (ID, TIMESTAMP, ...)
but in the second query, what I get, is this:
see image:
and I need to change field names ([0] to ID , 1 to TITLE, ...)
any solutions?!
Create a simple data holding object that contains your needed data.
Fill your data into such an object
Add this object to a new list/array
View the debugger on this list again
For example, I have a base entity called 'MyEntityParent' and two sub types 'ZChild' and 'AChild'.
When using the following HQL, this will sort the result list by the type's internal integer value (same as the special class property):
select e from MyEntityParent e
order by type(e)
However, I need to sort the result list by their entity type's name. I.e., first, instances of type 'AChild', then, instances of type 'ZChild'.
Try
select e from MyEntityParent e order by e.class
From the Hibernate 3.6 documentation:
The special property class accesses the discriminator value of an instance in the case of polymorphic persistence. A Java class name embedded in the where clause will be translated to its discriminator value.
http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html#queryhql-where
But this is deprecated in Hibernate 4.1
Also this is not exactly your wish as it returns the discriminator value, not the exact type name if i read the docs correctly.
There is no straightforward solution, because HQL is translated to SQL and database is not aware of how the value maps to the name of the class. There is way to make it, but it is not nice:
Query gets complex, because we have to define order in query.
Select list contains additional value that defines order, so we cannot return list of entities.
When model changes, query should be modified
Query is:
SELECT e,
CASE TYPE(e) WHEN AChild THEN 1
WHEN ZChild THEN 2
ELSE 3
END AS orderValue
FROM MyEntityParent e ORDER BY orderValue
And as said, result is no more list of entities, it is list of object arrays, so accessing entity is bit more hard. Entity itself is first item in array (index 0) and ordedValue in second:
List<Object[]> result = em.createQuery("JPQL given above").getResultList();
for (Object[] resultRow : result) {
MyEntityParent myEntityParent = (MyEntityParent) resultRow[0];
}
Solution is valid JPQL, so it is not Hibernate specific, but works also with other JPA providers.
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 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.