繁体   English   中英

@UniqueConstraint的Spring Boot / Hibernate问题

[英]Spring boot / Hibernate problems with @UniqueConstraint

我有Spring Boot 1.4.3 + Hibernate 5.0.11 + H2数据库。 当我尝试使用具有特定值的@UniqueConstraint时,我的应用程序启动失败。 就这个。 响应类用@UniqueConstraint标记,列名称为“ survey”和“ user”

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

调查类别:

@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)

我试图更改Response类中的列名,但这没有帮助。 仅当我完全删除此类中的@UniqueConstraint时,它才起作用。

有什么想法如何处理吗?

唯一约束中的列名称不应以逗号分隔,而应以数组中的字符串值分隔(请参见http://docs.oracle.com/javaee/6/api/javax/persistence/UniqueConstraint.html ):

@Table(
        name="EMPLOYEE", 
        uniqueConstraints=
            @UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM