指定索引(非唯一键)使用JPA [英] Specifying an Index (Non-Unique Key) Using JPA
问题描述
你如何定义一个字段,例如 email
作为使用JPA注释的索引。我们在电子邮件
上需要一个非唯一的密钥,因为每天在这个字段上有几百万个查询字符,而且它没有密钥有点慢。
@Entity
@Table(name =person,
uniqueConstraints = @ UniqueConstraint(columnNames = {code, uid}))
public class Person {
//唯一代码和uid
public String code;
public String uid;
public String username;
公共字符串名称;
public String email;
}
我已经看到了一个hibernate特定的注释,但我试图避免供应商特定的解决方案因为我们仍然在hibernate和datanucleus之间做出决定。
更新:
从JPA 2.1开始,你可以做这个。请参阅:此位置不允许使用注解@Index
据我所知,没有跨JPA-Provider的方式来指定索引。但是,您始终可以直接在数据库中创建它们,大多数数据库将在查询计划期间自动选取它们。
How do you define a field, eg email
as having an index using JPA annotations. We need a non-unique key on email
because there are literally millions of queries on this field per day, and its a bit slow without the key.
@Entity
@Table(name="person",
uniqueConstraints=@UniqueConstraint(columnNames={"code", "uid"}))
public class Person {
// Unique on code and uid
public String code;
public String uid;
public String username;
public String name;
public String email;
}
I have seen a hibernate specific annotation but I am trying to avoid vendor specific solutions as we are still deciding between hibernate and datanucleus.
UPDATE:
As of JPA 2.1, you can do this. See: The annotation @Index is disallowed for this location
As far as I know, there isn't a cross-JPA-Provider way to specify indexes. However, you can always create them by hand directly in the database, most databases will pick them up automatically during query planning.
这篇关于指定索引(非唯一键)使用JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!