Flexible Relationship

9 views
Skip to first unread message

Sam Walker

unread,
Sep 24, 2011, 12:04:52 PM9/24/11
to play-fr...@googlegroups.com
This is what I currently have:

Alpha extends Model
@OneToMany
List<Child> children;

Bravo extends Model
@OneToMany
List<Child> children;

Child extends Model
// Either alpha or bravo will be specified
@ManyToOne
Alpha alpha;

@ManyToOne
Bravo bravo;

Can I do something like this?

Alpha implements HasChildren extends Model
@OneToMany
List<Child> children;

Bravo implements HasChildren extends Model
@OneToMany
List<Child> children;

Child extends Model
@Required
@ManyToOne
HasChildren parent; // Either Alpha or Bravo

adrian10

unread,
Sep 25, 2011, 4:29:08 AM9/25/11
to play-fr...@googlegroups.com
I usually do something like this:

Child extends Model
public Long parentId;
public String parentType;

parentId is the id of either Alpha or Bravo parent.
parentType is a String that can either be "Alpha" or "Bravo".

I skip the OneToMany, ManyToMany relationships. This works as long the child has only one parent.
And the child can have a lot of parent types.
Reply all
Reply to author
Forward
0 new messages