I saw there are lot of questions asked earlier on the same thing in stack overflow in different ways. I looked at one of this issue on Hibernate forum and they mentioned it will work. We can refer this link
Based on this link, lazy loading should work for basic properties type like byte[]
I am using hibernate version 5.2.9 + postgresql DB
My entity model looks like this
@Entity
@Table
public class ResourceFileEntity {
@Id
@GeneratedValue
long id;
@Column
private String storageType;
@Column
private String path;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column
byte[] fileContent;
// removed getters/setter for readibility
}
Code to fetch the entity is
public ResourceFileEntity fetchEntity(long jId) throws IOException {
Session session = factory.openSession();
ResourceFileEntity entity = null;
Transaction tx = null;
try {
tx = session.beginTransaction();
entity = session.find(ResourceFileEntity.class, jId);
System.out.println(Hibernate.isPropertyInitialized(entity, "fileContent" ));
tx.commit();
} catch (HibernateException e) {
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return entity;
}
A lot of folks have mentioned about bytecode enhancement, i did try that putting up all the required details in my project build.gradle and using @LazyGroup but still no luck.
Any input on this will be of great help!