views:

84

answers:

1

I’m new to object oriented database designs and I’m trying to understand how I should be structuring my classes in JDO for google app engine, particularly one to many relationships.

Let’s say I’m building a structure for a department store where there are many departments, and each department has many products. So I’d want to have a class called Department, with a variable that is a list of a Product class.

@PersistenceCapable 
public class Department { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private String deptID; 

    @Persistent 
    private String departmentName; 

    @Persistent 
    private List<Product> products; 

}

@PersistenceCapable 
public class Product { 
    @PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private String productID; 

    @Persistent 
    private String productName; 

}

But one Product can be in more than one Department (like a battery could be in electronics and household supplies). So the next question is, how do I not duplicate data in the OOD world and have only one copy of product data in numerous departments? And the next question is, let’s say I delete out a particular product, how do each of the departments know it was deleted?

+2  A: 

You need to add the following to the relevant member of the Department class. Also, see the section regarding this in the App Engine documentation.

@Persistent
@Element(dependent = "true")
private List<Product> products; 
Taylor Leese
Would you say that in my particular example i should really be using Unowned One-to-Many Relationships? http://code.google.com/appengine/docs/java/datastore/relationships.html#Unowned_Relationships
adam
If you need to know the department for a particular product then the bidirectional relationship makes that pretty easy. If you don't need to do that ever or very often then it may not be necessary. I don't really see any harm in it.
Taylor Leese