Home » Modeling » EMF » [CDO] Fetch multiple objects by CDOID
|
Re: [CDO] Fetch multiple objects by CDOID [message #431307 is a reply to message #431300] |
Wed, 08 July 2009 06:24 |
|
This is a multi-part message in MIME format.
--------------000100060707050409000706
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Kai,
I strongly recommend to spend your time on efforts that are more
re-usable ;-)
Yesterday Martin implemented HQL for the HibernateStore within only 2 or
3 hours:
|/**
* Implements server side HQL query execution..
*
* @author Martin Taal
*/
*public class *HibernateQueryHandler *implements *IQueryHandler
{
*public static final *String QUERY_LANGUAGE = "hql";
*public static final *String FIRST_RESULT = "firstResult";
*private *HibernateStoreAccessor hibernateStoreAccessor;
/**
* Executes hql queries. Gets the session from the {@link HibernateStoreAccessor} creates a hibernate query and sets
* the parameters taken from the {@link CDOQueryInfo#getParameters()}. Takes into account the
* {@link CDOQueryInfo#getMaxResults()} and the {@link HibernateQueryHandler#FIRST_RESULT} values for paging.
*
* @param info
* the object containing the query and parameters
* @param context
* the query results are placed in the context
* @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
*/
*public **void *executeQuery(CDOQueryInfo info, IQueryContext context)
{
*if *(!QUERY_LANGUAGE.equals(info.getQueryLanguage().toLowerCase ()))
{
*throw new *IllegalArgumentException("Query language " + info.getQueryLanguage() + " not supported by this store");
}
// get a transaction, the hibernateStoreAccessor is placed in a threadlocal
// so all db access uses the same session.
*final *Session session = hibernateStoreAccessor.getHibernateSession();
*try*
{
// create the query
*final *Query query = session.createQuery(info.getQueryString());
// get the parameters with some parameter conversion
*int *firstResult = -1;
*for *(String key : info.getParameters().keySet())
{
*if *(key.toLowerCase().equals(FIRST_RESULT.toLowerCase()))
{
*final *Object o = info.getParameters().get(key);
*if *(o != *null*)
{
*try*
{
firstResult = (Integer)o;
}
*catch *(ClassCastException e)
{
*throw new *IllegalArgumentException("Parameter firstResult must be an integer but it is a " + o
+ " class " + o.getClass().getName());
}
}
}
*else*
{
// in case the parameter is a CDOID get the object from the db
*final *Object param = info.getParameters().get(key);
*if *(param *instanceof *CDOIDHibernate)
{
*final *CDOIDHibernate hibernateId = (CDOIDHibernate)param;
query.setEntity(key, session.get(hibernateId.getEntityName(), hibernateId.getId()));
}
*else*
{
query.setParameter(key, param);
}
}
}
// set the first result
*if *(firstResult > -1)
{
query.setFirstResult(firstResult);
}
// the max result
*if *(info.getMaxResults() != CDOQueryInfo.UNLIMITED_RESULTS)
{
query.setMaxResults(info.getMaxResults());
}
// and go for the query
// future extension: support iterate, scroll through a parameter
*for *(Object o : query.list())
{
*final **boolean *addOneMore = context.addResult(o);
*if *(!addOneMore)
{
*return*;
}
}
}
*finally*
{
hibernateStoreAccessor.endHibernateSession();
}
}
*public *HibernateStoreAccessor getHibernateStoreAccessor()
{
*return *hibernateStoreAccessor;
}
*public **void *setHibernateStoreAccessor(HibernateStoreAccessor hibernateStoreAccessor)
{
*this*.hibernateStoreAccessor = hibernateStoreAccessor;
}
}|
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Kai Schlamp schrieb:
> Hello.
>
> For perfomance reasons I need to do some direct SQL queries (regexp
> search in the database). As there is no query implemented for CDO yet,
> I thought of just connecting to the database directly (beside the CDO
> connection) and query for the CDOID's directly.
> What would be the best way to load the objects then with CDO?
> Just use Transaction.getObject(cdoId, true)? Even if it is a very
> large list of CDOID's (about 50000)?
>
> Regards,
> Kai
--------------000100060707050409000706
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Kai,<br>
<br>
I strongly recommend to spend your time on efforts that are more
re-usable ;-)<br>
Yesterday Martin implemented HQL for the HibernateStore within only 2
or 3 hours:<br>
<br>
<title></title>
<style type="text/css">
<!--code { font-family: Courier New, Courier; font-size: 10pt; margin: 0px; }-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- ======================================================== -->
<!-- = Java Sourcecode to HTML automatically converted code = --><!-- = Java2Html Converter 5.0 [2006-02-26] by Markus Gebhard markus@jave.de = -->
<!-- = Further information: http://www.java2html.de = -->
<div class="java" align="left">
<table bgcolor="#ffffff" border="0" cellpadding="3" cellspacing="0">
<tbody>
<tr>
<!-- start source code --> <td align="left" nowrap="nowrap"
valign="top"> <code><font color="#3f5fbf">/**</font><br>
<font color="#ffffff"> </font><font color="#3f5fbf"> * Implements server side  ;HQL query execution.. </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font
color="#7f9fbf">@author </font><font color="#3f5fbf">Martin Taal</font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">*/</font><br>
<font color="#7f0055"><b>public class </b></font ><font
color="#000000">HibernateQueryHandler </font><font color="#7f0055"><b>implements </b></font><font
color="#000000">IQueryHandler</font><br>
<font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public static final </b></font><font
color="#000000">String QUERY_LANGUAGE = </font><font color="#2a00ff">"hql"</font><font
color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public static final </b></font><font
color="#000000">String FIRST_RESULT = </font><font color="#2a00ff">"firstResult"</font><font
color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>private </b></font><font
color="#000000">HibernateStoreAccessor hibernateStoreAccessor; </font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">/**</font><br>
<font color="#ffffff"> </font><font color="#3f5fbf"> * Executes hql queries. Gets the session from th e </font><font
color="#3f3fbf">{@link HibernateStoreAccessor} </font><font
color="#3f5fbf"> creates a hibernate query&nbs p;and sets </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf"> * the parameters taken f rom the </font><font
color="#3f3fbf">{@link CDOQueryInfo#getParameters()} </font><font
color="#3f5fbf"> . Takes into account the </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font
color="#3f3fbf">{@link CDOQueryInfo#getMaxResults()} </font><font
color="#3f5fbf">and the </font><font color="#3f3fbf"> {@link HibernateQueryHandler#FIRST_RESULT}  ; </font><font
color="#3f5fbf">values for paging.</font ><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font
color="#7f9fbf">@param </font><font color="#3f5fbf">info</font><br>
<font color="#ffffff"> </font><font color="#3f5fbf"> *   ; the object containing the query an d parameters </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font
color="#7f9fbf">@param </font><font color="#3f5fbf">context</font><br>
<font color="#ffffff"> </font><font color="#3f5fbf"> *   ; the query& amp;nbsp;results are placed in&am p;nbsp;the context </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">* </font><font
color="#7f9fbf">@see </font><font color="#3f5fbf"> IQueryHandler#executeQuery(CDOQueryInfo, IQueryCont ext) </font><br>
<font color="#ffffff"> </font><font color="#3f5fbf">*/</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public </b></font><font
color="#7f0055"><b>void </b></font><font color="#000000">executeQuery</font><font
color="#000000">(</font><font color="#000000"> CDOQueryInfo info, IQueryContext context </font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">!QUERY_LANGUAGE.equals</font><font
color="#000000">(</font><font color="#000000">info.getQueryLanguage</font><font
color="#000000">()</font><font color="#000000">.toLowerCase</font><font
color="#000000">()))</font><br>
<font color="#ffffff"> </font ><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>throw new </b></font ><font
color="#000000">IllegalArgumentException</font><font color="#000000">(</font><font
color="#2a00ff">"Query language " </font><font color="#000000">+ info.getQueryLanguage</font><font
color="#000000">() </font><font color="#000000">+ </font><font
color="#2a00ff">" not supported by this&a mp;nbsp;store "</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font ><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font ><font color="#3f7f5f"> // get a transaction, th e hibernateStoreAccessor is place d in a threadlocal </font><br>
<font color="#ffffff"> </font ><font color="#3f7f5f"> // so all db access& nbsp;uses the same session. </font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>final </b></font><font
color="#000000"> Session session = hibernateStoreA ccessor.getHibernateSession </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>try</b></font><br>
<font color="#ffffff"> </font ><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#3f7f5f">// create the query </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>final </b></font><font
color="#000000"> Query query = session.createQuery </font><font
color="#000000">(</font><font color="#000000">info.getQueryString</font><font
color="#000000">())</font><font color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#3f7f5f"> // get the parameters wi th some parameter conversion </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>int </b></font><font
color="#000000">firstResult = -</font><font color="#990000">1</font><font
color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>for </b></font><font
color="#000000">(</font><font color="#000000">String key : info.getParameters </font><font
color="#000000">()</font><font color="#000000">.keySet</font><font
color="#000000">())</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">key.toLowerCase</font><font
color="#000000">()</font><font color="#000000">.equals</font><font
color="#000000">(</font><font color="#000000">FIRST_RESULT.toLowerCase</font><font
color="#000000">()))</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>final </b></font><font
color="#000000">Object o = info.getParameters </font><font
color="#000000">()</font><font color="#000000">.get</font><font
color="#000000">(</font><font color="#000000">key</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">o != </font><font
color="#7f0055"><b>null</b></font><font color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>try</b></font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">firstResult = </font><font
color="#000000">(</font><font color="#000000">Integer</font><font
color="#000000">)</font><font color="#000000">o;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>catch </b></font><font
color="#000000">(</font><font color="#000000">ClassCastException e</font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>throw new </b></font ><font
color="#000000">IllegalArgumentException</font><font color="#000000">(</font><font
color="#2a00ff">" Parameter firstResult must be& ;nbsp;an integer but it is a " </font><font
color="#000000">+ o</font><br>
<font color="#ffffff"> </font><font
color="#000000">+ </font><font color="#2a00ff">" class " </font ><font
color="#000000">+ o.getClass</font><font color="#000000">()</font><font
color="#000000">.getName</font><font color="#000000">())</font><font
color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>else</b></font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#3f7f5f"> // in case the parameter is a CDOID get  ;the object from the db </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>final </b></font><font
color="#000000"> Object param = info.getParameters </font><font
color="#000000">()</font><font color="#000000">.get</font><font
color="#000000">(</font><font color="#000000">key</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">param </font><font
color="#7f0055"><b>instanceof </b></font><font color="#000000">CDOIDHibernate</font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>final </b></font><font
color="#000000">CDOIDHibernate hibernateId = </font><font
color="#000000">(</font><font color="#000000">CDOIDHibernate</font><font
color="#000000">)</font><font color="#000000">param;</font><br>
<font color="#ffffff"> </font><font color="#000000">query.setEntity</font><font
color="#000000">(</font><font color="#000000">key, session.get</font><font
color="#000000">(</font><font color="#000000">hibernateId.getEntityName</font><font
color="#000000">()</font><font color="#000000">, hibernateId.getId</font><font
color="#000000">()))</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>else</b></font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">query.setParameter</font><font
color="#000000">(</font><font color="#000000">key, param</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#3f7f5f">// set the first result </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">firstResult > -</font ><font
color="#990000">1</font><font color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">query.setFirstResult</font><font
color="#000000">(</font><font color="#000000">firstResult</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#3f7f5f">// the max result </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">info.getMaxResults</font><font
color="#000000">() </font><font color="#000000">!= CDOQueryInfo.UNLIMITED_RESULTS </font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">query.setMaxResults</font><font
color="#000000">(</font><font color="#000000">info.getMaxResults</font><font
color="#000000">())</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#3f7f5f"> // and go for the&nb sp;query </font><br>
<font color="#ffffff"> </font><font color="#3f7f5f"> // future extension: support& nbsp;iterate, scroll through a&am p;nbsp;parameter </font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>for </b></font><font
color="#000000">(</font><font color="#000000">Object o : query.list </font><font
color="#000000">())</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>final </b></font><font
color="#7f0055"><b>boolean </b></font><font color="#000000">addOneMore = context.addResult </font><font
color="#000000">(</font><font color="#000000">o</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>if </b></font><font
color="#000000">(</font><font color="#000000">!addOneMore</font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>return</b></font><font
color="#000000">;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"> </font ><font color="#000000">}</font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>finally</b></font><br>
<font color="#ffffff"> </font ><font color="#000000">{</font><br>
<font color="#ffffff"> </font><font color="#000000">hibernateStoreAccessor.endHibernateSession </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff"> </font ><font color="#000000">}</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public </b></font><font
color="#000000">HibernateStoreAccessor getHibernateStoreAccessor </font><font
color="#000000">()</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>return </b></font><font
color="#000000">hibernateStoreAccessor;</font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> </font><font color="#7f0055"><b>public </b></font><font
color="#7f0055"><b>void </b></font><font color="#000000">setHibernateStoreAccessor</font><font
color="#000000">(</font><font color="#000000">HibernateStoreAccessor hibernateStoreAccessor </font><font
color="#000000">)</font><br>
<font color="#ffffff"> </font><font color="#000000">{</font><br>
<font color="#ffffff"> </font ><font color="#7f0055"><b>this</b></font><font
color="#000000"> .hibernateStoreAccessor = hibernateStoreAc cessor; </font><br>
<font color="#ffffff"> </font><font color="#000000">}</font><br>
<font color="#000000">}</font></code> </td>
<!-- end source code --> </tr>
</tbody>
</table>
</div>
<!-- = END of automatically generated HTML code = -->
<!-- ======================================================== --><br>
Cheers<br>
/Eike<br>
<br>
----<br>
<a class="moz-txt-link-freetext" href="http://thegordian.blogspot.com">http://thegordian.blogspot.com</a><br>
<a class="moz-txt-link-freetext" href="http://twitter.com/eikestepper">http://twitter.com/eikestepper</a><br>
<br>
<br>
<br>
Kai Schlamp schrieb:
<blockquote cite="mid:h30d1n$uau$1@build.eclipse.org" type="cite">Hello.
<br>
<br>
For perfomance reasons I need to do some direct SQL queries (regexp
search in the database). As there is no query implemented for CDO yet,
I thought of just connecting to the database directly (beside the CDO
connection) and query for the CDOID's directly.
<br>
What would be the best way to load the objects then with CDO?
<br>
Just use Transaction.getObject(cdoId, true)? Even if it is a very large
list of CDOID's (about 50000)?
<br>
<br>
Regards,
<br>
Kai
<br>
</blockquote>
</body>
</html>
--------------000100060707050409000706--
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
Re: [CDO] Fetch multiple objects by CDOID [message #431309 is a reply to message #431307] |
Wed, 08 July 2009 07:35 |
Kai Schlamp Messages: 344 Registered: July 2009 |
Senior Member |
|
|
Ok, sounds reasonable. I'll see what I can do.
Eike Stepper wrote:
> Kai,
>
> I strongly recommend to spend your time on efforts that are more
> re-usable ;-)
> Yesterday Martin implemented HQL for the HibernateStore within only 2 or
> 3 hours:
>
> |/**
> * Implements server side HQL query execution..
> *
> * @author Martin Taal
> */
> *public class *HibernateQueryHandler *implements *IQueryHandler
> {
> *public static final *String QUERY_LANGUAGE = "hql";
>
> *public static final *String FIRST_RESULT = "firstResult";
>
> *private *HibernateStoreAccessor hibernateStoreAccessor;
>
> /**
> * Executes hql queries. Gets the session from the {@link HibernateStoreAccessor} creates a hibernate query and sets
> * the parameters taken from the {@link CDOQueryInfo#getParameters()}. Takes into account the
> * {@link CDOQueryInfo#getMaxResults()} and the {@link HibernateQueryHandler#FIRST_RESULT} values for paging.
> *
> * @param info
> * the object containing the query and parameters
> * @param context
> * the query results are placed in the context
> * @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
> */
> *public **void *executeQuery(CDOQueryInfo info, IQueryContext context)
> {
> *if *(!QUERY_LANGUAGE.equals(info.getQueryLanguage().toLowerCase ()))
> {
> *throw new *IllegalArgumentException("Query language " + info.getQueryLanguage() + " not supported by this store");
> }
>
> // get a transaction, the hibernateStoreAccessor is placed in a threadlocal
> // so all db access uses the same session.
> *final *Session session = hibernateStoreAccessor.getHibernateSession();
> *try*
> {
> // create the query
> *final *Query query = session.createQuery(info.getQueryString());
>
> // get the parameters with some parameter conversion
> *int *firstResult = -1;
> *for *(String key : info.getParameters().keySet())
> {
> *if *(key.toLowerCase().equals(FIRST_RESULT.toLowerCase()))
> {
> *final *Object o = info.getParameters().get(key);
> *if *(o != *null*)
> {
> *try*
> {
> firstResult = (Integer)o;
> }
> *catch *(ClassCastException e)
> {
> *throw new *IllegalArgumentException("Parameter firstResult must be an integer but it is a " + o
> + " class " + o.getClass().getName());
> }
> }
> }
> *else*
> {
> // in case the parameter is a CDOID get the object from the db
> *final *Object param = info.getParameters().get(key);
> *if *(param *instanceof *CDOIDHibernate)
> {
> *final *CDOIDHibernate hibernateId = (CDOIDHibernate)param;
> query.setEntity(key, session.get(hibernateId.getEntityName(), hibernateId.getId()));
> }
> *else*
> {
> query.setParameter(key, param);
> }
> }
> }
>
> // set the first result
> *if *(firstResult > -1)
> {
> query.setFirstResult(firstResult);
> }
>
> // the max result
> *if *(info.getMaxResults() != CDOQueryInfo.UNLIMITED_RESULTS)
> {
> query.setMaxResults(info.getMaxResults());
> }
>
> // and go for the query
> // future extension: support iterate, scroll through a parameter
> *for *(Object o : query.list())
> {
> *final **boolean *addOneMore = context.addResult(o);
> *if *(!addOneMore)
> {
> *return*;
> }
> }
> }
> *finally*
> {
> hibernateStoreAccessor.endHibernateSession();
> }
> }
>
> *public *HibernateStoreAccessor getHibernateStoreAccessor()
> {
> *return *hibernateStoreAccessor;
> }
>
> *public **void *setHibernateStoreAccessor(HibernateStoreAccessor hibernateStoreAccessor)
> {
> *this*.hibernateStoreAccessor = hibernateStoreAccessor;
> }
> }|
>
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
> http://twitter.com/eikestepper
>
>
>
> Kai Schlamp schrieb:
>> Hello.
>>
>> For perfomance reasons I need to do some direct SQL queries (regexp
>> search in the database). As there is no query implemented for CDO yet,
>> I thought of just connecting to the database directly (beside the CDO
>> connection) and query for the CDOID's directly.
>> What would be the best way to load the objects then with CDO?
>> Just use Transaction.getObject(cdoId, true)? Even if it is a very
>> large list of CDOID's (about 50000)?
>>
>> Regards,
>> Kai
|
|
|
Re: [CDO] Fetch multiple objects by CDOID [message #431310 is a reply to message #431309] |
Wed, 08 July 2009 07:36 |
|
Kai,
Note that there is:
248933: [DB] Support SQL as a CDOQuery language
https://bugs.eclipse.org/bugs/show_bug.cgi?id=248933
Cheers
/Eike
----
http://thegordian.blogspot.com
http://twitter.com/eikestepper
Kai Schlamp schrieb:
> Ok, sounds reasonable. I'll see what I can do.
>
> Eike Stepper wrote:
>> Kai,
>>
>> I strongly recommend to spend your time on efforts that are more
>> re-usable ;-)
>> Yesterday Martin implemented HQL for the HibernateStore within only 2
>> or 3 hours:
>>
>> |/**
>> * Implements server side HQL query execution..
>> * * @author Martin Taal
>> */
>> *public class *HibernateQueryHandler *implements *IQueryHandler
>> {
>> *public static final *String QUERY_LANGUAGE = "hql";
>>
>> *public static final *String FIRST_RESULT = "firstResult";
>>
>> *private *HibernateStoreAccessor hibernateStoreAccessor;
>>
>> /**
>> * Executes hql queries. Gets the session from the {@link
>> HibernateStoreAccessor} creates a hibernate query and sets
>> * the parameters taken from the {@link
>> CDOQueryInfo#getParameters()}. Takes into account the
>> * {@link CDOQueryInfo#getMaxResults()} and the {@link
>> HibernateQueryHandler#FIRST_RESULT} values for paging.
>> * * @param info
>> * the object containing the query and parameters
>> * @param context
>> * the query results are placed in the context
>> * @see IQueryHandler#executeQuery(CDOQueryInfo, IQueryContext)
>> */
>> *public **void *executeQuery(CDOQueryInfo info, IQueryContext context)
>> {
>> *if *(!QUERY_LANGUAGE.equals(info.getQueryLanguage().toLowerCase ()))
>> {
>> *throw new *IllegalArgumentException("Query language " +
>> info.getQueryLanguage() + " not supported by this store");
>> }
>>
>> // get a transaction, the hibernateStoreAccessor is placed in a
>> threadlocal
>> // so all db access uses the same session.
>> *final *Session session =
>> hibernateStoreAccessor.getHibernateSession();
>> *try*
>> {
>> // create the query
>> *final *Query query = session.createQuery(info.getQueryString());
>>
>> // get the parameters with some parameter conversion
>> *int *firstResult = -1;
>> *for *(String key : info.getParameters().keySet())
>> {
>> *if *(key.toLowerCase().equals(FIRST_RESULT.toLowerCase()))
>> {
>> *final *Object o = info.getParameters().get(key);
>> *if *(o != *null*)
>> {
>> *try*
>> {
>> firstResult = (Integer)o;
>> }
>> *catch *(ClassCastException e)
>> {
>> *throw new *IllegalArgumentException("Parameter
>> firstResult must be an integer but it is a " + o
>> + " class " + o.getClass().getName());
>> }
>> }
>> }
>> *else*
>> {
>> // in case the parameter is a CDOID get the object from the db
>> *final *Object param = info.getParameters().get(key);
>> *if *(param *instanceof *CDOIDHibernate)
>> {
>> *final *CDOIDHibernate hibernateId = (CDOIDHibernate)param;
>> query.setEntity(key,
>> session.get(hibernateId.getEntityName(), hibernateId.getId()));
>> }
>> *else*
>> {
>> query.setParameter(key, param);
>> }
>> }
>> }
>>
>> // set the first result
>> *if *(firstResult > -1)
>> {
>> query.setFirstResult(firstResult);
>> }
>>
>> // the max result
>> *if *(info.getMaxResults() != CDOQueryInfo.UNLIMITED_RESULTS)
>> {
>> query.setMaxResults(info.getMaxResults());
>> }
>>
>> // and go for the query
>> // future extension: support iterate, scroll through a parameter
>> *for *(Object o : query.list())
>> {
>> *final **boolean *addOneMore = context.addResult(o);
>> *if *(!addOneMore)
>> {
>> *return*;
>> }
>> }
>> }
>> *finally*
>> {
>> hibernateStoreAccessor.endHibernateSession();
>> }
>> }
>>
>> *public *HibernateStoreAccessor getHibernateStoreAccessor()
>> {
>> *return *hibernateStoreAccessor;
>> }
>>
>> *public **void *setHibernateStoreAccessor(HibernateStoreAccessor
>> hibernateStoreAccessor)
>> {
>> *this*.hibernateStoreAccessor = hibernateStoreAccessor;
>> }
>> }|
>>
>>
>> Cheers
>> /Eike
>>
>> ----
>> http://thegordian.blogspot.com
>> http://twitter.com/eikestepper
>>
>>
>>
>> Kai Schlamp schrieb:
>>> Hello.
>>>
>>> For perfomance reasons I need to do some direct SQL queries (regexp
>>> search in the database). As there is no query implemented for CDO
>>> yet, I thought of just connecting to the database directly (beside
>>> the CDO connection) and query for the CDOID's directly.
>>> What would be the best way to load the objects then with CDO?
>>> Just use Transaction.getObject(cdoId, true)? Even if it is a very
>>> large list of CDOID's (about 50000)?
>>>
>>> Regards,
>>> Kai
Cheers
/Eike
----
http://www.esc-net.de
http://thegordian.blogspot.com
http://twitter.com/eikestepper
|
|
|
Goto Forum:
Current Time: Thu Mar 28 08:34:30 GMT 2024
Powered by FUDForum. Page generated in 0.03587 seconds
|