• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting first row as object  RSS feed

 
Miri Yusifli
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i use select query , query.list() return List<Objec[]>. is it true?  if i want to get first row as object , Should i get first element of list as Object[] , then get first element of array as myClass ?

Object[] rows=(Object[])models.get(i);
Model firstRow=(Model) modeller[0];

please answer all my questions , thanks in advance
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an example of what you are doing?
 
Miri Yusifli
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@Transactional
  public List<Object> GetModels(String marka)
  {
    Session session = sessionFactory.getCurrentSession();
    String hql = "from Model as m "
    + "inner join m.marka "
    + "Where marka=:marka";
    Query query = session.createQuery(hql);
    query.setParameter("marka", marka);
   
    System.out.println("query is "+query);
 
   
    // this is my question
   
    List<Object[]> li= (List<Object[]>)query.list();
    Object[] modeller=(Object[])li.get(1);
    Model model=(Model) modeller[0];



    System.out.println("test is "+model.getModel());
    return query.list();
   
 
  }
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first glance that should be returning a List<Model>.
Is that not the case?
 
Miri Yusifli
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly , i used List<Model> , but in this case , java say that :"java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to"
 
Dave Tolls
Ranch Foreman
Posts: 3056
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the first thing to do is find out what it is returning.
Actually, there's a join there, so it's likely an Object[], with the first bit being a Model and the second being whatever markka is.

But you can check that by logging what the classes of the Objects in the array are.
 
Miri Yusifli
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Well, the first thing to do is find out what it is returning.
Actually, there's a join there, so it's likely an Object[], with the first bit being a Model and the second being whatever markka is.

But you can check that by logging what the classes of the Objects in the array are.




Thanks, i got it
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding an explicit select:
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!