EJB QL that uses an in clause does not work.
robr Jun 12, 2005 2:01 PMI am trying to use a method in an EJB that uses a select statement that uses the in clause.
An sql example would be select * from quote q where q.id in ('sw','se')
My code looks like the following.
priceMatrixEntityHome.java
Collection findRowsById(String sIds) throws RemoteException, FinderException;
ejb-jar.xml definition for bean - query part at the end
<entity> <description>PriceMatrix Entity Bean</description> <display-name>PriceMatrix Entity Bean</display-name> <ejb-name>priceMatrixEntityEJB</ejb-name> <home>com.dynix.webquotes.ejb.entity.admin.priceMatrixEntityHome</home> <remote>com.dynix.webquotes.ejb.entity.admin.priceMatrixEntity</remote> <ejb-class>com.dynix.webquotes.ejb.entity.admin.priceMatrixEntityBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <cmp-version>2.x</cmp-version> <abstract-schema-name>priceMatrixEntityEJB</abstract-schema-name> <cmp-field> <description>ID</description> <field-name>id</field-name> </cmp-field> <cmp-field> <description>Description</description> <field-name>descr</field-name> </cmp-field> <cmp-field> <description>Type</description> <field-name>type</field-name> </cmp-field> <cmp-field> <description>Base</description> <field-name>base</field-name> </cmp-field> <cmp-field> <description>Staff</description> <field-name>staff</field-name> </cmp-field> <cmp-field> <description>Bib</description> <field-name>bib</field-name> </cmp-field> <cmp-field> <description>Item</description> <field-name>item</field-name> </cmp-field> <cmp-field> <description>Location</description> <field-name>location</field-name> </cmp-field> <primkey-field>id</primkey-field> <query> <description> Return Entity beans for that match id list passed in. </description> <query-method> <method-name>findRowsById</method-name> <method-params> <method-param>java.lang.String</method-param> </method-params> </query-method> <ejb-ql> <![CDATA[select object(u) FROM priceMatrixEntityEJB u where u.id in(?1)]]> </ejb-ql> </query> </entity>
Java code I am using to call the findRowsbyID method
cSelPMRows = pricMatrixEntityHome.findRowsById("'hacad'");
The above code doesn't return any rows. If I change the code to the following I get a row back.
cSelPMRows = pricMatrixEntityHome.findRowsById("hacad");
My problem I need this to be dynamic. My in clause may need to contain one or more elements.
I tried the following code and it did not work.
cSelPMRows = pricMatrixEntityHome.findRowsById("hacad,hpublic");
I try the following sql statement and it gets two rows back.
select * from pricematrix where id in ('hacad','hpublic')
I am sure that my problem is that I do not know how to format the sIds parameter that is being sent in the findRowsById function.
Collection findRowsById(String sIds) throws RemoteException, FinderException;
I assumed I should have created a string like "'ida','idb','idc'" and pass it in. My test showed that it did not like the single quotes because the "'hacad'" parameter did not work whereas the "hacad" parameter does work.
Maybe I need to pass in a String[] with an element for each ID i am interested in. Anybody know how to do this correctly. I will keep playing and post a solution when I figure it out. Hope someone beats me to the answer.
Thanks.