Hibernate - 一张表包含多个实体?

发布于 2025-01-05 07:23:05 字数 1301 浏览 1 评论 0原文

我有一个图片

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>

图片:

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 的记录。

I have a Picture:

public class Picture implements java.io.Serializable {

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

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?

edit

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>

Picture:

hbm::

  <one-to-one class="PictureBlob" constrained="true" name="pictureBlob" fetch="select"/>

How do i insert new pictures?

PictureBlob pictureBlob= new PictureBlob();
        pictureBlob.setBlob(new byte[]{84,32,22});
        Picture p = new Picture();
        p.setPictureBlob(pictureBlob);           
        session.save(p);

inserts record where blob value is null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三五鸿雁 2025-01-12 07:23:05

是否可以将 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" 来启用惰性
加载单个标量值类型属性(有点奇怪
案件)。需要编译持久性的字节码检测
用于注入拦截代码的类。可以被覆盖
使用 FETCH ALL PROPERTIES 进行 HQL。

在单值关联上使用 lazy="no-proxy" 来启用惰性
不使用代理获取。需要字节码检测
用于注入拦截代码。

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

请参阅此处了解更多信息。关于获取策略

已编辑。

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

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
}

Also Note:

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.

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.

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.

See here for more info. on fetching strategies

Edited.

衣神在巴黎 2025-01-12 07:23:05

如果您有兴趣使用注释而不是 hbm,您可以查看这些

http://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html,这将完全解决您的目的。

if you interested in using annotations instead of hbm you can take a look at these

http://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html, this will exactly solve your purpose.

纵情客 2025-01-12 07:23:05

我认为你可以使用这样的东西:

<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 类型的属性 pictureBlob

component 标记表示组件内部的属性映射到与 Picture 相同的表

I think you could use something like this:

<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>

This might need some edititng, but the idea is this:
You have a Picture class. This class has property name and property pictureBlob of type PictureBlob.

the component tag indicates the properties inside the component are mapped to the same table as Picture

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文