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
Related
I am using Jooq and as a result of a query with some joins I have a list of Record .
This record includes two columns with name "id" and I would need to select one of them.
This method has String as parameter and it is not failing when passing "id" (probably returning the first occurrence).
The other alternative I saw is to use the method that receives an int with the index. This would work ok but I would prefer to avoid it.
The best alternative I found on this case is to use the method that receives Field as parameter.
By using Field we can specify the table and column name so we are sure that we are selecting the column that we want.
So basically if we have table A and B both with "id" columns on the result of the query we can just define a field
final Field<UUID> bId = tableB.ID;
And then when processing the results we can access it by
query.fetch().forEach(r -> { UUID theId = r.get(bId); });
I have a list of records in a entity type ,i had a confusion the way how to represent in column wise list or each of record as a json object,that means list of JSON Objects.
::Explanation ::
Here firstName,secondName,thirdName are column names.
Need To Represent Like this format
__{{firstName:Rec1,secondName:Rec1,thirdName:Rec1},{firstName:Rec2,secondName:Rec2,thirdName:Rec2}...n}__
But i approached like
__{firstName:[Rec1,Rec2,Rec3...],secondName:[Rec1,Rec2,Rec3...],thirdName:[Rec1,Rec2,Rec3...]},__
When i tried to pass as a entity type means it shows like
__{"ServiceData":[com.aiway.eea.ejb.to.student.ParentStudentDetailsTO#8b0db2,com.aiway.eea.ejb.to.student.ParentStudentDetailsTO#1304967]}__
Please Help me to solve this...
we not able to pass as a entity type,we need Transfer Object to convert from ENTITY to JSON object,if List entityType means then Use List JSONObject and then put that List of JsonObject to another Json object like <"serviceData":JsonObject> then it will OUTPUT like
{"ServiceData":[{"parentId":4009,"parentRelation":"Father","parentNotification":false,"parentOccupation":"bussiness","parentEducation":"mba","parentName":"tharun"},{"parentId":4010,"parentRelation":"Mother","parentNotification":false,"parentOccupation":"sdfsdf","parentEducation":"sdfsdfs","parentName":"rakshitha"}]}
You can retrive like the Example syntax
See The Example From this IMAGE Link....
use this Link to lean about JSON Array
https://www.w3schools.com/js/js_json_arrays.asp
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
I have a object which contains an ArrayList with "Group" objects. Every Group object have a ArrayList of "Table" objects and these objects have a ArrayList with "Field" objects. All these object have their own properties as well like "name" etc.
I want to render this object in jasper reports but I'm having trouble figuring out how to iterate and group by Group's name and Table name and then iterate through the fields..
Not really sure how I should pass the object to the report and how I should handle it.
The result in my report should look something like this.
GroupA
GroupA information
Table A object name and version
Field1, properties...
Field2, properties...
Table B Object name and version
Field1, properties...
Field2, properties...
Group B
...
You can pass your data to the report by wrapping it in a JRBeanCollectionDataSource. That would iterate groups in the report detail band.
To further iterate on sublists, you can use lists, tables or subreports (also passing the sublists by wrapping them in JRBeanCollectionDataSource).
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();