Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » [CDO] Fetch multiple objects by CDOID
[CDO] Fetch multiple objects by CDOID [message #431300] Tue, 07 July 2009 21:00 Go to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
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 #431307 is a reply to message #431300] Wed, 08 July 2009 06:24 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
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">&nbsp;</font><font color="#3f5fbf"> *&nbsp;Implements&nbsp;server&nbsp;side&nbsp ;HQL&nbsp;query&nbsp;execution.. </font><br>
<font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><br>
<font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font
color="#7f9fbf">@author&nbsp;</font><font color="#3f5fbf">Martin&nbsp;Taal</font><br>
<font color="#ffffff">&nbsp;</font><font color="#3f5fbf">*/</font><br>
<font color="#7f0055"><b>public&nbsp;class&nbsp;</b></font ><font
color="#000000">HibernateQueryHandler&nbsp;</font><font color="#7f0055"><b>implements&nbsp;</b></font><font
color="#000000">IQueryHandler</font><br>
<font color="#000000">{</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;static&nbsp;final&nbsp; </b></font><font
color="#000000">String&nbsp;QUERY_LANGUAGE&nbsp;=&nbsp; </font><font color="#2a00ff">"hql"</font><font
color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;static&nbsp;final&nbsp; </b></font><font
color="#000000">String&nbsp;FIRST_RESULT&nbsp;=&nbsp; </font><font color="#2a00ff">"firstResult"</font><font
color="#000000">;</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>private&nbsp;</b></font><font
color="#000000">HibernateStoreAccessor&nbsp;hibernateStoreAccessor; </font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#3f5fbf">/**</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf"> *&nbsp;Executes&nbsp;hql&nbsp;queries.&nbsp; Gets&nbsp;the&nbsp;session&nbsp;from&nbsp;th e&nbsp; </font><font
color="#3f3fbf">{@link&nbsp;HibernateStoreAccessor}&nbsp; </font><font
color="#3f5fbf"> creates&nbsp;a&nbsp;hibernate&nbsp;query&nbs p;and&nbsp;sets </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf"> *&nbsp;the&nbsp;parameters&nbsp;taken&nbsp;f rom&nbsp;the&nbsp; </font><font
color="#3f3fbf">{@link&nbsp;CDOQueryInfo#getParameters()} </font><font
color="#3f5fbf"> .&nbsp;Takes&nbsp;into&nbsp;account&nbsp;the </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font
color="#3f3fbf">{@link&nbsp;CDOQueryInfo#getMaxResults()}&nbsp; </font><font
color="#3f5fbf">and&nbsp;the&nbsp;</font><font color="#3f3fbf"> {@link&nbsp;HibernateQueryHandler#FIRST_RESULT}&nbsp ; </font><font
color="#3f5fbf">values&nbsp;for&nbsp;paging.</font ><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font
color="#7f9fbf">@param&nbsp;</font><font color="#3f5fbf">info</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;object &nbsp;containing&nbsp;the&nbsp;query&nbsp;an d&nbsp;parameters </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font
color="#7f9fbf">@param&nbsp;</font><font color="#3f5fbf">context</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf"> *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;query& amp;nbsp;results&nbsp;are&nbsp;placed&nbsp;in&am p;nbsp;the&nbsp;context </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*&nbsp;</font><font
color="#7f9fbf">@see&nbsp;</font><font color="#3f5fbf"> IQueryHandler#executeQuery(CDOQueryInfo,&nbsp;IQueryCont ext) </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#3f5fbf">*/</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;</b></font><font
color="#7f0055"><b>void&nbsp;</b></font><font color="#000000">executeQuery</font><font
color="#000000">(</font><font color="#000000"> CDOQueryInfo&nbsp;info,&nbsp;IQueryContext&nbsp; context </font><font
color="#000000">)</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">{</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>if&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>throw&nbsp;new&nbsp;</b></font ><font
color="#000000">IllegalArgumentException</font><font color="#000000">(</font><font
color="#2a00ff">"Query&nbsp;language&nbsp;"&nbsp; </font><font color="#000000">+&nbsp;info.getQueryLanguage</font><font
color="#000000">()&nbsp;</font><font color="#000000">+&nbsp;</font><font
color="#2a00ff">" &nbsp;not&nbsp;supported&nbsp;by&nbsp;this&a mp;nbsp;store "</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#3f7f5f"> //&nbsp;get&nbsp;a&nbsp;transaction,&nbsp;th e&nbsp;hibernateStoreAccessor&nbsp;is&nbsp;place d&nbsp;in&nbsp;a&nbsp;threadlocal </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#3f7f5f"> //&nbsp;so&nbsp;all&nbsp;db&nbsp;access& nbsp;uses&nbsp;the&nbsp;same&nbsp;session. </font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#000000"> Session&nbsp;session&nbsp;=&nbsp;hibernateStoreA ccessor.getHibernateSession </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>try</b></font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f">//&nbsp;create&nbsp;the&nbsp;query </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#000000"> Query&nbsp;query&nbsp;=&nbsp;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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f"> //&nbsp;get&nbsp;the&nbsp;parameters&nbsp;wi th&nbsp;some&nbsp;parameter&nbsp;conversion </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>int&nbsp;</b></font><font
color="#000000">firstResult&nbsp;=&nbsp;-</font><font color="#990000">1</font><font
color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>for&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">String&nbsp;key&nbsp;:&nbsp;info.getParameters </font><font
color="#000000">()</font><font color="#000000">.keySet</font><font
color="#000000">())</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#000000">Object&nbsp;o&nbsp;=&nbsp;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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">o&nbsp;!=&nbsp;</font><font
color="#7f0055"><b>null</b></font><font color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>try</b></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">firstResult&nbsp;=&nbsp;</font><font
color="#000000">(</font><font color="#000000">Integer</font><font
color="#000000">)</font><font color="#000000">o;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>catch&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">ClassCastException&nbsp;e</font><font
color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#7f0055"><b>throw&nbsp;new&nbsp;</b></font ><font
color="#000000">IllegalArgumentException</font><font color="#000000">(</font><font
color="#2a00ff">" Parameter&nbsp;firstResult&nbsp;must&nbsp;be& ;nbsp;an&nbsp;integer&nbsp;but&nbsp;it&nbsp; is&nbsp;a&nbsp; "&nbsp;</font><font
color="#000000">+&nbsp;o</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font
color="#000000">+&nbsp;</font><font color="#2a00ff">"&nbsp;class&nbsp;"&nbsp;</font ><font
color="#000000">+&nbsp;o.getClass</font><font color="#000000">()</font><font
color="#000000">.getName</font><font color="#000000">())</font><font
color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#7f0055"><b>else</b></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f"> //&nbsp;in&nbsp;case&nbsp;the&nbsp;parameter &nbsp;is&nbsp;a&nbsp;CDOID&nbsp;get&nbsp ;the&nbsp;object&nbsp;from&nbsp;the&nbsp;db </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#000000"> Object&nbsp;param&nbsp;=&nbsp;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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">param&nbsp;</font><font
color="#7f0055"><b>instanceof&nbsp;</b></font><font color="#000000">CDOIDHibernate</font><font
color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#000000">CDOIDHibernate&nbsp;hibernateId&nbsp;=&nbsp; </font><font
color="#000000">(</font><font color="#000000">CDOIDHibernate</font><font
color="#000000">)</font><font color="#000000">param;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">query.setEntity</font><font
color="#000000">(</font><font color="#000000">key,&nbsp;session.get</font><font
color="#000000">(</font><font color="#000000">hibernateId.getEntityName</font><font
color="#000000">()</font><font color="#000000">,&nbsp;hibernateId.getId</font><font
color="#000000">()))</font><font color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>else</b></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">query.setParameter</font><font
color="#000000">(</font><font color="#000000">key,&nbsp;param</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f">//&nbsp;set&nbsp;the&nbsp;first&nbsp;result </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">firstResult&nbsp;&gt;&nbsp;-</font ><font
color="#990000">1</font><font color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f">//&nbsp;the&nbsp;max&nbsp;result </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">info.getMaxResults</font><font
color="#000000">()&nbsp;</font><font color="#000000">!=&nbsp;CDOQueryInfo.UNLIMITED_RESULTS </font><font
color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </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"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f"> //&nbsp;and&nbsp;go&nbsp;for&nbsp;the&nb sp;query </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#3f7f5f"> //&nbsp;future&nbsp;extension:&nbsp;support& nbsp;iterate,&nbsp;scroll&nbsp;through&nbsp;a&am p;nbsp;parameter </font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>for&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">Object&nbsp;o&nbsp;:&nbsp;query.list </font><font
color="#000000">())</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#7f0055"><b>final&nbsp;</b></font><font
color="#7f0055"><b>boolean&nbsp;</b></font><font color="#000000">addOneMore&nbsp;=&nbsp;context.addResult </font><font
color="#000000">(</font><font color="#000000">o</font><font
color="#000000">)</font><font color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#7f0055"><b>if&nbsp;</b></font><font
color="#000000">(</font><font color="#000000">!addOneMore</font><font
color="#000000">)</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#7f0055"><b>return</b></font><font
color="#000000">;</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">}</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">}</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>finally</b></font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">{</font><br>
<font color="#ffffff"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">hibernateStoreAccessor.endHibernateSession </font><font
color="#000000">()</font><font color="#000000">;</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#000000">}</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;</b></font><font
color="#000000">HibernateStoreAccessor&nbsp;getHibernateStoreAccessor </font><font
color="#000000">()</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">{</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>return&nbsp;</b></font><font
color="#000000">hibernateStoreAccessor;</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">}</font><br>
<font color="#ffffff"></font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#7f0055"><b>public&nbsp;</b></font><font
color="#7f0055"><b>void&nbsp;</b></font><font color="#000000">setHibernateStoreAccessor</font><font
color="#000000">(</font><font color="#000000">HibernateStoreAccessor&nbsp;hibernateStoreAccessor </font><font
color="#000000">)</font><br>
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">{</font><br>
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font ><font color="#7f0055"><b>this</b></font><font
color="#000000"> .hibernateStoreAccessor&nbsp;=&nbsp;hibernateStoreAc cessor; </font><br>
<font color="#ffffff">&nbsp;&nbsp;</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--


Re: [CDO] Fetch multiple objects by CDOID [message #431309 is a reply to message #431307] Wed, 08 July 2009 07:35 Go to previous messageGo to next message
Kai Schlamp is currently offline Kai SchlampFriend
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 Go to previous message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
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


Previous Topic:EMF - Teneo-EclipseLink - @OneToMany
Next Topic:Re: [Teneo] NPE in PersistentStoreAdapter
Goto Forum:
  


Current Time: Thu Mar 28 08:34:30 GMT 2024

Powered by FUDForum. Page generated in 0.03587 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top