我正在使用 Hibernate查询表我得到错误
"Exception in thread
"Thread-4" java.lang.OutOfMemoryError: Java heap space at
java.util.Arrays.copyOf(Arrays.java:3222) at
java.util.Arrays.copyOf(Arrays.java:3222)".
这是我的示例代码。
public List<MyBean> loadData() {
Session s=CFactory.getSession();
s.beginTransaction();
List<MyBean> mybean=null;
try {
Query sry=s.createQuery("from MyBean where flag='NP'");
mybean=sry.list();
s.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
return mybean;
}
我的问题是如何在具有大量数据的表中查询并将其转换为列表而不会在hibernate中出现内存错误?
Exception in thread "Thread-4" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3222)
at java.util.Arrays.copyOf(Arrays.java:3222)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
at org.hibernate.type.descriptor.java.DataHelper.extractBytes(DataHelper.java:170)
at org.hibernate.type.descriptor.java.DataHelper.extractBytes(DataHelper.java:170)
at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDescriptor.java:124)
at org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor.wrap(PrimitiveByteArrayTypeDesciptor.java:41)
at org.hibernate.type.descriptor.sql.BlobTypeDescriptor$5.doExtract(BlobTypeDescriptor.java:115)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:254)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:250)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:230)
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:331)
答案 0 :(得分:2)
我认为你不应该一次将大表加载到内存中,使用分页是必须的。
答案 1 :(得分:2)
您可以使用
Query sry=s.createQuery("from MyBean where flag='NP'")
.setFirstResult(start).setMaxResults(pageSize);
用于分页。
或者你可以increase heap size