JPA @Index注释不起作用

时间:2017-11-30 19:30:32

标签: jpa spring-data

我想在我的表中的一个列中添加索引,但它总是抱怨找不到列,我检查了表,列存在!

如果我从实体对象中删除了索引注释,它可以正常工作。

如果我将columnList更改为字段名称,例如columnList =“reportOwnerId”,则会失败并显示相同的消息:'找不到数据库列'

我怎样才能让它发挥作用?感谢

这是我的实体类:

import javax.persistence.Id;
import javax.persistence.Index;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@NoArgsConstructor
@Table(name = "schedules",
        indexes = { @Index(name = "SCHEDULE_OWNER_GUID_INDEX", columnList = "report_owner_guid")}
        )
public class ReportSchedule extends AbstractTimestampEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Setter
    @Column(name = "report_owner_guid", nullable = false, unique = false)
    private String reportOwnerId;
...

以下是错误消息:

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (report_owner_guid) on table schedules: database column 'report_owner_guid' 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.cfg.Configuration.buildUniqueKeyFromColumnNames(Configuration.java:1682)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1457)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)

后来我发现它可能是因为列不是唯一的,我将索引更新为:

@Table(name = "schedules",
            indexes = { @Index(name = "SCHEDULE_OWNER_GUID_INDEX", columnList = "report_owner_guid", unique = false)}
            )

它仍然以相同的错误失败。

0 个答案:

没有答案