【问题标题】:How to annotate index on compound key with morphia如何用 morphia 注释复合键上的索引
【发布时间】:2013-01-10 20:07:41
【问题描述】:

我正在使用 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?
}

提前致谢。

【问题讨论】:

    标签: mongodb morphia mongodb-java


    【解决方案1】:
    @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?
    }
    

    为索引命名是可选的。您还可以使复合索引唯一。

    【讨论】:

    • 使用这种方法,我在日志中收到警告消息,说我正在使用不推荐使用的方法。这就是我注释我的班级的方式:@Indexes(@Index(value = Chat.USER_ID_1 + "," + Ch.USER_ID_2, unique = true)) public class Chat {}
    • 语法在 Morphia 1.0 中发生了变化:@Indexes(@Index(fields = { @Field(Chat.USER_ID_1), @Field(Ch.USER_ID_2)}, options = @IndexOptions(unique = true)))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多