Hibernate - One table with multiple entities?

我怕爱的太早我们不能终老 提交于 2019-12-10 15:46:27

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/9291969/hibernate-one-table-with-multiple-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!