如何用morphia注释复合键上的索引

cub*_*buk 3 mongodb morphia mongodb-java

我正在使用morphia github 1.2.2版.我知道如何在morphia实体上的一个字段上注释索引,但有没有办法在两个字段的组合上注释索引.例如,如果我想在字段a和b上有一个复合索引,那么下一个类的注释是什么.

@Entity
public class TestClass
{
    @Property("a")
    private int fieldA;
    @Property("b")
    private int fieldB;
    //how to annotate index of compound key fieldA and fieldB using morphia index annotation?
}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

xer*_*raa 8

@Entity
@Indexes(@Index(name = "aAndB", value = "a, b"))
public class TestClass
{
    @Property("a")
    private int fieldA;
    @Property("b")
    private int fieldB;
    //how to annotate index of compound key fieldA and fieldB using morphia index annotation?
}
Run Code Online (Sandbox Code Playgroud)

为索引指定名称是可选的.您还可以使复合索引唯一.

  • Morphia 1.0的语法已经改变:`@Indexes(@Index(fields = {@Field(Chat.USER_ID_1),@ Field(Ch.USER_ID_2)},options = @IndexOptions(unique = true))) (3认同)