I have something like that
table ACTION
name
resource_type
resource_id
resource_type and resource_id are used to discriminate type of resource:
resource_type can be image
or video
, if resource_type contains image
resource_id is id of the table IMAGE; if resource_type contains video
resource_id is id of the table VIDEO
here IMAGE and VIDEO tables
table IMAGE (*)
id
imageType
table VIDEO (*)
id
videoType
(*) tables are more complicated, but for better explanation I shrank it!!!
I have the following class, too
class Action{
private Resource resource;
//getter and setter
}
interface Resource{}
class Image implements Resource{
private Integer id;
private String imageType;
//getter and setter
}
class Video implements Resource{
private Integer id;
private String videoType;
//getter and setter
}
i tried to understand something about discriminator attribute from this tutorial: http://viralpatel.net/blogs/hibernate-inheritence-table-per-hierarchy-mapping/ but example is little bit diffenet...-_-'
I want map IMAGE end VIDEO table in Resource object of Action class,how I can map these pojo classes with hibernate xml mapper???
You don't need to use a discriminator. You can use inheritance using the "Table per class" approach. Here's what you do.
With this approach, you'll get 3 tables in the database.
Here's the shell of an implementation:
With this approach, the following unit test shows the desired behavior:
Implementing toString() for all of these classes, the following output is 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 and Resources Action( name=imageAction resource=Image( id=i1 imageType=imageType) resourceType=image) Action( name=videoAction resource=Video( id=v1 videoType=videoType) resourceType=video)
Hope this helps to answer your question.
Sarah