java - Hibernate Discriminator -


i have that

table action name resource_type resource_id 

resource_type , resource_id used discriminate type of resource:

resource_type can image or video, if resource_type contains image resource_id id of table image; if resource_type contains video resource_id id of table video

here image , video tables

table image (*) id  imagetype  table video (*) id videotype 

(*) tables more complicated, better explanation shrank it!!!

i have following class, too

class action{     private resource resource;     //getter , setter }  interface resource{}  class image implements resource{     private integer id;     private string imagetype;     //getter , setter }  class video implements resource{     private integer id;     private string videotype;     //getter , setter } 

i tried understand discriminator attribute tutorial: http://viralpatel.net/blogs/hibernate-inheritence-table-per-hierarchy-mapping/ example little bit diffenet...-_-'

i want map image end video table in resource object of action class,how can map these pojo classes hibernate xml mapper???

you don't need use discriminator. can use inheritance using "table per class" approach. here's do.

  1. define class "resource" has "id" property id.
  2. define 2 subclasses, "image" , "video" extend resource , add additional properties - in example, "imagetype" , "videotype".
  3. define class "action" has @manytoone resource. if want "resourcetype" property here described in post, can have it, it's unnecessary things work.
  4. i'm not sure type of id generation (if any) want or type of cascading action resource, simplicity's sake assumed none both of these.

with approach, you'll 3 tables in database.

here's shell of implementation:

@entity  @inheritance (strategy=inheritancetype.table_per_class) public abstract class resource {      @id     @column(name="id")     private string id;      public resource() {}     public resource(string id)  { this.id = id;  }     ... // getters , setters }  @entity  /* table per concrete class */ @table (name="image")  public class image extends resource {      private string imagetype;  // other properties unique image      public image() {}     public image(string id) { super(id); }     public image(string id, ...) { super(id); .... // setters }     ... // getters , setters }  /* table per concrete class */ @table (name="video")  public class video extends resource {      private string videotype;   // other properties unique video     public video() {}     public video(string id) { super(id); }     public video(string id, ...) { super(id); .... // setters }     ... // getters , setters } 

with approach, following unit test shows desired behavior:

image image = new image("i1", "imagetype"); video video = new video("v1", "videotype");  action imageaction = new action("imageaction", image, "image"); action videoaction = new action("videoaction", video, "video");  manager.persist(image); manager.persist(video); manager.persist(imageaction); manager.persist(videoaction); manager.gettransaction().commit();  ...   manager.gettransaction().begin();  system.out.println("********** actions , resources"); list<action> result1 = manager.createquery( "from action" ).getresultlist(); ( action : result1 ) {     system.out.println(a); } manager.gettransaction().commit(); 

implementing tostring() of these classes, following output produced:

hibernate: create table action (name varchar(255) not null, resource_type varchar(255), resource_id varchar(255), primary key (name)) hibernate: create table image (id varchar(255) not null, imageproperty varchar(255), primary key (id)) hibernate: create table video (id varchar(255) not null, videoproperty varchar(255), primary key (id))

**** actions , resources action( name=imageaction resource=image( id=i1 imagetype=imagetype) resourcetype=image) action( name=videoaction resource=video( id=v1 videotype=videotype) resourcetype=video)

hope helps answer question.

sarah


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -