0

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(AbstractBeanFactory.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.pumpEvents(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(AbstractAutowireCapableBeanFactory.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?

2
  • Share your beans file where you define schema of table in hibernate. Commented Jul 6, 2015 at 9:51
  • see whether you have misspelled column names in your entity bean Commented Jul 6, 2015 at 9:55

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I had a property empId, in my case it worked with property name not with db column (emp_id). Whereas for the other field in superclass it accepts only db column name. I am confused by this double standard of hibernate now
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.