Note that there are some explanatory texts on larger screens.

plurals
  1. POGoogle App Engine: Storing unowned many to many relationship
    primarykey
    data
    text
    <p>I have two objects a user and a role which I make persistant using JDO and googles app engine. The two datatypes are related to each other as unowned many to many relationship. I tried to model that much as in the gae-tutorial desribed as sets holding the key of the corresponding object. By the time I create my objects, those keys are null. So in order to get some keys generated I make those objects persistant. After that, I add the refering keys to those objects. However those keys are not getting stored in the end.</p> <p>Beside setting the keys, I also manipulate some other attributes (after makePersistent). Those changes later are reflected in the datastore. However, all the key changes I make after makePersistent, do not make it to the datastore. If I set those keys before makePersistent, they get stored as they should. However, that is no solution, as at least one object must logically receive the key refrence after it was made persistant.</p> <p>Below some sample code, to explain the issue. </p> <p>What is good practice to store those keys?</p> <p>Persistable Role class</p> <pre><code>public class Role { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key Role_id; @Persistent private Set&lt;Key&gt; Users = new HashSet&lt;Key&gt;(); ... } </code></pre> <p>Persistable User class</p> <pre><code>public class User { @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key User_id; @Persistent private String EMail = null; @Persistent private Set&lt;Key&gt; Roles = new HashSet&lt;Key&gt;(); ... } </code></pre> <p>Code extract which creates 1 Role and 1 User and tries to set the key-references on both sides. They keys changs don't end up in the datastore. However, the change on the email address is written to the data store. ...</p> <pre><code> PersistenceManager pm = PMF.get().getPersistenceManager(); Role GlobalAdmin = new Role(); User Daniel = new User(); try { pm.makePersistent(GlobalAdmin); pm.makePersistent(Daniel); } catch (Exception e){ System.out.println("Storing failed: " + e.getMessage()); } GlobalAdmin.addUser(Daniel.getUser_id()); Daniel.addRole(GlobalAdmin.getRole_id()); Daniel.setEMail("a@b.com"); pm.close(); ... </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload