Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to use a discriminator. You can use inheritance using the "Table per class" approach. Here's what you do.</p> <ol> <li>Define class "Resource" that has "id" property which is an id.</li> <li>Define two subclasses, "Image" and "Video" that extend Resource and add additional properties - in your example, "imageType" and "videoType".</li> <li>Define class "Action" that has a @ManyToOne to Resource. If you want the "resourceType" property here as you described in your post, you can have it, but it's completely unnecessary to get things to work.</li> <li>I'm not sure what type of id generation (if any) you want or what type of cascading from Action to Resource, so for simplicity's sake I assumed none for both of these.</li> </ol> <p>With this approach, you'll get 3 tables in the database.</p> <p>Here's the shell of an implementation:</p> <pre><code>@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 and setters } @Entity /* Table per Concrete Class */ @Table (name="Image") public class Image extends Resource { private String imageType; // Other properties unique to Image public Image() {} public Image(String id) { super(id); } public Image(String id, ...) { super(id); .... // setters } ... // getters and setters } /* Table per Concrete Class */ @Table (name="Video") public class Video extends Resource { private String videoType; // Other properties unique to Video public Video() {} public Video(String id) { super(id); } public Video(String id, ...) { super(id); .... // setters } ... // getters and setters } </code></pre> <p>With this approach, the following unit test shows the desired behavior:</p> <pre><code>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 and Resources"); List&lt;Action&gt; result1 = manager.createQuery( "from Action" ).getResultList(); for ( Action a : result1 ) { System.out.println(a); } manager.getTransaction().commit(); </code></pre> <p>Implementing toString() for all of these classes, the following output is produced:</p> <p>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))</p> <p><em><strong></em>****</strong> Actions and Resources Action( name=imageAction resource=Image( id=i1 imageType=imageType) resourceType=image) Action( name=videoAction resource=Video( id=v1 videoType=videoType) resourceType=video)</p> <p>Hope this helps to answer your question.</p> <p>Sarah</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload