休眠-具有多个实体的一个表? [英] Hibernate - One table with multiple entities?

查看:85
本文介绍了休眠-具有多个实体的一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Picture:

public class Picture implements java.io.Serializable {

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

是否可以将byte[]移至另一个类,而无需在db中创建物理上分离的表?我需要使用一些继承策略吗?

Is it's possible to move byte[] to another class without creating physically separated table in db? Do i need to use some inheritance strategy?

修改

单独实体中的Blob:

Blob in separate entity:

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.

inserts record where blob value is null.

推荐答案

是否可以在不创建字节的情况下将byte []移至另一个类 db中物理上分开的表?

Is it's possible to move byte[] to another class without creating physically separated table in db?

使用组件映射在Picture和PictureBlob之间创建构图关系.示例:

Use component mapping which creates a composition relation between Picture and PictureBlob. Example:

<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"以启用惰性 单个标量值类型属性的加载(有点异国情调 案子).需要编译后的持久性的字节码检测 用于插入拦截代码的类.可以被覆盖 具有FETCH ALL PROPERTIES的HQL.

Use lazy="true" on , and mappings to enable lazy loading of individual scalar value-typed properties (a somewhat exotic case). Requires bytecode instrumentation of compiled persistent classes for the injection of interception code. Can be overriden in HQL with FETCH ALL PROPERTIES.

在单值关联上使用lazy="no-proxy"以启用惰性 无需使用代理即可进行获取.需要字节码检测 注入拦截代码.

Use lazy="no-proxy" on single-valued associations to enable lazy fetching without the use of a proxy. Requires bytecode instrumentation for the injection of interception code.

在集合上使用lazy="extra"来实现智能"集合行为,即 一些收集操作,例如size(), contains(), get(),等. 不触发集合初始化.这仅是非常明智的 大量收藏.

Use lazy="extra" on collections for "smart" collection behavior, i.e. some collection operations such as size(), contains(), get(), etc. do not trigger collection initialization. This is only sensible for very large collections.

有关详细信息,请参见此处.关于获取策略

已编辑.

这篇关于休眠-具有多个实体的一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆