- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有类似的东西
table ACTION
name
resource_type
resource_id
resource_type和resource_id用于区分资源类型:
resource_type可以是image
或video
,如果resource_type包含image
则resource_id是IMAGE表的id;如果resource_type包含video
resource_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 类???
最佳答案
您不需要使用鉴别器。您可以使用“每个类一个表”的方法来使用继承。这就是你要做的。
通过这种方法,您将在数据库中获得 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/
我写了一段代码,在比特流中连续 6 个“1”后添加一个“0”。但是如何解码呢? 这里是一个比特流的例子: original = {01101111110111000101111110001100...
我是一名优秀的程序员,十分优秀!