You cannot establish an owned relationship between two objects after the
child object (User in this case) has already been persisted. This is because
the parent key is embedded inside the child key, and keys can't be modified
once an entity is persisted in the datastore. You can, however, use an
unowned relationship and just store a reference to the User entity's Key. I
think this is what you're looking to do, since unowned relationships can be
established after the entities have already been stored.
http://code.google.com/appengine/docs/java/datastore/relationships.html#Unowned_Relationships

- Jason

On Wed, Aug 26, 2009 at 7:26 AM, Yong Niu(Michael, USA--Houston) <
[email protected]> wrote:

> you have to   make the  user-article relationship  bidirectly befroe you
> persist the article,  like:
>
>
> article.getusers().add(user);
>
>
>
>
>
> On Wed, Aug 26, 2009 at 5:42 AM, midomarocain <[email protected]>wrote:
>
>>
>> i use three class   User , Type ,and Article :
>>
>> @PersistenceCapable(identityType = IdentityType.APPLICATION)
>> public class User {
>>
>>        @PrimaryKey
>>        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>>        private Key id;
>>
>>        @Persistent
>>        private String login;
>>
>>        @Persistent
>>        private Type type;
>>
>>        .......
>>
>> }
>>
>>
>> @PersistenceCapable(identityType = IdentityType.APPLICATION)
>> public class Type {
>>
>>    @PrimaryKey
>>    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>>    private Key key;
>>
>>        .......
>>
>> }
>>
>>
>> @PersistenceCapable(identityType = IdentityType.APPLICATION)
>> public class Article {
>>
>>    @PrimaryKey
>>   @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>>    private Long id;
>>
>>   @Persistent
>>    private User user;
>>
>>        .....
>> }
>>
>> --------------------------------
>> I use this method to save the article :
>>
>> public static Article save(Article article, Long userId) {
>>                Article result = null;
>>                PersistenceManager pm = PMF.get().getPersistenceManager();
>>        try {
>>                        // Getting the user by id
>>                        Query query = pm.newQuery(User.class, "id ==
>> idParam");
>>                        query.declareParameters("Long idParam");
>>                        List<User> users = (List<User>)
>> query.execute(userId);
>>                        User user = null;
>>                        if (!users.isEmpty()) {
>>                                user = users.get(0);
>>                        }
>>
>>                        article.setUser(user);
>>
>>                        result = pm.makePersistent(article);
>>        } finally {
>>            pm.close();
>>        }
>>        return result;
>>        }
>>
>>
>>
>> the exception :
>>
>> javax.jdo.JDOFatalUserException: Detected attempt to establish Article
>> (16) as the parent of User(14) but the entity identified by User(14)
>> has already been persisted without a parent.  A parent cannot be
>> established or changed once an object has been persisted.
>>        at
>> org.datanucleus.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException
>> (NucleusJDOHelper.java:354)
>>        at org.datanucleus.jdo.JDOPersistenceManager.jdoMakePersistent
>> (JDOPersistenceManager.java:674)
>>        at org.datanucleus.jdo.JDOPersistenceManager.makePersistent
>> (JDOPersistenceManager.java:694)
>>        ......
>>
>>
>>
>> thanks in advance
>>
>>
>> On 25 août, 17:14, objectuser <[email protected]> wrote:
>> > Can you tell us what the error is?  And maybe some snippits of code
>> > showing the relevant parts of your model and how it's mapped?
>> >
>> > On Aug 25, 8:08 am, midomarocain <[email protected]> wrote:
>> >
>> > > I have a relation between a User and Article
>> >
>> > > (1) User can have one or many article
>> >
>> > > (2) an article is owned by only one user
>> >
>> > > i'am intersted only by the relation (2)
>> >
>> > > the User is persisted
>> >
>> > > I create a new Article and i want relie it with an existant User
>> >
>> > > But i have a probleme when trying to persist the article object
>> >
>> > > my code is like
>> >
>> > > article.setUser(user);
>> >
>> > >  pm.makePersistent(article);
>> >
>> > > can any one help me please
>>
>>
>
>
> --
> Yong Niu (Michael)
> [email protected]
>
>
>
> >
>

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