如何使用 Hibernate for PostgreSQL 创建索引
How to create indexes using Hibernate for PostgreSQL
我正在开发一个 Spring-MVC 应用程序,我在其中使用 Hibernate 作为 PostgreSQL 的 ORM 工具。对于项目模型中的一些实体,我想创建索引以便更快地查找。在阅读时,我发现可以使用 Hibernate 创建索引。不幸的是,我运气不佳。我只尝试在一个模型 class 上创建它,但是当我检查 PGAdmin 时,我看不到那个 table 的任何索引。
当我尝试将@Index 参数提供给@Table 注释时,出现错误。谁能告诉我如何注释列和整个 table 以供 Hibernate 自动索引。非常感谢。
在线用户模型://这个class我只是用来测试
import org.hibernate.search.annotations.Indexed;
import javax.persistence.*;
@Entity
@Table(name="onlineusers" )
@Indexed(index = "onlineuserindex")
public class OnlineUsers {
@Id
@Column(name="onlineuserid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "online_gen")
@SequenceGenerator(name = "online_gen",sequenceName = "online_seq")
private int onlineUserId;
@Column(name = "onlineusername")
private String personName;
}
请注意,当我尝试下面这样的操作时:
@Indexed(index = "usernameindex");
@Column(name="username");
private String userName;
我得到一个错误,@Indexed 不适用于某个字段。
POM.xml :
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.0.6.RELEASE </org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate.version>4.3.9.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Hibernate search dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
-->
请让我知道我做错了什么。非常感谢。 :-)
Hibernate Search
中的 @Indexed
注释仅适用于类型。所以你不能在属性上使用它们。
看了你的问题,你似乎想给 table 添加一个数据库索引?如果是这样,那么您必须使用 Index Annotation
您可以使用 @Index JPA 注释:
@Entity
@Table(name = "onlineusers",
indexes = {
@Index(name = "usernameindex", columnList="username", unique = true)
}
)
public class OnlineUsers {
...
}
仅当您使用自动 hbmddl
模式生成时才适用。如果数据库模式是在外部生成的(例如,当使用 Flyway 时),则此注释将没有任何效果。
当您将数据库架构生成委托给外部进程(数据库迁移工具或手动脚本更新过程)时,您需要在迁移脚本中包含索引。
因此,您要么使用 Hibernate(not even recommended for production systems)生成整个模式,要么依赖数据库迁移框架(例如 Flyway),索引仅包含在增量模式更新脚本中。
我正在开发一个 Spring-MVC 应用程序,我在其中使用 Hibernate 作为 PostgreSQL 的 ORM 工具。对于项目模型中的一些实体,我想创建索引以便更快地查找。在阅读时,我发现可以使用 Hibernate 创建索引。不幸的是,我运气不佳。我只尝试在一个模型 class 上创建它,但是当我检查 PGAdmin 时,我看不到那个 table 的任何索引。
当我尝试将@Index 参数提供给@Table 注释时,出现错误。谁能告诉我如何注释列和整个 table 以供 Hibernate 自动索引。非常感谢。
在线用户模型://这个class我只是用来测试
import org.hibernate.search.annotations.Indexed;
import javax.persistence.*;
@Entity
@Table(name="onlineusers" )
@Indexed(index = "onlineuserindex")
public class OnlineUsers {
@Id
@Column(name="onlineuserid")
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator = "online_gen")
@SequenceGenerator(name = "online_gen",sequenceName = "online_seq")
private int onlineUserId;
@Column(name = "onlineusername")
private String personName;
}
请注意,当我尝试下面这样的操作时:
@Indexed(index = "usernameindex");
@Column(name="username");
private String userName;
我得到一个错误,@Indexed 不适用于某个字段。
POM.xml :
<properties>
<java-version>1.8</java-version>
<org.springframework-version>4.0.6.RELEASE </org.springframework-version>
<org.aspectj-version>1.7.4</org.aspectj-version>
<org.slf4j-version>1.7.5</org.slf4j-version>
<hibernate.version>4.3.9.Final</hibernate.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<!-- Hibernate search dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.2.0.Final</version>
</dependency>
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
-->
请让我知道我做错了什么。非常感谢。 :-)
Hibernate Search
中的 @Indexed
注释仅适用于类型。所以你不能在属性上使用它们。
看了你的问题,你似乎想给 table 添加一个数据库索引?如果是这样,那么您必须使用 Index Annotation
您可以使用 @Index JPA 注释:
@Entity
@Table(name = "onlineusers",
indexes = {
@Index(name = "usernameindex", columnList="username", unique = true)
}
)
public class OnlineUsers {
...
}
仅当您使用自动 hbmddl
模式生成时才适用。如果数据库模式是在外部生成的(例如,当使用 Flyway 时),则此注释将没有任何效果。
当您将数据库架构生成委托给外部进程(数据库迁移工具或手动脚本更新过程)时,您需要在迁移脚本中包含索引。
因此,您要么使用 Hibernate(not even recommended for production systems)生成整个模式,要么依赖数据库迁移框架(例如 Flyway),索引仅包含在增量模式更新脚本中。