休眠:创建索引 [英] Hibernate: Create Index

查看:120
本文介绍了休眠:创建索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的数据库中创建几个索引。不幸的是,我们必须将持久性提供程序从EclipseLink更改为Hibernate,但使用javax.persistence.Index的解决方案也无法使用Hibernate的解决方案。



这就是类看起来像:

  @Entity 
@Table(name =my_shop)
public class Shop扩展BaseEntity {

@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
私有日历lastUpdate;
}

这应该是javax.persistence。*的解决方案:

  import javax.persistence.Index; 
import javax.persistence.Table;
$ b $ @ bable(name =my_shop,
indexes = @Index(columnList =lastupdate)

Hibernate注释已被弃用,所以必须有一个不使用这些注释的原因:

  import org.hibernate.annotations.Index; //弃用
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 =dialectvalue =org.hibernate.dialect.PostgreSQLDialect/>
...

只有EclipseLink可以轻松应对:

  import org.eclipse.persistence.annotations.Index; 

@Entity
@Table(name =my_shop)
public class Shop扩展BaseEntity {

@Index
@Temporal TemporalType.TIMESTAMP)
@Column(nullable = false)
私人日历lastUpdate;
}

我测试了所有组合的lastupdate,lastUpdate和@Column和@Index中额外的名称属性,但似乎没有任何工作。



希望有人能帮助我,
干杯!



更新1



坦克你的帮助!
实际上这个解决方案的工作原理:
$ b $ $ p $ @ javax.persistence.Table(name =my_shop)
@Table(applicableTo =my_shop
,indexes = {@Index(columnNames =name,name =name),
@Index(columnNames =lastupdate,name =lastupdate )}

但仍然 org.hibernate.annotations。 Index; 被标记为已弃用。那么使用它还是不好?如果不是什么替代方法,因为显然 javax.persistence.Index 不起作用。

org.hibernate.annotations.Index; 适用于每个值:create,update,...
javax.persistence.Index hibernate.hbm2ddl.auto具有哪些值并不重要。

解决方案

我使用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

虽然我的配置有

 < property name =hibernate.hbm2ddl.autovalue =update/> 

这就是我的实体映射

  import javax.persistence.Entity; 
import javax.persistence.Index;
import javax.persistence.Table;
$ b $ @Entity
@Table(name =users,indexes = {
@Index(columnList =id,name =user_id_hidx),
@Index(columnList =current_city,name =cbplayer_current_city_hidx)
})

PS。实际上,我在注释中遇到了一些问题。我无法为索引指定表空间,并且必须为父类的子类为SINGLE_TABLE层次结构创建indecies。


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.

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.*:

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:

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+".

This what my persistence.xml looks like:

...
    <property name="hibernate.hbm2ddl.auto" value="create"/>
    <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...

Only EclipseLink can handle this easily:

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.

Hope that someone can help me, cheers!

Update 1

Tank you for your help! 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; works with every value: create, update, ... javax.persistence.Index doesn't matter which value "hibernate.hbm2ddl.auto" has, doesn't work.

解决方案

I use JPA 2.1 with Hibernate 4.3 and PostgreSQL 9.3. I have no problems with indexes

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.

这篇关于休眠:创建索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆