Java 休眠:创建索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22816817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate: Create Index
提问by Rooky
I want to create several Indexes in my DB. Unfortunately we have to change the persistence provider from EclipseLink to Hibernate, but nor the solution with javax.persistence.Index neither the solution with Hibernate works.
我想在我的数据库中创建几个索引。不幸的是,我们必须将持久性提供程序从 EclipseLink 更改为 Hibernate,但是使用 javax.persistence.Index 的解决方案也无法使用 Hibernate 的解决方案。
This is what the class looks like:
这就是类的样子:
@Entity
@Table(name = "my_shop")
public class Shop extends BaseEntity {
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Calendar lastUpdate;
}
This should be the solution with javax.persistence.*:
这应该是 javax.persistence.* 的解决方案:
import javax.persistence.Index;
import javax.persistence.Table;
@Table(name = "my_shop",
indexes = @Index(columnList = "lastupdate")
)
The Hibernate annotations are deprecated, so there must be a reason not to use these annotations:
不推荐使用 Hibernate 注释,因此必须有理由不使用这些注释:
import org.hibernate.annotations.Index; // deprecated
import org.hibernate.annotations.Table;
@Table(...,
indexes = @Index(columnNames = "lastupdate")
)
I use Glassfish 3.1.2.2, PostgreSQL 9.1, JPA 2.1 and hibernate-core 4.3.4.Final. If I look in the database, there are no indexes created on the specific field via psql "\d+".
我使用 Glassfish 3.1.2.2、PostgreSQL 9.1、JPA 2.1 和 hibernate-core 4.3.4.Final。如果我查看数据库,没有通过 psql "\d+" 在特定字段上创建索引。
This what my persistence.xml looks like:
这就是我的 persistence.xml 的样子:
...
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...
Only EclipseLink can handle this easily:
只有 EclipseLink 可以轻松处理这个问题:
import org.eclipse.persistence.annotations.Index;
@Entity
@Table(name = "my_shop")
public class Shop extends BaseEntity {
@Index
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Calendar lastUpdate;
}
I tested the given solutions with all combinations "lastupdate", "lastUpdate" and additional "name" attributes in @Column and @Index, but nothing seems to work.
我在@Column 和@Index 中使用所有组合“lastupdate”、“lastUpdate”和附加“name”属性测试了给定的解决方案,但似乎没有任何效果。
Update 1
更新 1
Indeed this solution works:
这个解决方案确实有效:
@javax.persistence.Table(name = "my_shop")
@Table(appliesTo = "my_shop"
,indexes = {@Index(columnNames = "name", name = "name"),
@Index(columnNames = "lastupdate", name = "lastupdate")}
)
But still org.hibernate.annotations.Index;
is marked as deprecated. So is it good practice to use it or not? If not what's the alternative because apparently javax.persistence.Index
doesn't work.
但仍被org.hibernate.annotations.Index;
标记为已弃用。那么使用它还是不使用它是一种好习惯吗?如果不是,有什么替代方法,因为显然javax.persistence.Index
不起作用。
org.hibernate.annotations.Index;
works with every value: create, update, ...
javax.persistence.Index
doesn't matter which value "hibernate.hbm2ddl.auto" has, doesn't work.
org.hibernate.annotations.Index;
适用于每个值:创建,更新,...
javax.persistence.Index
与“hibernate.hbm2ddl.auto”具有哪个值无关,不起作用。
回答by Angular University
The @Index
annotation only works with hibernate.hbm2ddl.auto=create-drop
, see this entry in the Hibernate forums.
该@Index
只适用于注解hibernate.hbm2ddl.auto=create-drop
,看到这个在Hibernate论坛条目。
Just one word of caution is that this option drops the tables, but in general hibernate.hbm2ddl.auto
is meant for development purposes only.
需要注意的一点是,此选项会删除表,但通常hibernate.hbm2ddl.auto
仅用于开发目的。
回答by danbst
I use JPA 2.1 with Hibernate 4.3 and PostgreSQL 9.3. I have no problems with indexes
我将 JPA 2.1 与 Hibernate 4.3 和 PostgreSQL 9.3 一起使用。我对索引没有问题
hibernate-commons-annotations-4.0.4.Final.jar
hibernate-core-4.3.1.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
Though my config has
虽然我的配置有
<property name="hibernate.hbm2ddl.auto" value="update"/>
And that's my entity mapping
这就是我的实体映射
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
@Entity
@Table(name = "users", indexes = {
@Index(columnList = "id", name = "user_id_hidx"),
@Index(columnList = "current_city", name = "cbplayer_current_city_hidx")
})
PS. In fact, I have some problems with that annotations. I cannot specify tablespace for index and must create indecies for subclasses in parent class for SINGLE_TABLE hierarchy.
附注。事实上,我对这些注释有一些问题。我无法为索引指定表空间,并且必须为 SINGLE_TABLE 层次结构的父类中的子类创建索引。
回答by naXa
To sum up:
总结:
- JPA 2.1:
javax.persistence.Index
(see JSR-000338, p. 450, item 11.1.23) - Hibernate ORM:
org.hibernate.annotations.Index
- JPA 2.1:(
javax.persistence.Index
参见JSR-000338,第 450 页,第 11.1.23 项) - 休眠ORM:
org.hibernate.annotations.Index
回答by Douglas Nassif Roma Junior
With Hibernate
you need to enter the name
attribute in the @Index
annotation.
随着Hibernate
您需要输入name
在属性@Index
注解。
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Index;
import javax.persistence.Table;
@Entity
@Table(
indexes = {
@Index(columnList = "description", name = "product_description")
})
public class Product implements Serializable {
// ...
private String description;
// getters and setters
}
With EclipseLink
is not required, it creates the name
automatically.
WithEclipseLink
不是必需的,它会name
自动创建。
回答by Tapan
- JAVAX
- JAVAX
JAVAX package already included because of JPA or JERSEY or Spring Boot using Jersey so we should go with
由于 JPA 或 JERSEY 或 Spring Boot 使用 Jersey 已包含 JAVAX 包,因此我们应该使用
@Table(name = "userDetails", indexes={@Index(columnList="uid,name",name="Index_user")})
Benefits :
好处 :
- We don't need to add any extra dependency
- We can also define unique key inside @Table
- Hibernate Same thing we can do with @Index, but for that, we need to add Hibernate dependency.
- 我们不需要添加任何额外的依赖
- 我们还可以在@Table 中定义唯一键
- Hibernate 我们可以用@Index 做同样的事情,但为此,我们需要添加 Hibernate 依赖项。