Unable to create unique key constraint (ID) on table XXX: ID not found

项目是Spring整合Hibernate的项目,启动时报如下异常:
2017-1-5 10:25:34 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create unique key constraint (ID) on table WOS_DATA_COMPARISON: ID not found
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1486)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1229)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (ID) on table WOS_DATA_COMPARISON: ID not found
at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:616)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:348)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
... 26 more

异常原因:
实体类中的注解写错了,如下:
@Entity
@Table(name = "WOS_DATA_COMPARISON", schema = "SCAP", uniqueConstraints = @UniqueConstraint(columnNames = "ID"))
正确的:
@Entity
@Table(name = "WOS_DATA_COMPARISON", schema = "SCAP", uniqueConstraints = @UniqueConstraint(columnNames = "ORDERNO"))

对应get方法:
@Id
@Column(name = "ORDERNO",unique = true, nullable = false)
public String getOrderNo() {
return orderNo;
}
PS:未使用ID做为数据库对应表的主键,而是使用的orderno,用注解时标注成ID了,应该是ORDERNO
`UNIQUE constraint failed: TableMenu.id Unable to fetch row` 错误表明在向 `TableMenu` 表插入或更新数据时,违反了 `id` 列的唯一性约束,即尝试插入或更新的 `id` 值已存在于表中。以下是一些解决该问题的方法: #### 检查插入或更新的数据 在插入或更新数据之前,先检查要插入或更新的 `id` 值是否已经存在于表中。可以使用 SQL 查询来实现这一点。示例代码如下: ```python import sqlite3 # 连接到数据库 conn = sqlite3.connect('your_database.db') cursor = conn.cursor() # 要插入的 id 值 new_id = 1 # 检查 id 是否已经存在 cursor.execute("SELECT id FROM TableMenu WHERE id =?", (new_id,)) existing_id = cursor.fetchone() if existing_id is None: # 如果 id 不存在,则插入新记录 cursor.execute("INSERT INTO TableMenu (id, other_column) VALUES (?,?)", (new_id, 'other_value')) conn.commit() else: print(f"ID {new_id} already exists in the table.") # 关闭连接 conn.close() ``` #### 忽略重复插入 如果不关心重复插入的情况,可以使用 `INSERT OR IGNORE` 语句。该语句会在插入时忽略违反唯一性约束的情况,不会抛出错误。示例代码如下: ```sql INSERT OR IGNORE INTO TableMenu (id, other_column) VALUES (1, 'other_value'); ``` #### 更新现有记录 如果希望在 `id` 已存在时更新记录,可以使用 `INSERT OR REPLACE` 语句。该语句会在插入时,如果 `id` 已存在,则先删除原记录,再插入新记录。示例代码如下: ```sql INSERT OR REPLACE INTO TableMenu (id, other_column) VALUES (1, 'new_other_value'); ``` #### 检查数据来源 确保插入或更新的数据来源不会产生重复的 `id` 值。例如,如果 `id` 是自动生成的,要保证生成算法不会生成重复的 `id`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值