简体   繁体   English

H2 数据库与 Hibernate 错误导致:org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:唯一索引或主键

[英]H2 Database with Hibernate error Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key

I am getting JdbcSQLIntegrityConstraintViolationException exception while starting my server.我在启动服务器时收到JdbcSQLIntegrityConstraintViolationException异常。

Seems like some problem in using <map> , earlier it was working file with older version of H2 but now not working.使用<map>似乎有些问题,之前它是使用旧版本的 H2 工作的文件,但现在无法正常工作。

My curren H2 version is:我目前的 H2 版本是:

implementation group: 'com.h2database', name: 'h2', version: '1.4.200'

Older was:较旧的是:

compile group: 'com.h2database', name: 'h2', version: '1.4.193'

My class is using simple map variable like: private Map<String, String> extraData;我的 class 使用简单的 map 变量,例如: private Map<String, String> extraData;

Can someone tell me, how to fix this???谁能告诉我,如何解决这个问题??? Thanks谢谢

Here is my XML snippet:这是我的 XML 片段:

  <class name="pojo.MachineInstruction" >
    <id name="machineId" type="java.lang.Integer"/>
    <!--Time stamp is auto generated. No need to set the value..-->
    <timestamp name="timestamp"/>
    <property name="instructionType" not-null="true">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">pojo.MachineInstructionType</param>
      </type>
    </property>
    <property name="instructionStatus" not-null="true">
      <type name="org.hibernate.type.EnumType">
        <param name="enumClass">pojo.MachineInstructionStatus</param>
      </type>
    </property>
    <property name="version" type="java.lang.String"/>
    <map name="extraData" cascade="all">
      <key column="extraData" />
      <map-key type="text" column="key"/>
      <element type="text" column="value"/>
    </map>
  </class>
Caused by: org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation: "PRIMARY KEY ON """".PAGE_INDEX"; SQL statement:
ALTER TABLE PUBLIC.MACHINEINSTRUCTION_EXTRADATA ADD CONSTRAINT PUBLIC.FKROHO504EJPG9G4R81YYKTV44K FOREIGN KEY(EXTRADATA) REFERENCES PUBLIC.MACHINEINSTRUCTION(MACHINEID) NOCHECK [23505-200]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:459)
at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
at org.h2.message.DbException.get(DbException.java:205)
at org.h2.message.DbException.get(DbException.java:181)
at org.h2.pagestore.db.PageDataIndex.add(PageDataIndex.java:125)
at org.h2.pagestore.PageStore.addMeta(PageStore.java:1804)
at org.h2.pagestore.db.PageBtreeIndex.<init>(PageBtreeIndex.java:65)
at org.h2.pagestore.db.PageStoreTable.addIndex(PageStoreTable.java:183)
at org.h2.command.ddl.AlterTableAddConstraint.createIndex(AlterTableAddConstraint.java:298)
at org.h2.command.ddl.AlterTableAddConstraint.tryUpdate(AlterTableAddConstraint.java:223)
at org.h2.command.ddl.AlterTableAddConstraint.update(AlterTableAddConstraint.java:78)
at org.h2.engine.MetaRecord.execute(MetaRecord.java:60)
at org.h2.engine.Database.open(Database.java:759)
at org.h2.engine.Database.openDatabase(Database.java:307)
at org.h2.engine.Database.<init>(Database.java:301)
at org.h2.engine.Engine.openSession(Engine.java:74)
at org.h2.engine.Engine.openSession(Engine.java:192)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:171)
at org.h2.engine.Engine.createSession(Engine.java:166)
at org.h2.engine.Engine.createSession(Engine.java:29)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:340)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:173)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:152)
at org.h2.Driver.connect(Driver.java:69)
at org.hibernate.engine.jdbc.connections.internal.DriverConnectionCreator.makeConnection(DriverConnectionCreator.java:38)
... 35 more

Unfortunately, H2 1.4.* and older versions don't have automatic upgrade procedure, it is under responsibility of their users.不幸的是,H2 1.4.* 和更早的版本没有自动升级程序,这是由其用户负责的。 https://h2database.com/html/tutorial.html#upgrade_backup_restore https://h2database.com/html/tutorial.html#upgrade_backup_restore

(The upcoming H2 2.0 will have a built-in upgrade utility and will refuse to open old files without upgrade to avoid such issues.) (即将推出的 H2 2.0 将具有内置的升级实用程序,并且将拒绝在未升级的情况下打开旧文件以避免此类问题。)

1.4.196 and older versions had a bug with some combinations of indexes and referential constraints, it was fixed in 1.4.197, but upgrade path for the legacy PageStore engine wasn't provided. 1.4.196 及更早的版本存在一些索引和引用约束组合的错误,在 1.4.197 中已修复,但未提供旧版 PageStore 引擎的升级路径。 MVStore also had similar upgrade issues, but they were fixed. MVStore 也有类似的升级问题,但已修复。

Right now your database seems to be corrupted, because upgrade wasn't performed.现在您的数据库似乎已损坏,因为未执行升级。 You need to write some dummy implementation of org.h2.api.DatabaseEventListener , put it into the classpath of your application (classpath of your server if you use separate H2 Server process), and open your database with ;DATABASE_EVENT_LISTENER='path.to.YourListener' appended to JDBC connection URL.您需要编写一些org.h2.api.DatabaseEventListener的虚拟实现,将其放入应用程序的类路径(如果您使用单独的 H2 服务器进程,则为服务器的类路径),然后使用;DATABASE_EVENT_LISTENER='path.to.YourListener'附加到 JDBC 连接 URL。 Wrong constraints will be logged to your listener.错误的约束将记录到您的侦听器中。 After that you will be able to export your database to the SQL Script with SCRIPT TO 'filename.sql' command and create a new database file and populate it with that script with RUNSCRIPT FROM 'filename.sql' .之后,您将能够使用SCRIPT TO 'filename.sql'命令将数据库导出到 SQL 脚本,并创建一个新的数据库文件并使用RUNSCRIPT FROM 'filename.sql'使用该脚本填充它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException:唯一索引或主键违规 - org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: Unique index or primary key violation 我的 spring 启动项目导致 org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL 不允许列“ID” - My spring boot project causes org.h2.jdbc.JdbcSQLIntegrityConstraintViolationException: NULL not allowed for column "ID", how do fix it? 带有H2数据库的JUnit:为多个数据添加多语言服务时的唯一索引或主键冲突 - JUnit with H2 Database : Unique index or primary key violation when adding multilingual services for multiple data org.h2.jdbc.JdbcSQLSyntaxErrorException h2 数据库 java - org.h2.jdbc.JdbcSQLSyntaxErrorException h2 database java H2 数据库抛出 org.h2.jdbc.JdbcSQLNonTransientException - H2 database throws org.h2.jdbc.JdbcSQLNonTransientException H2数据库-在主键冲突时替换 - H2 Database - on primary key conflict replace 在数据库中将Hibernate与H2一起使用时出错 - Error using Hibernate with H2 in database 在内存数据库中使用Hibernate和H2时出错 - Error using Hibernate with H2 in memory database org.h2.jdbc.JdbcSQLException:在视图+ H2数据库1.4.190 + Hibernate 4.2.0.Final上找不到“ X”列 - org.h2.jdbc.JdbcSQLException: Column “X” not found on view + H2 Database 1.4.190 + Hibernate 4.2.0.Final 与JDBC(h2数据库)相比,使用hibernate进行长SQL查询 - Long SQL query with hibernate compared to JDBC (h2 database)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM