I figured it out.
In my code that builds the query expression I can include an object that is
part of a many-to-many relationship using:
ExpressionBuilder.anyOf( the relationship attribute name ).equal( the
relationship object );
So in my case of having the Person entity class that contains a many-to-many
relationship with Address entity called "addressIdCollection". I can take
an Address entity object with some variable name like "anAddressObject" and
use it in building my Person adhoc query using:
ExpressionBuilder eb .... the expression builder I have been working with
Expression addrExp =
eb.anyOf("addressIdCollection").equals(anAddressObject);
....then append it to my main query Expression exp ...
if ( exp != null ) {
exp = exp.and(addrExp);
}
then my query will include a test for the Address object in the Person
search.
sonavor wrote:
>
> Can you give me an example?
> Continuing with my example of the Person entity class, the Person entity
> has this relationship:
>
> @ManyToMany(mappedBy = "personIdCollection")
> private Collection<Address> addressIdCollection;
>
> In my construction of the adhoc Person query using the ExpressionBuilder
> how do I create an expression and add it to the rest of the query
> expression where I want to append:
>
> AND Person addressIdCollection contains some Address object
>
>
>
> Gordon Yorke-2 wrote:
>>
>> Great, You shouldn't need to access the JoinTable columns simply use
>> "anyOf()" on the ManyToMany relationship and TopLink will do the joins
>> for you. If you have non-relation fields on the table then you can
>> create DirectQueryKeys to represent the fields and then use the "get()"
>> functionality of the expression.
>> Using the TopLink Expression Framework is a great option especially in
>> your case where you want to create dynamic queries.
>> The TopLink Expression Framework is a specification extension provided
>> by TopLink. Other Providers may have similar functionality but will use
>> different interfaces and patterns.
>> The way you have chosen to create a query is a great way to create a
>> query.
>> --Gordon
>>
>> sonavor wrote:
>>> I was able to solve my ORDER BY issue using the Toplink
>>> ExpressionBuilder.
>>> To do that I had to switch from using -
>>> javax.persistence.Query q = (
>>> (oracle.toplink.essentials.ejb.cmp3.EntityManager)
>>> em.getDelegate()).createQuery(exp, Person.class);
>>>
>>> To using a Toplink ReadAllQuery -
>>> oracle.toplink.essentials.queryframework.ReadAllQuery readAllQuery = new
>>> ReadAllQuery(Person.class);
>>>
>>> Since the ExpressionBuilder, Expression and ReadAllQuery classes are all
>>> from oracle.toplink.essentials I am locking my solution to Toplink.
>>>
>>> Anyway, the dynamic query now works great with the ORDER BY that I
>>> wanted.
>>>
>>> The Expression building part of my code is identical to the previous
>>> posting. It is actually in its own method called
>>> "createAdhocAllPersonQuery". So with my main Expression object named
>>> "exp"
>>> built up the change in the query implementation is as shown below -
>>>
>>> Expression exp = createAdhocAllPersonQuery(srchPerson);
>>>
>>> ReadAllQuery readAllQuery = new ReadAllQuery(Person.class);
>>> readAllQuery.addAscendingOrdering("lastName");
>>> readAllQuery.addAscendingOrdering("firstName");
>>> readAllQuery.addAscendingOrdering("midInitial");
>>> readAllQuery.setSelectionCriteria(exp);
>>> readAllQuery.prepareForExecution();
>>>
>>> // Session is from
>>> oracle.toplink.essentials.sessions.Session;
>>> Session session = (
>>> (oracle.toplink.essentials.ejb.cmp3.EntityManager)
>>> em.getDelegate()).getActiveSession();
>>> if (session != null) {
>>> List<Person> results = (List)
>>> session.executeQuery(readAllQuery);
>>> if (results != null) {
>>> for (Iterator i = results.iterator(); i.hasNext();)
>>> {
>>> Person p = (Person) i.next();
>>> // do stuff with Person object here
>>> pList.add(p);
>>> }
>>> }
>>> }
>>>
>>>
>>>
>>
>>
>
>
--
View this message in context: http://www.nabble.com/Is-using-Toplink-ExpressionBuilder-a-good-choice-tp18645581p18727148.html
Sent from the java.net - glassfish persistence mailing list archive at Nabble.com.