休眠 - 一个表与多个实体?
我有一个Picture
:休眠 - 一个表与多个实体?
public class Picture implements java.io.Serializable {
private byte[] picEncoded;
private String Name;
//etc
是它的可移动byte[]
另一个类,而不在数据库创建物理分隔的表?我需要使用一些继承策略吗?
编辑
斑点在独立的实体:
POJO:
public class PictureBlob implements java.io.Serializable {
private Integer pictureBlobId;
private byte[] blob;
HBM::
<class name="PictureBlob" table="PICTURE">
<id name="pictureBlobId" type="int">
<column length="200" name="PictureID"/>
</id>
<property name="blob" type="byte[]" insert="false" update="false">
<column name="PicEncoded" not-null="false"/>
</property>
</class>
图片:
HBM::
<one-to-one class="PictureBlob" constrained="true" name="pictureBlob" fetch="select"/>
如何插入新的图片?
PictureBlob pictureBlob= new PictureBlob();
pictureBlob.setBlob(new byte[]{84,32,22});
Picture p = new Picture();
p.setPictureBlob(pictureBlob);
session.save(p);
插入记录,其中BLOB值为null。
我想你可以使用这样的事情:
<class name="Picture">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<component name="pictureBlob" class="PictureBlob">
<property name="pictureBlobId"/>
<property name="blob"/>
<property name="picture"/>
</component>
</class>
这可能需要一些edititng,但这个想法是这样的: 你有一个Picture
类。本课程有name
和属性PictureBlob
。
的component
标签指示组件内的属性映射到同一个表作为Picture
感谢您的回答!这可以工作,但pictureBlob不会延迟加载(即使在组件标签中使用'lazy =“true”')。 – bunnyjesse112 2012-02-17 07:23:28
,如果你有兴趣使用注释,而不是HBM你可以看看这些
http://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html,这正是解决你的目的。
我是否需要字节码增强来使嵌入式对象延迟加载? – bunnyjesse112 2012-02-22 09:20:02
你不需要字节码增强我猜....还有一个注释基本的,我们可以指定获取类型。此链接有关嵌入式,嵌入式和基本注释的详细信息。 http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html你可以尝试结合这些和你实现你在想什么... – raddykrish 2012-02-23 06:14:35
是有可能的byte []移动到另一个类,而不在DB制作 物理上分离的表?
使用在图片和PictureBlob之间创建构图关系的构件映射。例如:
<hibernate-mapping>
<class name="Picture" table="PICTURE">
<id name="pictureId" type="int">
<generator class="native" />
</id>
<component name="pictureBlob " class="PictureBlob" lazy="no-proxy">
<property name="pictureBlobId" column="PictureID" type="int" length="200" />
<property name="blob" type="byte[]" insert="false" update="false"column="PicEncoded"/>
</component>
</class>
</hibernate-mapping>
POJO
public class Picture implements java.io.Serializable {
private int pictureId;
private PictureBlob pictureBlob;
//Setters & Getters
}
public class PictureBlob implements java.io.Serializable {
private int pictureBlobId;
private byte[] blob;
//Setters & Getters
}
另外注意:
使用
lazy="true"
上,且映射至使能懒惰 加载个人标量值类型属性的(一个有点奇特的 情况下)。需要编码的持久性 类的字节码插入用于注入拦截代码。可以在 HQL中使用FETCH ALL PROPERTIES重写。在单值关联上使用
lazy="no-proxy"
可以在不使用代理的情况下获取惰性 。要求用于注入拦截代码的字节码工具 。使用
lazy="extra"
在集合上的“智能”集合行为,即 收集的一些操作,如size(), contains(), get(),
等也 不会触发集合初始化。这只适用于非常大的收藏。
See here for more info. on fetching strategies
编辑。
sqlite没有blob类型。你可以切掉类型属性。休眠会找出它 – Firo 2012-02-15 10:56:55
删除类型=“字节[]”。没有改变。 – bunnyjesse112 2012-02-15 10:58:32
您是否禁用了图片类的延迟加载? – Firo 2012-02-15 11:41:27