java - How to handle multiple parent types with JPA? -
i've inherited system has several tables of form this:
create table notes (     id int primary key,     note text,     parent_id int,     parent_type varchar );   basically, idea have several other types, "tickets" , "widgets", , if want add note ticket 123, you'd do:
insert notes (note, parent_id, parent_type)     values ('blah blah', 123, 'ticket');   is there sensible way have jpa create @onetomany relationships from, say, ticket note schema?
or need split notes table out separate ticket_notes, widgets_notes, etc tables?
would possible create separate ticketnotes, widgetnotes, etc entities in java using @discriminatorcolumn, perhaps?
it seems taking advantage of discriminators , inheritance gets me want.
for example:
@entity class ticket {     @id     private integer id;      // ...      @onetomany(mappedby="ticket", fetch=fetchtype.lazy)     private list<ticketnotes> notes; }   @entity @inheritence @discriminatorcolumn(name="parent_type") public abstract class note {     @id     private integer id;      // ... }  @entity @discriminatorvalue("ticket") public class ticketnote extends note {     @manytoone     @joincolumn(name="parent_id")     private ticket ticket; }   helpful reference: http://en.wikibooks.org/wiki/java_persistence/inheritance
Comments
Post a Comment