我看到之前在堆栈溢出中以不同的方式对同一件事提出了很多问题。我在 Hibernate 论坛上查看了这个问题之一,他们提到它会起作用。大家可以引用这个link
基于此链接,延迟加载应该适用于像 byte[] 这样的基本属性类型
我正在使用 hibernate 版本 5.2.9 + postgresql DB
我的实体模型如下所示
@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
}
获取实体的代码是
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;
}
很多人都提到了字节码增强,我确实尝试在我的项目 build.gradle 中添加所有必需的详细信息并使用@LazyGroup,但仍然没有成功。
对此的任何意见都会有很大帮助!
最佳答案
关于java - Hibernate 5.2.9 :@Basic(fetch = FetchType. LAZY) 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43261550/