2

我正在尝试为 Oracle 上的 Hibernate 应用程序配置 seqhilo 生成器。

<id name="idTest" type="int">
       <column name="ID_TEST" precision="6" scale="0" />
       <generator class="seqhilo">
        <param name="sequence">S_TEST</param>
        <param name="max_lo">1000</param>
       </generator>
 </id>

我在 oracle 数据库 10g 中创建了一个序列(S_TEST),不幸的是它不起作用:id 始终为空。你能向我解释一下如何在 oracle 数据库中使用 seqhilo 生成器吗,我可能很困惑 :(

这是生成的 sql 跟踪:

08:52:44,441 DEBUG AnnotationTransactionAttributeSource:107 - Adding transactional method [create] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
08:52:44,441 DEBUG HibernateTransactionManager:346 - Using transaction object [org.springframework.orm.hibernate3.HibernateTransactionManager$HibernateTransactionObject@1786a3c]
08:52:44,441 DEBUG HibernateTransactionManager:374 - Creating new transaction with name [com.cylande.utilities.GenericDAO.create]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
08:52:44,472 DEBUG HibernateTransactionManager:496 - Opened new Session [org.hibernate.impl.SessionImpl@1bf68a9] for Hibernate transaction
08:52:44,472 DEBUG HibernateTransactionManager:507 - Preparing JDBC Connection of Hibernate Session [org.hibernate.impl.SessionImpl@1bf68a9]
08:52:44,487 DEBUG DriverManagerDataSource:163 - Creating new JDBC DriverManager Connection to [jdbc:oracle:thin:@localhost:1521:orcl]
08:52:44,519 DEBUG HibernateTransactionManager:572 - Exposing Hibernate transaction as JDBC transaction [oracle.jdbc.driver.T4CConnection@8c7be5]
08:52:44,519 DEBUG TransactionSynchronizationManager:186 - Bound value [org.springframework.jdbc.datasource.ConnectionHolder@11a0d35] for key [org.springframework.jdbc.datasource.DriverManagerDataSource@13b5a3a] to thread [main]
08:52:44,519 DEBUG TransactionSynchronizationManager:186 - Bound value [org.springframework.orm.hibernate3.SessionHolder@12c4c57] for key [org.hibernate.impl.SessionFactoryImpl@1594a88] to thread [main]
08:52:44,519 DEBUG TransactionSynchronizationManager:261 - Initializing transaction synchronization
08:52:44,534 DEBUG TransactionInterceptor:290 - Getting transaction for [com.cylande.utilities.GenericDAO.create]
08:52:44,534 DEBUG TransactionSynchronizationManager:142 - Retrieved value [org.springframework.orm.hibernate3.SessionHolder@12c4c57] for key [org.hibernate.impl.SessionFactoryImpl@1594a88] bound to thread [main]
08:52:44,550 DEBUG SQL:102 - select categorie_.ID_CATEGORIE, categorie_.CATEGORIE as CATEGORIE9_ from AHMED.CATEGORIE categorie_ where categorie_.ID_CATEGORIE=?
08:52:44,550 TRACE IntegerType:128 - binding '1' to parameter: 1
08:52:44,550 TRACE StringType:170 - returning 'Test dintegration' as column: CATEGORIE9_
08:52:44,550 DEBUG TransactionInterceptor:319 - Completing transaction for [com.cylande.utilities.GenericDAO.create]
08:52:44,550 DEBUG HibernateTransactionManager:880 - Triggering beforeCommit synchronization
08:52:44,550 DEBUG HibernateTransactionManager:893 - Triggering beforeCompletion synchronization
08:52:44,550 DEBUG HibernateTransactionManager:707 - Initiating transaction commit
08:52:44,566 DEBUG HibernateTransactionManager:651 - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@1bf68a9]
08:52:44,566 DEBUG SQL:102 - insert into AHMED.TEST (ID_APPLICATION, ID_MODULE, ID_CATEGORIE, ID_PAGE, NOM_TEST, DESCRIPTION_TEST, ID_TEST) values (?, ?, ?, ?, ?, ?, ?)
08:52:44,566 TRACE IntegerType:121 - binding null to parameter: 1
08:52:44,566 TRACE IntegerType:121 - binding null to parameter: 2
08:52:44,566 TRACE IntegerType:128 - binding '1' to parameter: 3
08:52:44,566 TRACE IntegerType:121 - binding null to parameter: 4
08:52:44,566 TRACE StringType:128 - binding 'nomTest2' to parameter: 5
08:52:44,566 TRACE IntegerType:128 - binding '0' to parameter: 7
08:52:44,581  WARN JDBCExceptionReporter:77 - SQL Error: 1, SQLState: 23000
08:52:44,581 ERROR JDBCExceptionReporter:78 - ORA-00001: violation de contrainte unique

即使我将生成器更改为序列或原生,必须生成的字段(id_Test)始终为零,结果保持不变

4

1 回答 1

1

声明看起来是正确的,或者至少非常接近文档中的示例:

5.1.4.2。高/低算法

hilo 和 seqhilo 生成器提供了 hi/lo 算法的两种替代实现。第一个实现需要一个“特殊”数据库表来保存下一个可用的“hi”值。在支持的情况下,第二个使用 Oracle 样式的序列。

<id name="id" type="long" column="cat_id">
        <generator class="seqhilo">
                <param name="sequence">hi_value</param>
                <param name="max_lo">100</param>
        </generator>
</id>

但为了调试问题,我会尝试

  • 激活生成的 SQL 的日志记录以查看究竟发生了什么(并发布结果)
  • 坚持上面的例子,即在id元素中定义列并使用long
    • 如果可行,请开始修改配置
于 2010-09-21T01:29:41.677 回答