【发布时间】:2014-05-14 01:16:15
【问题描述】:
我想在我的数据库中创建多个索引。不幸的是,我们必须将持久性提供程序从 EclipseLink 更改为 Hibernate,但使用 javax.persistence.Index 的解决方案也无法使用 Hibernate 的解决方案。
这是类的样子:
@Entity
@Table(name = "my_shop")
public class Shop extends BaseEntity {
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
private Calendar lastUpdate;
}
这应该是 javax.persistence.* 的解决方案:
import javax.persistence.Index;
import javax.persistence.Table;
@Table(name = "my_shop",
indexes = @Index(columnList = "lastupdate")
)
Hibernate 注释已被弃用,所以一定有理由不使用这些注释:
import org.hibernate.annotations.Index; // deprecated
import org.hibernate.annotations.Table;
@Table(...,
indexes = @Index(columnNames = "lastupdate")
)
我使用 Glassfish 3.1.2.2、PostgreSQL 9.1、JPA 2.1 和 hibernate-core 4.3.4.Final。如果我查看数据库,没有通过 psql "\d+" 在特定字段上创建索引。
这就是我的 persistence.xml 的样子:
...
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...
只有 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;
}
我在@Column 和@Index 中使用“lastupdate”、“lastUpdate”和其他“name”属性的所有组合测试了给定的解决方案,但似乎没有任何效果。
更新 1
这个解决方案确实有效:
@javax.persistence.Table(name = "my_shop")
@Table(appliesTo = "my_shop"
,indexes = {@Index(columnNames = "name", name = "name"),
@Index(columnNames = "lastupdate", name = "lastupdate")}
)
但org.hibernate.annotations.Index; 仍被标记为已弃用。那么使用它是否是一种好习惯?如果不是,还有什么选择,因为显然javax.persistence.Index 不起作用。
org.hibernate.annotations.Index; 适用于每个值:创建、更新、...
javax.persistence.Index 与“hibernate.hbm2ddl.auto”的哪个值无关,不起作用。
【问题讨论】:
标签: java hibernate postgresql jpa