1
我有Spring引導1.4.3 + Hibernate 5.0.11 + H2數據庫。 當我嘗試對特定值使用@UniqueConstraint時,我的應用程序啓動失敗。就這個。響應類標記與COLUMNNAMES 「調查」 和 「用戶」@UniqueConstraint Spring引導/休眠問題
@Entity
@Table(name = "responses", uniqueConstraints = {@UniqueConstraint(columnNames = {"survey, user"}, name = "responses_survey_user_idx")})
public class Response extends BaseEntity
{
@NotNull
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "survey", nullable = false)
private Survey survey;
@NotNull
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "user", nullable = false)
private User user;
@NotNull
private String answers;// json serialized
}
調查類@UniqueConstraint:
@Entity
@Table(name = "surveys", uniqueConstraints = {@UniqueConstraint(columnNames = {"name"}, name = "surveys_name_idx")})
public class Survey extends BaseEntity{
@NotNull
@SafeHtml
@Length(min = 3)
private String name;
@NotNull
@SafeHtml
@Length(min = 3)
private String description;
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "survey")
private List<Response> responses;
}
用戶等級:
@Entity
@Table(name = "users", uniqueConstraints = {@UniqueConstraint(columnNames = "login", name = "users_unique_email_idx")})
public class User extends BaseEntity {
@NotEmpty
@SafeHtml
private String login;
@NotEmpty
@Length(min = 8)
@SafeHtml
private String password;
@OneToMany(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "user")
private List<Response> responses;
}
我得到的錯誤是:
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 (survey, user) on table responses: database column 'survey, user' 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)
我試過了e在Response類中的列名稱,但它沒有幫助。 只有當我在這個類中刪除@UniqueConstraint時,它才起作用。
任何想法如何處理這個?
是的,你是對的。感謝名單 –