-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Retrieving first item in collection - unindexed fromElement
PostPosted: Wed May 14, 2008 6:10 am 
Newbie

Joined: Wed May 14, 2008 5:58 am
Posts: 4
Hello there, I need to retrieve a property on the first item in a collection:

Code:
public class Order {
@OneToMany(mappedBy="order", targetEntity=Item.class)
private List<Item> items;

// Getter and setter
}

public class Item {
private Integer ID;
@ManyToOne(targetEntity=Order.class)
private Order order;
private Integer code;

// Getters and setters
}

I have tried:

Code:
executeHQLQuery("FROM Order AS order WHERE order.items[0].code = 5");


But I get the error:

Code:
org.springframework.orm.hibernate3.HibernateQueryException: unindexed fromElement before []


Any ideas? The join works fine when retrieving an Order normally.

Many thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 9:55 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
So, you are looking for an Order with a particular code property - in this case the value of 5?

Wouldn't a slick use of the Criteria API make this a whole lot easier?


Code:
    Order o = new Order();
    o.setCode(5);
    Example example = Example.create(o);
    Session session = HibernateUtil.beginTransaction();
    Criteria criteria = session.createCriteria(Order.class);
    criteria.add(example);
    List results = criteria.list();
    HibernateUtil.commitTransaction();
    for (int i = 0; i<results.size(); i++) {
      System.out.println(results.get(i).toString());
   }


This example is taken from a Hibernate example tutorial from here:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=09howtousethecriteriaapi

Criteria queries are very efficient, and will alleviate many of your Hibernate Query Language headaches.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 14, 2008 10:38 am 
Newbie

Joined: Wed May 14, 2008 5:58 am
Posts: 4
Thank you :) Yes, that is easier - I just started with HQL and didn't want to let go! I can't find many references to the error on the web, so I would still be interested in knowing the problem...


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 12:10 pm 
Newbie

Joined: Wed May 14, 2008 5:58 am
Posts: 4
Sorry, on second glance this doesn't solve the problem at all. I am looking for a property called "code" on the FIRST Item in an Order's "items" list. It seems there's a problem with addressing the index 0 in the way that I have done it....

Thanks!


Top
 Profile  
 
 Post subject: I am getting the very same problem
PostPosted: Tue Jan 13, 2009 4:46 am 
Newbie

Joined: Tue Jan 13, 2009 4:32 am
Posts: 3
I am a newbie to hibernate too

here is a schema:

Code:
public class Person {
   String firstName;
   String middleName;
   String lastName;
   int Id;

   Set setEmails=new HashSet(); //Bad naming convention indeed
}


This is the query that i am trying to execute:

Code:
from Person p where p.setEmails[0]='a@b.com'


I am getting the same exception. Please Help

I had referred to
http://www.hibernate.org/hib_docs/refer ... sions.html
for the above syntax

My exception:
Code:
Exception in thread "main" org.hibernate.QueryException: unindexed fromElement before []: p.setEmails [from simpledemo.Person p where p.setEmails[0]='a@b.com']

_________________
You cannot see you cannot tell
the boundaries of my personal hell.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 5:00 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The collection needs to be an array or list if you want to use indexes. Eg. you need to specify an @IndexColumn in your annotations. See http://www.hibernate.org/hib_docs/annot ... -extratype for more information


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 5:07 am 
Newbie

Joined: Tue Jan 13, 2009 4:32 am
Posts: 3
thanks for the prompt replay.

its good if its possible to use array/list


Is there another way to access using Sets ?

_________________
You cannot see you cannot tell
the boundaries of my personal hell.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.