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, Sydney wrote:

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 at http://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.

Reply via email to