具有多个实体的一个表

具有多个实体的一个表

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

问题描述

我有一个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.

推荐答案

使用组件映射在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="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.

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

已编辑.

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

08-21 04:04