I used an extent to verify that a B with a B name exists.
Extent<B> e = pm.getExtent(B.class);
Iterator<B> it = e.iterator();
while (it.hasNext()) {
B rsb = it.next();
System.out.println(rsb.getName());
System.out.println(rsb.getKey());
}
e.closeAll();
The output:
B
agR0ZXN0chALEgFBIgFBDAsSAUIiAUIM
On Jan 27, 6:41 pm, Sydney <[email protected]> wrote:
> Since I declared the parameter with query.declareParameters("String
> nameParam");, the colon is not valid. As expected I got an exception
> org.datanucleus.store.query.QueryCompilerSyntaxException: Explicit
> parameters defined for query, yet implicit parameter syntax
> (":nameParam") found
>
> On Jan 27, 4:39 pm, Rusty Wright <[email protected]> wrote:
>
>
>
> > Do you need a colon in front of nameParam?
>
> > query.setFilter("name == :nameParam");
>
> > Sydneywrote:
> > > The query returns no result.
>
> > > ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment
> > > ());
> > > ApiProxy.setDelegate(new ApiProxyLocalImpl(new File("."))
> > > {
> > > });
> > > A a = new A("A");
> > > B b = new B("B");
> > > a.getBs().add(b);
> > > PersistenceManagerFactory pmf =
> > > JDOHelper.getPersistenceManagerFactory("transactions-optional");
> > > PersistenceManager pm = pmf.getPersistenceManager();
> > > Transaction tx = pm.currentTransaction();
> > > try {
> > > tx.begin();
> > > pm.makePersistent(a);
> > > tx.commit();
> > > } finally {
> > > if (tx.isActive()) {
> > > tx.rollback();
> > > }
> > > }
>
> > > System.out.println(a.getKey());
> > > A ap = pm.getObjectById(A.class, "A");
> > > System.out.println(ap.getKey());
> > > Query query = pm.newQuery(B.class);
> > > query.setFilter("name == nameParam");
> > > query.declareParameters("String nameParam");
> > > List<B> rs = (List<B>) query.execute("B");
> > > System.out.println(rs.size());
>
> > > On Jan 23, 12:20 am, John Patterson <[email protected]> wrote:
> > >> Execute a query on the name property of B rather than loading it by
> > >> key. If the name is unique the result should only contain one B.
>
> > >> On 23 Jan 2010, at 11:41,Sydneywrote:
>
> > >>> I have a one to many relationship. There is a problem in the way I
> > >>> query for a B object. I was wondering what is the best way (most
> > >>> efficient) to do that.
> > >>> A a = new A("A");
> > >>> B b = new B("B");
> > >>> a.getBs().add(b);
> > >>> pm.makePersistent(a); // it's done inside a transaction
> > >>> A ap = pm.getObjectById(A.class, "A"); // FINE
> > >>> B bp = pm.getObjectById(B.class, "B"); // NOT FOUND Exception
> > >>> I guess B is not found because the real key is not B but B + A key.
> > >>> How do you get the B object when you don't know nothing about A?
> > >>> public class A {
> > >>> �...@primarykey
> > >>> �...@persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > >>> �...@extension(vendorName = "datanucleus", key = "gae.encoded-pk",
> > >>> value = "true")
> > >>> private String key;
> > >>> �...@persistent
> > >>> �...@extension(vendorName = "datanucleus", key = "gae.pk-name", value
> > >>> = "true")
> > >>> private String name;
> > >>> �...@persistent(mappedBy = "a")
> > >>> private List<B> bs;
> > >>> public A(String name) {
> > >>> this.name = name;
> > >>> bs = new ArrayList<B>();
> > >>> }
> > >>> ... Getter/Setter
> > >>> }
> > >>> @PersistenceCapable(identityType =
> > >>> javax.jdo.annotations.IdentityType.APPLICATION)
> > >>> public class B {
> > >>> �...@primarykey
> > >>> �...@persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
> > >>> �...@extension(vendorName = "datanucleus", key = "gae.encoded-pk",
> > >>> value = "true")
> > >>> private String key;
> > >>> �...@persistent
> > >>> �...@extension(vendorName = "datanucleus", key = "gae.pk-name", value
> > >>> = "true")
> > >>> private String name;
> > >>> �...@persistent
> > >>> private A a;
> > >>> public B(String name) {
> > >>> this.name = name;
> > >>> }
> > >>> ... Getter/Setter
> > >>> }
> > >>> --
> > >>> You received this message because you are subscribed to the Google
> > >>> Groups "Google App Engine for Java" group.
> > >>> To post to this group, send email to
> > >>> [email protected]
> > >>> .
> > >>> To unsubscribe from this group, send email to
> > >>> [email protected]
> > >>> .
> > >>> For more options, visit this group
> > >>> athttp://groups.google.com/group/google-appengine-java?hl=en
> > >>> .
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.