I`m new in Java and have a problem with showing data from a list of objects. I have a simple method, which should collect data across multiple tables and return it to my controller:
public List<Object> getHouseInfo(){
Query q = em.createNativeQuery("SELECT houses.id, addresses.country, addresses.region, house_details.rooms, house_details.square FROM houses, addresses, house_details");
List<Object> myList = q.getResultList();
return myList;}
Now I want to get this data in controller, but I don`t know how to get single results from the list. I tried to do something like this:
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0));}
but I`m getting only references to this objects (for example [Ljava.lang.Object;@167a47b). I also tried to use Iterator, but the result is the same. I tried to use a code like this:
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(0)[0]);}
but it doesn`t help me too - this ends with a compile error.
Can someone tell me how to get an 'id'(integer value) from this list? I`m using MyFaces in my 'View' where I have a code like this (houseControll is the name of my JSF Managed Bean - the controller):
<t:dataList id="myDataList" value="#{houseControll.fullOffer}" var="element" rows="3" >
...
<t:outputText id="houseId" value="#{element[0]}"/>
...
</t:dataList>
this code shows an 'id' value properly - I have 1,2,3,... values. How can I get the same result in my controller? How to print the data in controller?
The list of all declared fields can be obtained using the java. lang. Class. getDeclaredFields() method as it returns an array of field objects.
You can get the object at an index using obj = listName. get(index) and set the object at an index using listName. set(index,obj) .
get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.
It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. The List interface is found in java. util package and inherits the Collection interface.
For starters you aren't iterating over the result list properly, you are not using the index i at all. Try something like this:
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
System.out.println("Element "+i+list.get(i));
}
It looks like the query reutrns a List of Arrays of Objects, because Arrays are not proper objects that override toString you need to do a cast first and then use Arrays.toString().
List<Object> list = getHouseInfo();
for (int i=0; i<list.size; i++){
Object[] row = (Object[]) list.get(i);
System.out.println("Element "+i+Arrays.toString(row));
}
Do like this
List<Object[]> list = HQL.list(); // get your lsit here but in Object array
your query is : "SELECT houses.id, addresses.country, addresses.region,..."
for(Object[] obj : list){
String houseId = String.valueOf(obj[0]); // houseId is at first place in your query
String country = String.valueof(obj[1]); // country is at second and so on....
.......
}
this way you can get the mixed objects with ease, but you should know in advance at which place what value you are getting or you can just check by printing the values to know. sorry for the bad english I hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With