This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting first row as object  RSS feed

 
Ranch Hand
Posts: 49
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
 
Rancher
Posts: 3492
39
  • 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: 49
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
Rancher
Posts: 3492
39
  • 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: 49
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
Rancher
Posts: 3492
39
  • 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: 49
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
 
Sheriff
Posts: 21327
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding an explicit select:
 
See where your hand is? Not there. It's next to this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!