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

发布于 2024-11-18 17:19:19 字数 3501 浏览 0 评论 0原文

我的休眠有问题。当我想将对象保存到数据库中时,出现以下异常:

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 方法,表属性上的列 attributeid 也存在。

Hibernate 显示 SQL 查询,它们是:

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

原因是什么?

谢谢!

I have a problem with Hibernate. When I want to save an object into database, I get this exception:

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

My hibernate configuration file is:

<?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>

And my Hibernate mapping file:

<?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>

The attribute iAttrId exists and it has get/set methods and column attributeid on table Attribute also exists.

Hibernate shows the SQL querys, and they are:

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

What is the cause?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪都笑了 2024-11-25 17:19:19

您是否在 PostgreSQL 中创建了序列?这很像 Oracle。桌子本身是不够的;您还必须创建一个序列。

Did you create a sequence in PostgreSQL? It's much like Oracle that way. The table, by itself, isn't enough; you have to create a sequence, too.

橘寄 2024-11-25 17:19:19

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

I have deleted previous sequence and generated a new sequence and it works fine!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文