org.hibernate.exception.GenericJDBCException: 无法获取下一个序列值

3

我在使用Hibernate时遇到了问题。当我想要将一个对象保存到数据库时,出现了以下异常:

org.hibernate.exception.GenericJDBCException: could not get next sequence value
25P02, current transaction is aborted, commands ignored until end of transaction block

我的Hibernate配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory name="">
  <!-- Database connection settings -->
  <property name="connection.driver_class">org.postgresql.Driver</property>
  <property name="connection.url">jdbc:postgresql://localhost:5432/3encult</property>
  <property name="connection.username">3encult</property>
  <property name="connection.password">3encult</property>
  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">10</property>
  <!-- SQL dialect -->
  <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>
  <!-- Disable the second-level cache  
  Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>
  <!-- Drop and re-create the database schema on startup 
    <property name="hbm2ddl.auto">create</property>-->
  <mapping resource="resources/User.hbm.xml"/>
  <mapping resource="resources/ApplicationField.hbm.xml"/>
  <mapping resource="resources/Attribute.hbm.xml"/>
  <mapping resource="resources/Device.hbm.xml"/>
  <mapping resource="resources/Role.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

并且我的Hibernate映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                               "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 07-abr-2011 13:29:19 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
 <class name="com.cartif.database.Attribute" table="Attribute">
  <id name="iAttrId" column="attributeid" type="java.lang.Integer">
  <generator class="native">
    <param name="sequence">s_attribute</param>
  </generator>
</id>
 <property column="name" generated="never" lazy="false" name="name" type="java.lang.String"/>
 <property column="value" generated="never" lazy="false" name="value" type="java.lang.String"/>
 <property column="timestamp" generated="never" lazy="false" name="date" type="java.sql.Timestamp"/>
 <property column="deviceId" generated="never" lazy="false" name="iDeviceId" type="java.lang.Integer"/>
 <property column="sensorId" generated="never" lazy="false" name="iSensorId" type="java.lang.Integer"/>
 </class>
</hibernate-mapping>

属性 iAttrId 存在并且它有 get/set 方法,表 Attribute 上也存在列 attributeid。

Hibernate 显示 SQL 查询语句,它们是:

Hibernate: select nextval ('s_attribute')
Hibernate: insert into Attribute (name, value, timestamp, deviceId, sensorId, attributeid) values (?, ?, ?, ?, ?, ?)

什么是原因?
谢谢!
2个回答

1
你在PostgreSQL中创建了序列吗?这与Oracle非常相似。仅有表是不够的,你还需要创建一个序列。

是的,我有一个名为s_attribute的序列。 - Jose Hdez

1

我已经删除了之前的序列,生成了一个新的序列,现在它可以正常工作!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接