Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

EJB-QL problem

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
I have a table List(onwer_id, list_title, pers_id). The primary key for this table is owner_id, list_title, pers_id and the data in the table is : 1, "title_1", 1
1, "title_1", 2
1, "title_2", 1
1, "title_2", 3
1, " title_2",4
I would like to get distinct list_title(ex: "title_1" and "title_2") of owner having id=1
Here is my EJB_QL
If I use this : "SELECT OBJECT(L) FROM List L WHERE L.owner_id=?" and I input 1 into my ejbFinder method. It will return all the rows in the table.(I don't want this solution)
If I use this : "SELECT DISTINCT L.list_title FROM List L WHERE L.owner_id=?"(This will return only "title_1" and "title_2" as I wanted but this throws an ClassCastException when I use EJBRemote interface to cast the object from the returned collection to the remote interface. However, If I cast the object in the returned collection to String object It will work but does not return "title_1" and "title_2"
pls provide any suggestion or solution to this problem.
Thanks in advanced
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you using "?" or "?1" as your parameter placeholder ("?" is not valid EJB-QL although some appserver might support it for some weird reason)?
 
THUAN VN
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lasse
Thanks for your answer. I used "?1" in my EJB-QL. The problem here is that when I use "SELECT DISTINCT L.List_Title FROM List L WHERE L.owner_id=?1" the result of this query is a collection of String object not a collection of EJB( because when I cast an object from the returned collection to EJB this will throws ClassCastException). When I cast an object of the returned collection to String object it worked well and I use the toString() function to get the values of this object it returned "EJB/ListLocal:title_1" not "title_1"
The question is how to get "title_1" without parsing the returned string "EJB/ListLocal:title_1"?
Thanks in advance
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange. The EJB-QL you're using should be correct. Btw, it returns string objects because your SELECT clause identifies an object of type String.
Which application server you're using?
 
THUAN VN
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lasse
I use JBoss 3.2.0 to run my application.
Could you pls suggest any solution for this?
Thanks in advance
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thuan, I would love to give you a solution but my suggestion would be exactly the same as what you've already done (i.e. the EJB-QL statement looks correct). Maybe you should ask about the weird return values at our JBoss forum (or at the official forum).
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A finder method always returns bean instances. You are looking for String instances (or are beyond the spec [i think] by looking for instances with a specific field.).
Use an ejbSelect method to return the distinct Strings (it'll cast and look right this way). You can't get a distinct bean based on a field - think about it, which bean would it choose to give you? The first? Last?
I don't think that EJBQL supports subqueries, but if they did, this would allow you to specify a specific one based on some other criteria (like max(pers_id))....
Get it?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dana. I had completely missed the fact that the query was for an ejbFinder.
 
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic