gpt4 book ai didi

java - hibernate 鉴别器

转载 作者:行者123 更新时间:2023-11-30 04:14:57 25 4
gpt4 key购买 nike

我有类似的东西

table ACTION
name
resource_type
resource_id

resource_type和resource_id用于区分资源类型:

resource_type可以是imagevideo,如果resource_type包含image则resource_id是IMAGE表的id;如果resource_type包含videoresource_id是表VIDEO的id

这里是 IMAGE 和 VIDEO 表

table IMAGE (*)
id
imageType

table VIDEO (*)
id
videoType

(*) 表格更复杂,但为了更好的解释,我缩小了它!!!

我也有以下类(class)

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
}

我试图从本教程中了解有关鉴别器属性的一些内容: http://viralpatel.net/blogs/hibernate-inheritence-table-per-hierarchy-mapping/但例子有点不同...-_-'

我想在 Action 类的 Resource 对象中映射 IMAGE 和 VIDEO 表,如何使用 hibernate xml 映射器映射这些 pojo 类???

最佳答案

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

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

通过这种方法,您将在数据库中获得 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(),会产生以下输出:

Hibernate:创建表操作(NAME varchar(255)不为空,RESOURCE_TYPE varchar(255),RESOURCE_ID varchar(255),主键(NAME))Hibernate:创建表Image(ID varchar(255)不为空,imageProperty varchar(255),主键(ID))Hibernate:创建表Video(ID varchar(255)不为空,videoProperty varchar(255),主键(ID))

**** 行动和资源 Action (名称=imageAction资源=图像(id=i1 imageType=imageType)resourceType=image)操作(名称=videoAction资源=视频(id=v1 videoType=videoType)resourceType=video)

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

莎拉

关于java - hibernate 鉴别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18618546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com