Hibernate无法创建唯一键约束

时间:2015-07-06 09:49:32

标签: java spring hibernate

我有一个使用Hibernate访问数据库的应用程序。它会抛出一个错误:

  

org.springframework.beans.factory.BeanCreationException:创建名称为' sessionFactory'的bean时出错在URL [file:/ C:/ Program%20Files%20(x86)/XXX/applicationContext.xml]中定义:init方法的调用失败;嵌套异常是org.hibernate.AnnotationException:无法在表上创建唯一键约束(column1,column2)TABLE:column1,column2 not found       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)       在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:273)       在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)       at java.awt.event.InvocationEvent.dispatch(Unknown Source)       at java.awt.EventQueue.dispatchEventImpl(Unknown Source)       在java.awt.EventQueue.access $ 400(未知来源)       在java.awt.EventQueue $ 2.run(未知来源)       在java.awt.EventQueue $ 2.run(未知来源)       at java.security.AccessController.doPrivileged(Native Method)       at java.security.AccessControlContext $ 1.doIntersectionPrivilege(Unknown Source)       at java.awt.EventQueue.dispatchEvent(Unknown Source)       at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)       at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.pumpEvents(Unknown Source)       at java.awt.EventDispatchThread.run(Unknown Source)   引起:org.hibernate.AnnotationException:无法在表上创建唯一键约束(column1,column2)TABLE:column1,column2 not found       在org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:616)       在org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:348)       在org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1162)       在org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:720)       在org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)       ......还有27个

我已经检查了这个问题:org.hibernate.AnnotationException: Unable to create unique key constraintUnable to create unique key constraint但是他们没有解决我的问题。

有任何想法如何找出问题吗?

2 个答案:

答案 0 :(得分:1)

请确保对@UniqueConstraint的columnNames属性使用数据库级列名称。例如,虽然对于简单类型,数据库级别的列名称可能与实体级别的属性名称相同,但对于关系属性通常不是这种情况。例如

this

答案 1 :(得分:0)

你很可能在UniqueKey的定义中有一个拼写错误,因为在数据库中找不到约束应该应用的列(我假设它是由hibernate构造的)。

请注意,在指定唯一键约束时,必须使用列名而不是属性名。