Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在@ManyToOne字段上创建@Index?_Java_Hibernate - Fatal编程技术网

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

Java 如何在@ManyToOne字段上创建@Index?,java,hibernate,Java,Hibernate,我想使用hibernate创建一个@索引。索引应由3个字段组成,其中一些字段嵌套在其他实体中。我怎样才能做到这一点 @Table(name = "my_entity", indexes = { @Index(name = "my_index", columnList = "id, person.firstname, person.lastname") }) @Entity public class MyEntity { private Long id; @ManyToOne

我想使用hibernate创建一个
@索引。索引应由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

您在这里尝试的是在两个不同的表(MyEntity和Person)上创建索引,因此这是不可能的。 如果要这样做,必须嵌入Person类,如下所示:

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

@Embeddable
public class Person {
   private String firstname;
   private String lastname;
}

请注意,我在Person类上通过@Embeddeble重新放置了@Entity,这样在数据库中,您将有一个名为Test的表,其中列id、firstname和lastname

columnList
采用一个列名数组。不是实体属性的数组

因此,您只需要提供测试表中列的名称,就像使用SQL定义索引一样


看起来您没有将人员映射到表中?另外,我认为应该是Person.firstname和Person.lastname(大写P)。我不能嵌入
Person
类。有没有其他方法可以达到这个目的?我的错,我没有看到多通注释。但是为什么您的Person实体(可能还有表)没有id呢?那么我需要索引
columnList=“id,fk\u Person\u id”
?如果要搜索这3个字段,我将如何编写本机查询<代码>从my_实体中选择*,其中id=:id和=:firstname和=:lastname
?如果测试表的列名为“fk_person_id”,则选择“是”。编写本机查询的方式与表上定义的索引无关。该索引仅由数据库使用,以便在可能的情况下通过使用索引而不是扫描表来加快查询速度。您需要学习SQL中的基本联接:在t.fk_person_id=p.id上的test t internal join person p中选择*,其中t.id=:id和p.firstname=:firstname和p.lastName=:lastName。@membersound在示例
@Index(name=“my_Index”,columnList=“id,person.firstname,person.lastName”)
中您是如何解决的。答案至少对我来说不是很清楚。@momo
columnList=id,firstname,lastname
,完全像您的sql字段而不是java实体字段一样编写。对不起,我的问题太老了,记不起来了。否则,您无法在
@manytone
子字段上创建索引。因为在
sql
中,它们会产生一个额外的表。并且不能在多个表上创建sql索引。因此,您唯一的机会是将外键id包括在索引中(例如,在我的示例中,
fk_person_id
)。