Java 如何正确使用 Hibernate @Index 注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6633432/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:26:41  来源:igfitidea点击:

How to use Hibernate @Index annotation properly?

javahibernategrailsindices

提问by

i have a java class used as an entity that has 2 classes that inherit from it. this class has some indices but these indices didn't appear in the database. this is my java super class code

我有一个用作实体的 java 类,它有 2 个继承自它的类。这个类有一些索引,但这些索引没有出现在数据库中。这是我的 java 超类代码

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.persistence.OneToMany;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="service", uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
@org.hibernate.annotations.Table(appliesTo = "service", 
                                 indexes = { @Index(name = "service_name", columnNames = { "name" }), 
                                 @Index(name = "service_description", columnNames = { "description" }),
                                 @Index(name = "service_accessNumber", columnNames = { "access_number" })   
                     })
public class Service implements Serializable {

@Column(name="access_number",length = 95,nullable=false)
    String accessNumber;

@Column(length=80,nullable=false)
    String name;

@Column(length=140)
    String description;

}

does any one know what is my problem Note: i have this problem in my all java classes but this is one of them. the code in all class is the same of this

有谁知道我的问题是什么 注意:我的所有 Java 类都有这个问题,但这是其中之一。所有类中的代码都与此相同

Edit: i build an xml file and put it in a grails project, and when i run this project, database created

编辑:我构建了一个 xml 文件并将其放在一个 grails 项目中,当我运行这个项目时,创建了数据库

采纳答案by Georgian Citizen

i have the same problem, but i found it's solution and it works fine with me try it, it may help you

我有同样的问题,但我找到了它的解决方案,它对我来说很好用试试吧,它可能对你有帮助

in your DataSource.groovy file in your grails project make sure that under 
environment dbCreate is not equal to "update": if it is equal to "update", change 
it to "create".

This works fine just try it

这很好用,试试吧

回答by Xiè Jìléi

Would a single @Table annotation work? I haven't tried it, I guess the Hibernate @Table might be overridden by JPA @Table.

单个@Table 注释会起作用吗?我还没有尝试过,我猜 Hibernate @Table 可能会被 JPA @Table 覆盖。

You may also try @Index annotation on the column fields:

您也可以在列字段上尝试 @Index 注释:

public class Service implements Serializable {
    @Index(name="service_accessnumber")
    @Column(name="access_number",length = 95,nullable=false)
    String accessNumber;

    @Index(name="service_name")
    @Column(length=80,nullable=false)
    String name;

    @Index(name="service_description")
    @Column(length=140)
    String description;
}