gpt4 book ai didi

java - Hibernate 无法创建唯一键约束

转载 作者:行者123 更新时间:2023-12-01 11:20:51 25 4
gpt4 key购买 nike

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

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

我检查过这个问题:org.hibernate.AnnotationException: Unable to create unique key constraintUnable to create unique key constraint然而他们并没有解决我的问题。

有什么办法可以解决这个问题吗?

最佳答案

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

@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;
}

关于java - Hibernate 无法创建唯一键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31242648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com