【发布时间】:2017-04-06 17:02:29
【问题描述】:
我看到之前有很多关于堆栈溢出的同一件事以不同方式提出的问题。我在 Hibernate 论坛上查看了这个问题之一,他们提到它会起作用。我们可以参考这个link
基于此链接,延迟加载应该适用于基本属性类型,如 byte[]
我使用的是休眠版本 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 postgresql-9.4