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

时间:2011-07-04 09:07:21

标签: hibernate jdbc java-ee

我遇到了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上的列属性也存在。

Hibernate显示SQL查询,它们是:

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

原因是什么?

谢谢!

2 个答案:

答案 0 :(得分:1)

你在PostgreSQL中创建了一个序列吗?这就像Oracle一样。桌子本身是不够的;你也必须创建一个序列。

答案 1 :(得分:1)

我删除了之前的序列并生成了一个新序列,它运行正常!