- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要对 OneToOne 和 ManyToOne 关系的一些 JoinColumn 设置唯一约束,但即使我在 @JoinColumn 注释中正确设置名称,Hibernate 似乎也找不到这些列。这对我来说似乎很奇怪,因为同样的约束在 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 元素字符串数组,第一个元素是无效的列名称引用。
关于java - JPA- hibernate : UniqueConstraint on OneToOne & ManyToOne JoinColumns,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354191/
我只是没有明白这一点。下面的代码是怎么回事,哪里出错了?我必须上课:资源和预订。一个资源可以有多个预留,并且关系是双向的。对我来说,一切似乎都找到了,我已经查看了一堆资源和文档 - 是的,还有很多示例
我正在寻找有关大型遗留模式映射的一些建议。 情况是这样的。假设我们有一个名为 BusinessTransaction 的类。此类有几个引用用户 ID 的字段: @Entity public class
我正在寻找有关大型遗留模式映射的一些建议。 情况是这样的。假设我们有一个名为 BusinessTransaction 的类。此类有几个引用用户 ID 的字段: @Entity public class
我上课了: @Entity @Table(name="users") public class User{ private Integer id; private String name; priva
我有一个实体类 付款 其中有实体类 付款方式 与 多对一 关系。 现在因为我的 PaymentMethod 是主表。 所以我想在不影响主表 PaymentMethod 的情况下保存付款。 但是每次我保
关系的 @OneToMany 端填充良好,但 @ManyToOne 端每次都会覆盖(仅最后一项保留) @Entity @Table(name="order") public c
下面的联接保留了与每个客户端相关的 TaxJurisdictions,但它不包括插入中联接 (CLIENT_CODE) 中使用的列,导致我的数据库表 TBL_TAX_JURISDICTION.CLIE
我得到了城市和天气。天气应该指向数据库中的城市对象。一个城市可以有多个天气条目。我的问题是,每次我向数据库添加天气时,它都会创建一个具有相同名称但具有其他 ID 的新城市。 天气实体; @Entity
所以我的数据库中有一份文档有修订。文档表包含文档通用的元数据,修订版包含内容。目前我可以获得正确修订的文档。 我想做的是通过映射的所有修订获取文档列表。 这是我的两个实体: @Entity publi
大家好。 我有一个问题让我睡不着)例如,如果我有一个名为 Product 的实体(类)。我应该将这个产品与一堆图像链接起来。在数据库(在我的例子中是MySQL)中,我会创建一个产品表和图像表。图像表将
我使用 Spring Roo 对 MySQL 数据库进行逆向工程,这是一个我只有读取权限的数据库。 其中一个名为 Transaction 的实体有一个名为 originalTransaction 的字
我可以建立如下关系吗: @Entity Table1{ @ManyToOne @JoinColumn(name = "Column1", referen
我有以下单向 ManyToOne 关系: @Entity @Table(name = "Child") public class Child { @Id private Integer
我的关系非常简单,我的模型如下所示: public class Project { [PrimaryKey, AutoIncrement] public int ID { get; s
我有以下数据库结构: CREATE TABLE `author` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255
目录 jpa实体@ManyToOne @OneToMany无限递归 问题描述 解决办法 @OneToMany和@ManyTo
我在保存具有 ManyToOne 双向关系的实体时遇到问题,这里是: 警告:HHH000437:正在尝试保存一个或多个与未保存的 transient 实体具有不可空关联的实体。在保存这些依赖实体之前,
我有 2 个实体 - 具有映射 @ManyToOne 的用户和角色。我想更改用户角色,但角色也想更新。 用户实体: @ManyToOne @JoinColumn(name = "role_id", i
我有 4 个类如下... @MappedSuperclass @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstrac
想象一下下面的类 @Embeddable class A { @ManyToOne public B classB; ... public State someEnum
我是一名优秀的程序员,十分优秀!