如何正确使用Hibernate @Index注解?
我有一个用作实体的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
单个 @Table 注释可以工作吗?我还没有尝试过,我猜 Hibernate @Table 可能会被 JPA @Table 覆盖。
您还可以尝试在列字段上使用 @Index 注释:
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:
我有同样的问题,但我找到了它的解决方案,它对我来说工作正常,尝试一下,它可能会帮助你
这工作正常,试试吧
i have the same problem, but i found it's solution and it works fine with me try it, it may help you
This works fine just try it