Hi:

Let say we have two classes with a many-to-many relation: Item and Category.
Only the Item owns the relation, while the Category is not aware of the Item
(unidirectional). Both entities use a Key as their primary key which is
generated in a naive way.

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Item {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
  @Persistent
  private String name;
  @Persistent
  private Set<Key> categories;
}

    And the category class

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Category {
  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  private Key key;
  @Persistent
  private String name;
}



Now, in the DAO class, there are two methods: one that saves the items and
categories and one that lists them. Note that I’m first checking if this
object exists by catching the JDOObjectNotFoundException exception which may
not be the best choice.

public String save(String itemName, String categryName) {
    PersistenceManager pm = PMF_INSTANCE.getPersistenceManager();
    try {
      KeyFactory.Builder keyBuilder = new KeyFactory.Builder(Item.class
          .getSimpleName(), itemName);
      keyBuilder.addChild(Category.class.getSimpleName(), categryName);
      Key categoryKey = keyBuilder.getKey();

      Category category = null;
      try {
        category = pm.getObjectById(Category.class, categoryKey);
      } catch (JDOObjectNotFoundException e) {
        category = new Category();
        category.setKey(categoryKey);
      }
      category.setName(categryName);
      pm.makePersistent(category);
      System.out.println("Category Key: " + categoryKey);

      Key itemKey = KeyFactory.createKey(Item.class.getSimpleName(),
          itemName);
      Item item = null;
      try {
        item = pm.getObjectById(Item.class, itemKey);
      } catch (JDOObjectNotFoundException e) {
        item = new Item();
        item.setKey(itemKey);
      }
      item.setName(itemName);
      item.addCategory(categoryKey);
      pm.makePersistent(item);
    } catch (Exception e) {
      e.printStackTrace();
      return "Failed";
    } finally {
      pm.close();
    }


    return "Done";
  }



The list method is very simple:



@SuppressWarnings("unchecked")
  public List<String> list() {
    PersistenceManager pm = PMF_INSTANCE.getPersistenceManager();
    List<String> list = new ArrayList<String>();
    try {
      Query query = pm.newQuery(Item.class);
      List<Item> results = (List<Item>) query.execute();
      for (Item i : results) {
        list.add(String.valueOf(i));
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      pm.close();
    }


    return list;
  }





Will try to elaborate on this example tomorrow if I manage some time.



Hope this helps,

Albert Attard

Charles de 
Gaulle<http://www.brainyquote.com/quotes/authors/c/charles_de_gaulle.html>
- "The better I get to know men, the more I find myself loving dogs."

2009/8/31 objectuser <kevin.k.le...@gmail.com>

>
> I think it depends on the queries you want to support.  In GAE it's
> usually important to know that up-front.  Also, the specific trouble
> you're having might help.  Is it a modeling issue, a querying issue,
> just an issue with the APIs ...?
>
> I have some stuff here (the "querying" posts) that might be relevant
> to you (it's JPA, but the models should be the same):
>
> http://objectuser.wordpress.com/google-app-engine/
>
> Without knowing the queries though, something like this should cover
> the basic relationships:
>
> class Beverage {
>  Long id;
>  Key packaging;
>  Key district;
>  List<Key> materials;
> }
>
> On Aug 31, 10:46 am, funkforce <funkfo...@gmail.com> wrote:
> > Hi,
> >
> > No it doesnt really help me. I understand that I have to manage the
> > relationships myself but I cant really get it working. So an example
> > would be nice.
> >
> > Thanks
> >
> > FF
> >
> > On Aug 31, 5:31 pm, Albert Attard <albertatt...@gmail.com> wrote:
> >
> > > Hi:
> > > Quote from the page:
> http://code.google.com/appengine/docs/java/datastore/relationships.ht...
> >
> > > "The App Engine implementation of JDO does not yet implement this
> facility,
> > > but don't worry, you can still manage these relationships using Key
> values
> > > in place of instances (or Collections of instances) of your model
> objects."
> >
> > > Hope this helps, even though it's not much
> > > Albert Attard
> >
> > > Ogden Nash <
> http://www.brainyquote.com/quotes/authors/o/ogden_nash.html>  -
> > > "The trouble with a kitten is that when it grows up, it's always a
> cat."
> >
> > > 2009/8/31 funkforce <funkfo...@gmail.com>
> >
> > > > Hi
> >
> > > > Can someone post a full example of working code with unowned
> > > > relationships? It semas that several pople are havning problems with
> > > > this on I cant find any solution posted in this group yet.
> >
> > > > I think that I must use unowned relationships in my code. If I am
> > > > mistaken please correct me.
> >
> > > > I have a beverage class that has a packaging class ,a district class
> > > > and a Set of materials.
> > > > A beverage can have one type of packaing, one type of district ans
> > > > several materials.
> > > > Packaing can belong to several beverages.
> > > > District can belong to several beverages.
> > > > Materials can belong to several beverages.
> >
> > > > Any thoughts?
> >
> > > > Thnkas
> >
> > > > FF- Hide quoted text -
> >
> > > - Show quoted text -
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 google-appengine-java@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Attachment: Category.java
Description: Binary data

Attachment: Item.java
Description: Binary data

Attachment: DAOServiceImpl.java
Description: Binary data

Reply via email to