It's possible to do anything in low-level API :)

Although your code wouldn't work because you don't have question key
available, and you need to include transaction parameter in your puts.

It would need to be something like

Transaction txn = dataStore.beginTransaction();
Key key = dataStore.put(txn, question);
Entity a1 = new Entity("Answer", key);
Entity a2 = new Entity("Answer", key);
dataStore.put(txn, a1);
dataStore.put(txn, a2);
txn.commit();

But it would probably make more sense to combine the answer puts:

Transaction txn = dataStore.beginTransaction();
Key key = dataStore.put(txn, question);
Entity a1 = new Entity("Answer", key);
Entity a2 = new Entity("Answer", key);
List<Entity> as = new ArrayList<Entity>(2);
as.add(a1);
as.add(a2);
dataStore.put(txn, as);
txn.commit();

On top of that, if you know what the keys should be, (if you're naming
them with strings), then you could probably do it more efficiently
with one put

Key questionKey = KeyFactory.createKey("Question", "question1");
Key answer1Key = KeyFactory.createKey(questionKey, "Answer",
"answer1");
Key answer2Key = KeyFactory.createKey(questionKey, "Answer",
"answer2");
Entity question = new Entity(questionKey);
Entity answer1 = new Entity(answer1Key);
Entity answer2 = new Entity(answer2Key);
List<Entity> entities = new ArrayList<Entity>(3);
entities.add(question);
entities.add(answer1);
entities.add(answer2);
Transaction txn = dataStore.beginTransaction();
dataStore.put(txn, entities);
txn.commit();

The other thing you need to make sure is that JDO understands low-
level API keys and relationships that you code and that persistance
manager doesn't rewrite them in some way. (I don't know an answer to
that).

Cheers.

On Jun 12, 11:03 am, luka <[email protected]> wrote:
> Hi,
>
> My application is currently using JDO, although for some stress
> testing
> I am trying to employ batch insert using low level API (since it is
> less practical to do it with JDO).
>
> I have JDO parent-child one to many model which I am trying to insert
> using the low level API  "Entity" model.
> So far I have succeeded to batch insert the entities although failed
> in making the relationship.
>
> Is it possible to accomplish a parent-child one to many relationship
> using Entity API ?
>
> Short Example:
> -------------------
> // One Question has Many Answers
>
> @PersistenceCapable (detachable = "true")
> public class Question implements Serializable     {
>
>     �...@primarykey
>     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>     @Extension(vendorName="datanucleus", key="gae.encoded-pk",
> value="true")
>     private String key;
>
>   �...@persistent(mappedBy = "question")
>     @Element(dependent = "true")
>     private List<Answer> answers;
>
>     ......
>
> }
>
> @PersistenceCapable (detachable = "true")
> public class Answer implements Serializable     {
>
>     @PrimaryKey
>     @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>     @Extension(vendorName="datanucleus", key="gae.encoded-pk",
> value="true")
>     private String key;
>
>     �...@persistent
>      private Question question;
>
>      ....
>
> }
>
> // Using low level API
>
> Entity question = new Entity("Question");
>
> Entity a1 = new Entity("Answer",question.getKey());
> ....
> Entity a2 = new Entity("Answer",question.getKey());
> ...
>
> DatastoreService dataStore =
> DatastoreServiceFactory.getDatastoreService();
> Transaction txn = dataStore.beginTransaction();
> dataStore.put(question);
> dataStore.put(a1);
> dataStore.put(a2);
> txn.commit();
>
> //END
>
> Thanks
> Luka

-- 
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