Hibernate - single table with multiple objects?

I have a Picture :

 public class Picture implements java.io.Serializable { private byte[] picEncoded; private String Name; //etc 

Is it possible to move byte[] to another class without creating a physically partitioned table in db? Do I need to use some inheritance strategy?

change

Blob in a separate object:

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> 

Photo:

HBM:

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

How to insert new photos?

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

inserts a record where blob is null.

+4
source share
3 answers

Is it possible to move byte [] to another class without creating a physically partitioned table in db?

Use a component mapping that creates a compositional relationship 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 display to enable lazy loading of individual properties of a scalar value (somewhat exotic thing). Requires bytecode toolkit compiled constant classes for injection interception code. HQL with FETCH ALL PROPERTIES can be reevaluated.

Use lazy="no-proxy" for unambiguous associations to enable lazy fetching without using a proxy. Requires bytecode equipment to inject an interception code.

Use lazy="extra" for collections for "smart" collection behavior, i.e. some collection operations, such as size(), contains(), get(), etc. Do not initiate collection initialization. This is only reasonable for large collections.

See here for more information. retrieval strategies

Ed.

+4
source

If you are 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 definitely solve your goal.

+3
source

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 may require some editing, but the idea is this: You have a Picture class. This class has a name property and a pictureBlob property of type pictureBlob .

The component tag indicates that the properties within the component are mapped to the same table as the Picture

+1
source

Source: https://habr.com/ru/post/1396598/


All Articles