我已经将 PostgreSQL 中的 OID 列映射到 Hibernate 中的 Blob。这样我就可以创建新行来存储二进制数据并将其取回。但是当我尝试通过 hibernate 更新同一行或删除该行时,出现上述错误。我正在使用 Hibernate 3.2 和 PostgreSQL 驱动程序 9.2-1001 jdbc4 驱动程序。基于阅读 hibernate bug with reading oid type,我已经升级到 Hibernate 3.6.10 但仍然遇到相同的错误。
现在在做研究的时候,我发现hibernate有一些问题,OID类型的PG jdbc驱动程序无法检索数据。有人建议我们在hibernate中使用“byte[]”作为映射类型,并将列更改为“bytea”。但我不想映射到 byte[],因为它会读取 memroy 中的整个二进制内容。
2012-11-16 13:36:32,614 [main] INFO Cache - No object present with id=1295
org.hibernate.exception.GenericJDBCException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:160)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:81)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1473)
at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:345)
at $Proxy0.beginTransaction(Unknown Source)
at x.y.objectstore.dao.TransactionManager.begin(TransactionManager.java:53)
at x.y.objectstore.dao.BaseStore.load(BaseStore.java:122)
at x.y.objectstore.dao.BaseStore.load(BaseStore.java:110)
at x.y.objectstore.ConceptInstanceImpl.getContainerBean(ConceptInstanceImpl.java:284)
at x.y.objectstore.deletion.PropertyInstanceDeletionTask.run(PropertyInstanceDeletionTask.java:53)
at x.y.objectstore.deletion.DeletionService.executeDeletionTask(DeletionService.java:56)
at x.y.objectstore.ConceptInstanceImpl.deletePropertyInstance(ConceptInstanceImpl.java:726)
at x.y.objectstore.ConceptInstanceImpl.removeProperty(ConceptInstanceImpl.java:719)
at x.y.objectstore.tests.PropertyEditTests.testEditBinaryProperty(PropertyEditTests.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.postgresql.util.PSQLException: Cannot change transaction isolation level in the middle of a transaction.
at org.postgresql.jdbc2.AbstractJdbc2Connection.setTransactionIsolation(AbstractJdbc2Connection.java:929)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:126)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
... 36 more
更新:
只是尝试遵循@a_horse_with_no_name 的建议。 尝试使用 bytea 列并关注此 Hibernate, Postgresql: Column "x" is of type oid but expression is of type byte创建修改后的 postgresql 方言,但现在出现错误 插入行时出现“'binaryValue' 列的类型为 bytea,但表达式的类型为 bigint”。
最佳答案
最后我通过@a_horse_with_no_name 的建议解决了我的问题。我将列更改为 bytea
,并将 bean 属性更改为 byte[]
。我保留了方言。我不不再将Blob
与PostgreSQL 一起使用。
通过此更改,我可以毫无问题地编辑/删除列。
- PostgreSQL 版本:
9.2.1
- PostgreSQL 驱动版本:
9.2-1001
- Hibarnate 版本:
3.6.10
关于hibernate - PostgreSQL hibernate 错误 : "Cannot change transaction isolation level in the middle of a transaction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13412768/