Hi,
I want to add the ability to attach comments to any entity in my system and I'd like to try and do it in a generic way. My initial thought was to create an entity comment table with these columns
-UserId //user id
-EntityId //entity id
-EntityType // in case entity id not unique for everything
-Comment //text comment
-Timestamp //comment timestamp
The table has no PK and the lifecycle of a comment is defined by its owning entity, so I would declare the collection of comments on my entity base class using
Code:
@CollectionOfElements
@JoinTable(name="`EntityComment`", joinColumns = @JoinColumn(name="`EntityId`"))
private List<EntityComment> comments=new ArrayList<EntityComment>();
This works but (not suprisingly) doesn't include the EntityType column during the join.
Most of my entities have unique ids across the entire DB but I'd rather not require it (hence the addition of the 'EntityType' column). It is easy enough to manage the values in the 'EntityType' column as comments are created, but how can I get hibernate to include 'EntityType' in the join when it retrieves the comment list for an entity. I looked at using @Where(..) but I don't know the entity type at compile time since I'm declaring the collection using an annotcation in my entity base class. Is there a special keyword I can use in the @Where clause to refer to this entities type? Something like @Where(clause="`EntityType`=this.class")?
I have a feeling I may be SOL here, so I'd appreciate other suggestions for implementing this mechanism as well.