JPA - an analog of LobCreator from sleep mode?

in pure hibernate I can do:

Blob blob= Hibernate.getLobCreator(sessionFactory.getCurrentSession()).createBlob(inputStream, len); 

How to do this in jpa (with hibernation as a provider)?

In pure sleep mode, I created a custom type for blobs that used the prepared setBinaryStream . This solution worked fine for me, and I'm looking for a way to migrate it to JPA.

+6
source share
3 answers

You can use the @Lob annotation for your permanent property ( Annotation Lob ):

 @Entity public class MyEntity { private byte[] content; ... @Lob public byte[] getContent() { return content; } public void setContent(byte[] newContent) { this.content = newContent; } } 

In your code, you can convert the stream to byte [] with the following code:

 @Transient public void setContentFromInputStream(InputStream is) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[1024]; int l = 0; do { l = is.read(buff); if (l > 0) { baos.write(buff, 0, l); } } while (l > 0); is.close(); baos.flush(); baos.close(); content = baos.toByteArray(); } 

@Lob annotation can also be used with String, in which case you will get CLOB in DB

You should pay attention to byte size [] to avoid OutOfMemoryError.

To use only streams, you must rely on a specific jdbc provider implementation. For example, if you use Hibernate> = 3.6, you can change their type MyEntity.content to Blob and write:

 MyEntity entity = new MyEntity(); Session session = (Session)entityManager.getDelegate(); Blob newContent = session.getLobHelper().createBlob(inputStream, len); entity.setContent(newContent); 

Hope this helps you.

+7
source

If you want to save any serialized object into a column, set the column type in the database as blob, use the SerializationHelper class provided by Hibernate, and put this code in the yout bean class.

 class MyBean{ @Column private byte[] object; @Transient public Object getObjectDeserialized() throws IOException, ClassNotFoundException { if(this.object == null) return new String(); // or another type return SerializationHelper.deserialize(this.object); } byte[] getObject() { return this.object; } void setObject(byte[] object) { this.object = object; } @Transient public void setObject(Object obj) throws IOException { if(obj != null) this.object = SerializationHelper.serialize((Serializable) obj); } } 
+1
source

xxxx, maybe you can convey what you want.

class XXXBean {

 @Column private byte[] object; @Transient public Object getObjectDeserialized() throws IOException, ClassNotFoundException { if(this.object == null) return new String(); // or another type return SerializationHelper.deserialize(this.object); } byte[] getObject() { return this.object; } void setObject(byte[] object) { this.object = object; } @Transient public void setObject(Object obj) throws IOException { if(obj != null) this.object = SerializationHelper.serialize((Serializable) obj); } 

}

0
source

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


All Articles