簡體   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