• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

EJB QL - returning collection

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to EJB 2.o specs, page 233:

1) SELECT l.product FROM ORDER o, IN(o.lineitems) l --> is Valid.
2) SELECT o.lineitems FROM ORDER o ---> is Invalid
3) SELECT OBJECT (l) FROM ORDER o, IN (o.lineitems) l ---> is valid.

What is the difference between 2 & 3. WHy is the second query invalid ?

Is o.lineitems collective valued expression ? If so what about the same in third query.

Thanks
Sankar
 
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SELECT clause in EJB-QL determines what is returned by the query. As per the rules of EJB QL, the SELECT clauses are classified into 2 types.

1. SELECT clauses with RANGE variables
2. SELECT clauses with single-valued path expressions

When you want to return a collection of component interfaces, you have to use 1. Whenever you use option 1, you must use OBJECT in your SELECT clause.

Example : SELECT OBJECT (l) FROM ORDER o, IN (o.lineitems) l

This returns a collection of line item component interface references belonging to all the Orders in the database.

When you do not want to return a collection of component interface references, but you want to return a collection of a CMP field type, you have to use option 2.

Ex : SELECT l.product FROM ORDER o, IN(o.lineitems) l

is valid assuming product is a CMP field. This takes all the Orders in the database that have atleast one line item and returns each of the line item's product. Hence it returns a collection of products.

Important rule : You cannot have a CMR field as the SELECT return type

This rule is violated by 2 and hence is invalid.

If you want to return a collection of a CMR field type, you should not use the option 2 (single-valued path expression) as it would violate the above rule. Your only option is option 1 (Range variable), hence 3 is valid
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sathish,
Thanks for your reply. But i am not convinced with the clarification.

According to you "SELECT l.product FROM ORDER o, IN(o.lineitems) l " only if product is a CMP field.
Refer to relation between Order, LineItems, Product in specs (page 220).
(i.e) Order indirectly refers to product via lineitems. So it is a CMR field.
But then why the above query is valid according to EJB specs page 233 ?
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think CMP field as something which corresponds to a database field and CMR field corresponds to a database row or a bean object.

1) SELECT l.product FROM ORDER o, IN(o.lineitems) l --> is Valid.
2) SELECT o.lineitems FROM ORDER o ---> is Invalid
3) SELECT OBJECT (l) FROM ORDER o, IN (o.lineitems) l ---> is valid.

You see in example 2) lineitems is refering to a CMR field which can be used to get list of objects. Whenever you want an object, you have to use OBJECT() keyword. Hence 3) is right way of doing it and 2 is invalid.
In 1) product is a CMP field in child table, so OBJECT is not required.
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please provide me additional clarification regarding the point "In 1) product is a CMP field in child table, so OBJECT is not required. "
Thanks.
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The HFEJB book misled me here. There is a bang box that says "You cannot have a CMR field as the SELECT return type"

After referring to the spec, I now realize that's wrong

You can have a CMR field as the SELECT return type as long as it is a single-valued field, but you cannot have a CMR collection-valued field as the SELECT return type.

So change the rule to :

You cannot have a collection-valued CMR field as the SELECT return type using the dot navigation operator, but you can have CMP fields and single-valued CMR fields. For collection-valued CMR fields, you must use range variables with the IN operator in the FROM clause

This explains why 2 is wrong and 1 is right. Thanks for pointing this out. This is a mistake in HFEJB book
 
Sankar Subbiramaniam
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sathish. Thats exactly what i was trying to point out with my examples. I couldn't find anywhere in the specs about the point "You cannot have a CMR field as the SELECT return type".

I wish the authors of HFEJB confirm this.
 
B.Sathish
Ranch Hand
Posts: 372
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the authors will be able to confirm this, because I believe they will hardly remember anything about EJB 2.0. I think they'll be busy learning Java 5.0, EJB 3.0 and Web Services to write new books and they would have forgotten all about this old stuff
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic