我需要对OneToOne和ManyToOne关系的几个JoinColumns设置唯一的约束,但Hibernate似乎没有找到这些列,即使我在@JoinColumn注释中正确设置了名称。这对我来说似乎很奇怪,因为相同的约束在LiquiBase中工作得很好。
package entities;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
import java.util.Objects;
/**
* A MyAnswer.
*/
@Entity
@Table(name = "my_answer",
uniqueConstraints = @UniqueConstraint(columnNames = {"question_id, questionnaire_id", "user_id"})
)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "myanswer")
public class MyAnswer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "mycheck")
private String mycheck;
@OneToOne
@JoinColumn
private Answer answer;
@OneToOne
@JoinColumn(name = "question_id")
private Question question;
@OneToOne
@JoinColumn(name = "questionnaire_id")
private Questionnaire questionnaire;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMycheck() {
return mycheck;
}
public MyAnswer mycheck(String mycheck) {
this.mycheck = mycheck;
return this;
}
public void setMycheck(String mycheck) {
this.mycheck = mycheck;
}
public Answer getAnswer() {
return answer;
}
public MyAnswer answer(Answer answer) {
this.answer = answer;
return this;
}
public void setAnswer(Answer answer) {
this.answer = answer;
}
public Question getQuestion() {
return question;
}
public MyAnswer question(Question question) {
this.question = question;
return this;
}
public void setQuestion(Question question) {
this.question = question;
}
public Questionnaire getQuestionnaire() {
return questionnaire;
}
public MyAnswer questionnaire(Questionnaire questionnaire) {
this.questionnaire = questionnaire;
return this;
}
public void setQuestionnaire(Questionnaire questionnaire) {
this.questionnaire = questionnaire;
}
public User getUser() {
return user;
}
public MyAnswer user(User user) {
this.user = user;
return this;
}
public void setUser(User user) {
this.user = user;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MyAnswer myAnswer = (MyAnswer) o;
if (myAnswer.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), myAnswer.getId());
}
@Override
public int hashCode() {
return Objects.hashCode(getId());
}
@Override
public String toString() {
return "MyAnswer{" +
"id=" + getId() +
", mycheck='" + getMycheck() + "'" +
"}";
}
}
在抛出的异常日志下面:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to create unique key constraint (question_id, questionnaire_id, user_id) on table my_answer: database column 'question_id, questionnaire_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at project.App.main(App.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (question_id, questionnaire_id, user_id) on table my_answer: database column 'question_id, questionnaire_id' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.buildUniqueKeyFromColumnNames(InFlightMetadataCollectorImpl.java:2074)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.buildUniqueKeyFromColumnNames(InFlightMetadataCollectorImpl.java:1935)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processUniqueConstraintHolders(InFlightMetadataCollectorImpl.java:1923)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1595)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:858)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:885)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:360)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:382)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:371)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 19 common frames omitted
2018-05-15 17:25:20.943 WARN 16173 --- [neut-Executor-1] com.zaxxer.hikari.pool.ProxyConnection : HikariPool-1 - Connection org.postgresql.jdbc.PgConnection@669c1cb9 marked as broken because of SQLSTATE(08003), ErrorCode(0)
您的约束声明错误。而不是
@UniqueConstraint(columnNames = {"question_id, questionnaire_id", "user_id"})
使用
@UniqueConstraint(columnNames = {"question_id", "questionnaire_id", "user_id"})
注意引号中的差异。对于3列约束的列名需要一个3元素字符串数组,但您只有一个2元素字符串数组,第一个元素是无效的列名引用。
问题内容: 我知道这个问题在这里和整个互联网上已经被问过无数次了,我已经阅读了许多答案,但是我仍然不了解解决这个问题的正确方法。我正在尝试使用Spring MVC和JPA,并且每次访问延迟加载的属性时,都会得到LazyInitializationException。 这是我正在尝试的一些代码: 我正在访问的实体: 从DOI引用的实体 Spring MVC控制器: 我的Spring配置: 和堆栈跟踪
问题内容: 从Tomcat迁移到WebSphere时,我遇到了Spring 4 / Hibernate项目无法正确加载的问题。我已经将应用程序设置为PARENT_LAST类加载器,并将Hibernate版本恢复为4.2.16(由于WAS 8.5.5仅支持JPA 2.0)。我确实必须将WAR封装在EAR内,以符合IDE集成要求。 我现在在启动时遇到以下错误。任何援助将不胜感激。如果有人在WebSph
我将JPA与Hibernate一起使用。
我已经将project从Spring3.2更新到4.1,并将Hibernate4.2更新到4.3.7,发现了一个有趣的问题。我有疑问: 现在,在得到结果的时候
问题内容: 我该怎么办,JPA(我使用Hibernate)创建具有无符号类型的列?目前,我所有的ID列均已签名。 问题答案: 使用注释上的属性即可。对要使用的SQL类型进行 全面 猜测: 注意: 并非所有数据库(我认为像SQL Server)都支持unsigned int类型。
解决这个问题的正确方法是什么?如果你需要任何额外的信息,请说。提前谢了。