简体   繁体   中英

Hibernate Unable to Create Unique Key Constraint

I have an application which uses Hibernate to access database. It throws an error as like that:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Program%20Files%20(x86)/XXX/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create unique key constraint (column1, column2) on table TABLE: column1, column2 not found at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1482) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 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(AbstractBeanFactor y.java:198) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:273) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$400(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) at java.awt.EventQueue$2.run(Unknown Source) 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.pumpEve nts(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (column1, column2) on table TABLE: column1, column2 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:1541) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBe anFactory.java:1479) ... 27 more

I've checked this question: org.hibernate.AnnotationException: Unable to create unique key constraint and Unable to create unique key constraint however they did not solve my problem.

Is there any idea how to figure out the problem?

Be sure to use the database-level column names for the columnNames property of a @UniqueConstraint. For example, whilst for simple types the database-level column name may be the same as the entity-level property name, this is often not the case for relational properties.For example

@Table(name = "persons",
        uniqueConstraints = {
                @UniqueConstraint(columnNames = {"number", "person_type" ,"company_id"})
        })

public abstract class Person extends BaseEntity implements ActorProfile {

    @Column(name = "zpa_number")
    private String zpaNumber;

    @Column(name = "number")
    private Long number;

    @Column(name = "person_type")
    private String personType;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id")
    private Company company;
}

You most likely have a typo in the definition of that UniqueKey, because the columns the constraint should apply to aren't found in the database (which I assume is getting constructed by hibernate).

Beware that you have to use column names and not property names when specifying a unique key constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM