Hibernate 5.2.9:@Basic(fetch = FetchType.LAZY) is not working

时间:2017-04-06 17:02:29

标签: java hibernate postgresql-9.4

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!

1 个答案:

答案 0 :(得分:1)

您可能会遇到HHH-10929。虽然尚未通过使用Hibernate template的自包含测试用例来证明这一点。如果您认为存在问题,可能值得创建一个吗?