2016-01-12 2 views
4

Скажем, у меня есть MongoDB Entity, который выглядит следующим образом:Как создать составной уникальный индекс в Morphia/MongoDB Java?

@Entity(value = "car") 
public class Car { 
    public String manufacturer; 
    public String model; 
    public String year; 
    public Double price; 
} 

Я хотел бы добавить индекс, что пара manufacturer,model,year уникальна.

Когда я пробую следующую аннотацию - @Indexes(@Index(value="manufacturer,model,year, unique=true)) - она ​​работает. Но это вызывает следующую ошибку:

[WARN] (main) : DatastoreImpl - This index on 'Car' is using deprecated configuration options. Please update to use the fields value on @Index: @org.mongodb.morphia.annotations.Index(unique=true, dropDups=false, background=false, name=, value=manufacturer, model, year, expireAfterSeconds=-1, disableValidation=false, sparse=false, fields=[], [email protected](unique=false, dropDups=false, background=false, name=, expireAfterSeconds=-1, disableValidation=false, language=, languageOverride=, sparse=false)) 

Как правильно настроить индекс?

ответ

10

Попробуйте следующее annotation

@Entity(value = "car") 
@Indexes(@Index(fields = { @Field("manufacturer"), @Field("model"), @Field("year") }, options = @IndexOptions(unique = true))) 
public class Car { 
    public String manufacturer; 
    public String model; 
    public String year; 
    public Double price; 
} 
Смежные вопросы