[play-framework] Mixins & JPA

131 views
Skip to first unread message

Leif Singer

unread,
May 17, 2010, 11:07:36 AM5/17/10
to play-fr...@googlegroups.com
Hello!

I'm thinking about the best way to add generic capabilities to model classes. For example, I might have model classes names "User" and "Bookmark". Now, I want both of these to be "taggable". Later, I might want to add the "likable" capability to one or both of them.

I'm looking for a solution in Java that plays nice with JPA.

The best solution I've found so far is having the domain classes implement a "Taggable" / "Likable" interface and providing "AbstractTaggable" / "AbstractLikable" skeleton classes that contain default implementations the domain classes can use.

Still, in a domain class, I would need to manually create the interface methods and have them use the skeleton classes.

Does anyone have a best practice to share that would match this use case?

Thanks in advance
Leif

--
Leif Singer
http://leif.singr.org




--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Guillaume Bort

unread,
May 18, 2010, 12:33:39 PM5/18/10
to play-fr...@googlegroups.com
It's difficult to apply the mixins concept to Java. Scala could help
you to do that. But I'm not sure about the limitations of using scala
mixins and JPA.

Leif Singer

unread,
May 19, 2010, 2:12:15 PM5/19/10
to play-fr...@googlegroups.com
Hello,

> It's difficult to apply the mixins concept to Java. Scala could help
> you to do that. But I'm not sure about the limitations of using scala
> mixins and JPA.

I wanted to avoid Scala because I don't know about the Eclipse support. So I was now looking further at a solution in Java, and seemingly what I need are polymorphic associations in some of my model classes. However, I couldn't find any documentation on how to create them using Hibernate, let alone JPA.

I'd be willing to accept a Hibernate dependency if that's what it takes, but right now I don't see any solution at all.

Basically what I need is this:

interface Taggable
addTag(Tag tag)
removeTag(Tag tag)

Document implements Taggable
@OneToMany
List<Tagging> taggings
addTag(Tag tag) {...}
removeTag(Tag tag) {...}

User implements Taggable
@OneToMany
List<Tagging> taggings
addTag(Tag tag) {...}
removeTag(Tag tag) {...}

Tagging
@ManyToOne
Tag tag
@ManyToOne
Taggable taggable

Tag
@OneToMany
List<Tagging> taggings

The Tagging.taggable @ManyToOne assoiciation is where I run into problems, as JPA seemingly can only create associations to @Entity classes.

Again, any pointers as to how to solve this would be greatly appreciated.

Thanks in advance
Leif

>> I'm thinking about the best way to add generic capabilities to model classes. For example, I might have model classes names "User" and "Bookmark". Now, I want both of these to be "taggable". Later, I might want to add the "likable" capability to one or both of them.
>>
>> I'm looking for a solution in Java that plays nice with JPA.
>>
>> The best solution I've found so far is having the domain classes implement a "Taggable" / "Likable" interface and providing "AbstractTaggable" / "AbstractLikable" skeleton classes that contain default implementations the domain classes can use.
>>
>> Still, in a domain class, I would need to manually create the interface methods and have them use the skeleton classes.
>>
>> Does anyone have a best practice to share that would match this use case?

Guillaume Bort

unread,
May 19, 2010, 3:05:25 PM5/19/10
to play-fr...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages