Hi Jeremy,

If you want to execute queries by means of JDO, you can inject the 
IsisJdoSupport service, as of:


    // {{ injected: IsisJdoSupport
    protected IsisJdoSupport isisJdoSupport;

    public void injectIsisJdoSupport(final IsisJdoSupport isisJdoSupport) {
        this.isisJdoSupport = isisJdoSupport;
    }

    // }}


After that, you can use the service to execute queries against the database. As 
an example, one "generic" finder method is this one:

  @SuppressWarnings("unchecked")
    @Programmatic
    private <S extends AbstractXMSDomainObject> List<S> doFindByProp(final 
Class<S> clazz, final String whereClause, final Integer firstResult, final 
Integer maxResults, final String orderClause) {

        // Ensure any newly persisted objects are saved to the object store
        // before executing the query.
        this.getContainer().flush();

        // See this for examples:
        // 
http://www.datanucleus.org/products/accessplatform_2_1/jdo/query_api.html
        final Query query = 
this.isisJdoSupport.getJdoPersistenceManager().newQuery(clazz);

        // Always add the Tenant filter.
        String filter = String.format("tenantId == '%s'", 
this.userAccount.authenticatedUserTenantId());
        if ((whereClause != null) && (whereClause.trim().length() != 0)) {
            filter = filter.concat(" && ").concat(whereClause);
        }
        query.setFilter(filter);

        query.setRange(this.firstResultToLong(firstResult), 
this.maxResultsToLong(maxResults));
        final List<S> result = (List<S>) query.execute();
        return result;
    }


HTH,

Oscar




El 27/08/2013, a las 18:50, Jeremy Gurr <[email protected]> escribió:

> Hi Oscar,
> 
> That is nice to know, but I'm looking more for a direct sql lookup of a given 
> entity. I don't want to have to pull the list of all objects from my database 
> and iterate through them, since that will be much slower than a simple sql 
> statement with a where clause. I can't just do it with a prepared statement 
> since I will be searching on an arbitrary number of fields. I could just 
> compose a sql statement myself directly, but I'd prefer to use the JDO 
> framework and am wondering how to best access it through the isis utilities 
> (I'm guessing the DomainObjectContainer?)
> 
> thanks,
> Jeremy
> 
>> Hi, Jeremy.
>> 
>> In my case, the equivalent code in my domain would be:
>> 
>>   @Then("^.*the \"([^\"]*)\" risk register's risks collection should 
>> contain:$")
>>   public void 
>> then_riskregisters_risks_collection_should_contain(@Transform(RiskSpecTransformer.RiskRegister.class)
>>  final RiskRegister riskRegister, final List<RiskDesc> listOfExpecteds) 
>> throws Throwable {
>> 
>>       this.nextTransaction();
>> 
>>       final SortedSet<Risk> risks = riskRegister.getRisks();
>>       final ArrayList<Risk> risksList = Lists.newArrayList(risks);
>>       assertTableEquals(listOfExpecteds, risksList);
>> 
>>   }
>> 
>> 
>> Where the "assertTableEquals(xxx)" method is implemented on the 
>> "CukeGlueAbstract" class and compares by name each field on the spec, with 
>> the proper one on the class. Is this what you want to achieve?
>> 
>> 
>> Regards,
>> 
>> Oscar
>> 
> 

Reply via email to