Hibernate - 单个表格用于多个实体?

5

我有一张图片

public class Picture implements java.io.Serializable {

    private byte[] picEncoded;
    private String Name;
    //etc

有没有可能在不在数据库中创建物理分离表的情况下将byte[]移动到另一个类中?我需要使用一些继承策略吗?

编辑

在单独的实体中使用Blob:

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>

Picture:

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值为空。


1
SQLite没有blob类型。您可以删除类型属性,Hibernate会自动识别。 - Firo
移除了 type="byte[]",没有做其他更改。 - bunnyjesse112
1
你已经为Picture类禁用了懒加载吗? - Firo
没有关联到其他实体的情况下,通常会按惯例进行延迟加载。 - bunnyjesse112
3个回答

4
能否通过组件映射将byte[]移动到另一个类中而不创建在数据库中物理上分离的表呢?使用创建Picture和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() 等不会触发集合初始化。这只适用于非常大的集合。

更多提取策略信息请点击此处

已编辑.


3

我需要字节码增强来使嵌入对象实现延迟加载吗? - bunnyjesse112
我猜你不需要字节码增强...还有一个注解Basic,我们可以指定获取类型。这个链接详细介绍了Embedded、Embeddable和Basic注解。http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/entity.html 你可以尝试将它们组合起来,实现你想要的效果... - raddykrish

1
我认为你可以使用类似这样的代码:
<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>

这可能需要一些编辑,但是思路是这样的: 您有一个名为Picture的类。该类具有属性name和类型为PictureBlob的属性pictureBlobcomponent标记指示组件内部的属性映射到与Picture相同的表。

谢谢回答!这个方法可行,但是pictureBlob没有被懒加载(即使在组件标签中加入了“lazy = true”)。 - bunnyjesse112

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接