1

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???

4

1 回答 1

2

您不需要使用鉴别器。您可以使用“每个类的表”方法使用继承。这就是你要做的。

  1. 定义具有“id”属性的“资源”类,该属性是一个id。
  2. 定义两个子类,“Image”和“Video”,它们扩展 Resource 并添加其他属性 - 在您的示例中,“imageType”和“videoType”。
  3. 定义具有@ManyToOne 资源的类“Action”。如果您想要您在帖子中描述的“resourceType”属性,您可以拥有它,但完全没有必要让事情正常工作。
  4. 我不确定你想要什么类型的 id 生成(如果有的话)或者从 Action 到 Resource 的级联类型,所以为了简单起见,我假设这两个都没有。

使用这种方法,您将在数据库中获得 3 个表。

这是一个实现的外壳:

@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
}

使用这种方法,以下单元测试显示了所需的行为:

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<Action> result1 = manager.createQuery( "from Action" ).getResultList();
for ( Action a : result1 ) {
    System.out.println(a);
}
manager.getTransaction().commit();

为所有这些类实现 toString(),会产生以下输出:

休眠:创建表操作(NAME varchar(255)不为空,RESOURCE_TYPE varchar(255),RESOURCE_ID varchar(255),主键(NAME))休眠:创建表图像(ID varchar(255)不为空,imageProperty varchar(255) ), 主键 (ID)) Hibernate: 创建表 Video (ID varchar(255) not null, videoProperty varchar(255), 主键 (ID))

****动作和资源 Action(name=imageAction resource=Image(id=i1 imageType=imageType) resourceType=image) Action(name=videoAction resource=Video(id=v1 videoType=videoType) resourceType=video)

希望这有助于回答您的问题。

莎拉

于 2013-09-06T02:19:08.277 回答