Convert a List<Object> to List String - java

Query w=em.createQuery("SELECT ar.authorId.firstName,
count(ar) FROM Article ar
WHERE ar.categoryId.categoryText=:
categoryText GROUP BY ar.authorId");
w.setParameter("categoryText", categoryText);
List list1=w.getResultList();
System.out.println(list1.toString());
Whats the wrong. i get [[Ljava.lang.Object;#60a19573, [Ljava.lang.Object;#44a085e5]
I want to print the query like "name:"+firtname+"Articles:"+count(ar)

Implement toString() in that particular type SomeType

What you're getting back in your query is a List<Object[]> which is expected as per the spec. The first element in your array is the string ar.authorId.firstName and the second is a Long representing count(ar)
A String.format should work here for you:
for(Object[] objs : list1) {
System.out.println(String.format("name %s articles %s",objs[0],objs[1]);
}

for(Object objs : list1) {
System.out.println(String.valueOf(objs));
}
valueOf is an String method which helps to convert primitive data types to String.

Related

Error in casting Object[] to byte[]

I want to read from database a pdf, which stores as BLOB and I want to get List<bytes[]>
session.beginTransaction();
final Criteria criteria = session.createCriteria(MyClass.class);
criteria.add(Restrictions.eq("id",id));
final ProjectionList projectionList = Projections.projectionList().add(
Projections.property("bdoc"));
criteria.setProjection(projectionList);
List<Object[]> list = criteria.list();
List<byte[]> listBytes = new ArrayList<byte[]>();
for (Object[] item : list) {
listBytes.add((byte[]) item[0]);
}
session.getTransaction().commit();
But I get an error in this line for (Object[] item : list) {
[ERROR] [B cannot be cast to [Ljava.lang.Object;
I've debugged and I do read data from database: my List<Object[]> list = criteria.list() is not empty.
But I can not convert from List<Object[]> to List<bytes[]>. What am I doing wrong? Help me please to resolve my problem.
B cannot be cast to [Ljava.lang.Object;
Means you are indeed getting bytearray and you are trying to convert it into Object array.
This problem is caused because Criteria.list() returns a raw List. As a result, you are able to write code that causes a ClassCastException. With a properly typed list, the compiler would prevent this.
As explained in the other answer, the text [B cannot be cast to [Ljava.lang.Object means that a byte[] is being cast to an Object[], which is not allowed.
This means that your list contains byte[] objects and you should declare it as such:
List<byte[]> list = criteria.list();
In addition, the contents of your for-loop now look incorrect:
listBytes.add((byte[]) item[0]);
Since item is now a byte[], it's not correct to cast a byte to a byte[]. Perhaps you need to remove the array index [0].

Extract data from an Object

I have a SQL query that returns a list of deals which have this structure : deal_id, value_1, value_2, ..., value 50
I put the result of this query in a list like this (using org.hibernate.SQLQuery) :
SQLQuery select = session.createSQLQuery(QUERY);
List result = select.list();
Then I use this loop to iterate through the results :
for (Object o : result) {
}
When debugging, o looks like this (one String, 50 BigDecimal) :
[SSA12325, 0, 1.012, 1.1235, ..., 0]
I want to extract the deal_id, how do I do that ?
I tried to convert o to a List, but I got a classCastException : [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
here your Object o is Array of Object because it contains all Objects such as String and Integer.
So, try:
Object[] objArray = (Object[]) o;
then do whatever you want to do with your array.

How to get attributes of an Object in Java?

I have a function that returns an Object
The toString() method shows that my object has two BigDecimal attributes. But I don't know how to get them in the code ?
My function uses hibernate to get results from a query is :
public Object executeQuery(final String sql) {
final Query query = getSessionFactory().getCurrentSession().createSQLQuery(sql);
return query.list().get(0);
}
Thank you.
-- Additional infos:
obj.getClass().getDeclaredFields(); // empty array []
obj.getClass().getName(); // [Ljava.lang.Object;
final BigDecimal b = (BigDecimal) obj[0]; //Compilation error: The type of the expression must be an array type but it resolved to Object
obj.getClass().getDeclaredFields() can help you. Generally learn reflection API. If you object bean you can also use Jackarta BeanUtils.
Judging from your comments, your Object is and Object array.
So you should first cast the result to an Object array:
Object[] obj = (Object[]) query.list().get(0);
Then, you should be able to access the first BigDecimal like that:
BigDecimal b = (BigDecimal) obj[0];
Probably, you want to add some exception handling.
It is not an Object, it is an Array of Objects.
BigDecimal firstColumn = (BigDecimal) ((Object[])query.list().get(0))[0];
BigDecimal secondColumn = (BigDecimal) ((Object[])query.list().get(0))[1];
That's all.
UPDATE:
You have a resultset with 2 columns.
Object[] result= query.list().get(0);
BigDecimal number1 = (BigDecimal) result[0];
BigDecimal number2 = (BigDecimal) result[1];
Get first what class name of that object by
System.out.println(obj.getClass());
Since you are running a sql query, result might be an Entity or Object[].
when you came to know retrieved object from query is an Object[] you can iterate like
if( obj instanceof Object[] ) {
Object[] objA = (Object[])obj;
for(Object atomicObj : objA ) {
System.out.println(atomicObj);
}
}
It works for all elements which presents in object array. This time you may get BigDecimal, next query might return a String and BigDecimal.

Why string is not printing correctly?

I have created a user type in oracle
create or replace type my_friend_list
is
table of
varchar2(100);
Now i have written a procedure which has a output parameter like this :
PROCEDURE friends_diff_prc
(
my_name IN VARCHAR2,
friend_i IN my_friend_list ,
good_friend_o OUT my_friend_list ,
best_friend_o OUT my_friend_list ,
isSuccess OUT NUMBER
);
I run it on SQLDeveloper And it is running correctly .
And it is giving me list both of my good friends and best friends.
But when i am calling it through JAVA Class which is extending StoredProcedure.
best_friend list is correcly coming but for the good_friend list it is printing
Ljava.lang.Object;#24342434,
Ljava.lang.Object;#243a243a,
Ljava.lang.Object;#24402440,
Ljava.lang.Object;#24462446
My java code to fetch this output arraylist is like this:
List<String> goodfriends = Arrays.asList((String[]) results.get("good_friend_o");
List<String> bestfriends = Arrays.asList((String[]) results.get("best_friend_o");
Why it is not giving correct result for good_friends ?
Thanks in advance.
Your Getting the answer as Array of String, and then storing in a String of List.. You cannot store the String[] into String straightly.
can you post the, java code for fetching and printing...
Try this code. This will print the strings
System.out.println("Printing goodfriends");
for (Object object : goodfriends) {
Object[] goodfriendsString = (Object[]) object;
for (Object object2 : objLocationVO) {
System.out.println(object2);
}
}

How to convert string array of data object to array list of another data object in java

I have vehicle data object returning string array .I want to convert or cast to Arraylist BuyingDo data object.I have ArrayList objectList
I am getting value like this
VehicleDo list[]=data.getList()
I am converting like this
objectList=(ArrayList<BuyingDo)list
but its not working can anybody tell how to do this
Your question is a bit... well... unclear.
I think you mean either this:
List<VehicleDO> realList = Arrays.asList(list);
Or this:
List<BuyingDO> realList = new ArrayList<BuyingDO>(list.length);
for (VehicleDO vdo : list){
realList.add((BuyingDO)vdo);
}
Assuming of course that VehicleDO can be cast to BuyingDO.
Something like that?

Categories