1

I am trying to model my objects in a way that would work both with hibernate JPA and GAE JPA. I have a class Item that has a list of properties. I need to find all the items with specific properties. For example:

@Entity
public class Item {
    @Id
    public Long id;

    @ManyToMany(name="properties")
    public List<Property> props; 
}

@Entity
public class Property {
    @Id
    public Long id;
    public String value;
}

and my query would be like

select i from Item i join i.props p where p.id=1 and p.id=3 and p.id=10 

in which 1, 3, and 10 are the ids of the properties I am looking for. For example I can have book items and look for these properties: Property(1, "category:book") Property(3, "text:anatomy"), Property(10, "text:brain"). You can think of the properties as tags for Items.

JAE JPA doesn't support ManyToMany relationship. Any suggestion how to model this?

Thank you in advance.

danial
  • 4,058
  • 2
  • 32
  • 39

2 Answers2

0

GAE JPA obviously does support unowned M-N relationships, but then there is nothing M-N in your model (i.e Property doesn't know Item). You have a collection field so you mark it as @OneToMany. The query would be more problematic due to how little GAE allows in queries. (i.e not much in the way of joins)

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
0

Here is the solution I ended up with:

@Entity
public class Item implements Serializable {
    @Id
    public Long id;

    @Unowned
    @OneToMany(cascade = CascadeType.ALL)
    @Column(name = "properties")
    public Set<Property> properties = new HashSet<Property>();

    @ElementCollection
    public Set<Long> propIds = new HashSet<Long>();
}

@Entity
public class Property implements Serializable {
    @Id
    public Long id;
    public String value;
}

and query is like this:

select i from Item i where 1 member of i.propIds and 3 member of i.propIds

This will return all items that I need. Then I can access the properties through item.properties.

danial
  • 4,058
  • 2
  • 32
  • 39