cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mike Kienenberger" <>
Subject Re: Custom SQL query - HowTo
Date Thu, 03 Aug 2006 12:20:23 GMT
Here's an example.   Note that the only reason you need to supply one
of the following (DataObject class, DataMap, ObjEntity, DbEntity, or
ObjEntity name) to SQLTemplate is so that it can find an appropriate
DataNode, which tells it where it get the JDBC connection information.
   The actual value supplied doesn't matter when you're fetching data
rows just so long as the supplied object is in the same data node as
where you want the query to run.

[Side note: And yes, I know the default sql on here is not
thread-safe, but it's not an issue in my dev environment, and getting
a duplicate value isn't going to cause any technical problems.]

private Integer fetchDocumentNumberAutoIncrement() {
        String defaultSql = "SELECT #result('max(DOCUMENT_NUMBER)+1'
    	String oracleSql = "SELECT
#result('SEQ_AuthDocDocumentNumber.NextVal' 'long' 'ID') FROM DUAL";
        SQLTemplate rawSelect = new SQLTemplate(getClass(), defaultSql);
        rawSelect.setTemplate(OracleAdapter.class.getName(), oracleSql);

        List list = getDataContext().performQuery(rawSelect);
        Map row = (Map)list.get(0);

        Number autoincrementID = (Number)row.get("ID");

        return new Integer(autoincrementID.intValue());

On 8/3/06, Ayhan Kondoz <> wrote:
> Hello,
> i need to make a custom SQL query where the resultSet doesn't match any
> of the dataMap's. My SQL query only returns a list of id's. Something
> like this
> SELECT FROM customer c WHERE .....
> And I want to iterate through this list of id's and do something with
> them.
> Could someone please tell me how to do this with cayenne? I tried to use
> SQLTemplate but it needs a DataMap or something similar to work but I
> don't have an appropriate DataMap!!!
> Then I tried to use a dataContext.performGenericQuery() but again I
> don't know how to create a proper Query object.
> Thx
> Ayhan Kondoz

View raw message