gpt4 book ai didi

java - 如何在@ManyToOne 字段上创建@Index?

转载 作者:行者123 更新时间:2023-12-05 06:43:40 25 4
gpt4 key购买 nike

我想使用 hibernate 创建一个@Index。索引应包含 3 个字段,其中一些字段嵌套在其他实体中。我怎样才能做到这一点?

@Table(name = "my_entity", indexes = {
@Index(name = "my_index", columnList = "id, person.firstname, person.lastname")
})
@Entity
public class MyEntity {
private Long id;

@ManyToOne
private Person person;
}

@Entity
public class Person {
@Id private Long id;
private String firstname;
private String lastname;
}

结果:

Caused by: org.hibernate.AnnotationException: Unable to create unique key constraint (id, person.firstname, person.lastname) on table my_entity: database column 'person.firstname', 'person.lastname' 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:1684)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1459)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
... 46 more

最佳答案

columnList 采用 名称数组。不是实体属性的数组。

因此您只需提供测试表中列的名称,就像您使用 SQL 定义索引时所做的那样。

Documentation

关于java - 如何在@ManyToOne 字段上创建@Index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32377892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com