2017-08-07 2 views
6

Ich habe das Modell wie folgtFrühling mongodb Vorlage speichert in demselben Objekt

@CompoundIndexes(value = { 
     @CompoundIndex(name = "catalog_idx", def = "{'code' : 1, 'brand' : 1}", unique = true) }) 
@Document(collection = Catalog.ENTITY) 
public class Catalog extends AbstractModel<String> { 

    private static final long serialVersionUID = 1L; 

    public static final String ENTITY = "catalog"; 

    @NotNull(message = "Code is required") 
    @Field("code") 
    private String code; 

    @NotNull(message = "Brand is required") 
    @DBRef(lazy = true) 
    @Field("brand") 
    private Brand brand; 
} 

Wenn ich mit mongoTemplate.save(object); sparen sehe ich nur zwei Objekte in DB erstellt statt 6. Kurz bevor meine Debug-Linien für Objekte speichern gespeichert werden.

Catalog [code=StagedCatalog, brand=Brand [code=Brand_3]] 
Catalog [code=StagedCatalog, brand=Brand [code=Brand_2]] 
Catalog [code=StagedCatalog, brand=Brand [code=Brand_1]] 
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_2]] 
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_1]] 
Catalog [code=OnlineCatalog, brand=Brand [code=Brand_3]] 

Irgendwelche Ideen warum? Ich finde das Index-Einmalige funktioniert irgendwie nicht. Ich möchte code und brand zu unique combination sein.

public abstract class AbstractModel<ID extends Serializable> implements Serializable { 

    private static final long serialVersionUID = 1L; 

    @Id 
    private ID id; 
} 
+0

Sie haben Code, der Katalog erstellen? Hast du eine "@id" -Spalte im abstrakten Modell? Kannst du das auch protokollieren? – wargre

+0

@wargre tat .... –

Antwort

5

Sie haben einen eindeutigen Index festgelegt. Das bedeutet, dass Sie nicht 2 Dokumente mit demselben Code und derselben Marke haben können.

Jetzt haben Sie die ID-Spalte auf ID-Objekt festgelegt. Die Tatsache, dass Sie 2 Einsatz statt 6 haben bedeutet, dass Sie die gleiche ID für 3 Einsatz verwenden, so etwas wie:

for (code: {"StagedCatalog","OnlineCatalog"}) { 
    ID id=new ID(...); 
    for (brand: {1, 2, 3}){ 
     Catalog cat=new Catalog(); 
     cat.setId(id);    // <<== this is wrong, you reuse the same id, you will insert first brand, then update to brand2 and brand3. 
     cat.setCode(code); 
     cat.setBrand(brand); 
     mongoTemplate.persist(cat); 
    } 
} 

Um das zu verhindern, müssen Sie:

Catalog cat=new Catalog(); 
ID id=new ID(realUniqueId); // RealuniqueId can be code+brand for instance 
cat.setId(id); 
... 
+0

ID wird von Feder mongodb direkt gesetzt. –

+0

Sie sind sicher, dass Sie das gleiche Objekt nicht wiederverwenden? – wargre

+0

Ja, ich bin mir sicher. Sie sehen die Protokolle kurz bevor ich speichere. Jetzt kollidieren keine der Code- und Markenkombinationen. Was ich von zusammengesetzten Schlüsseln gemäß der Federdokumentation verstehe Ich hoffe, dass meine Konfiguration korrekt ist. Zusätzlich, wenn ich alle diese Objekte speichere, sind diese frisch, das bedeutet, dass die ID leer ist vor dem Speichern und nach dem Speichern haben sie einen Wert. Also verstehe ich nicht, warum Spring Mongodb mit der bestehenden Entität spart. Also Frühling mongodb compound Schlüssel einzigartig ich nehme an, die Kombination ist einzigartig und nicht einzelnen Code und Marke sind einzigartig. –