如何正确使用Hibernate @Index注解?

发布于 2024-11-19 08:22:03 字数 1331 浏览 4 评论 0原文

我有一个用作实体的 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;

}

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-11-26 08:22:03

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

您还可以尝试在列字段上使用 @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;
}

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

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

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;
}
双手揣兜 2024-11-26 08:22:03

我有同样的问题,但我找到了它的解决方案,它对我来说工作正常,尝试一下,它可能会帮助你

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

这工作正常,试试吧

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文