2015-06-10 41 views
1

我的應用程序使用Spring Boot/JPA/MongoDB。Mongo @DBRef unique

我使用

org.springframework.data.mongodb.core.mapping.Document; 
org.springframework.data.mongodb.core.index.Indexed; 
org.springframework.data.mongodb.core.mapping.DBRef; 

一切都很好,我的域名類映射到MongoDB的努力使DBREF獨特時除外:

@DBRef @Indexed(unique = true) 
private User owner; 

我已經試過@DBRef,@Indexed(的不同組合unique = true)並且不能使DBRef具有唯一性。我可以做其他字段類型獨特,如「名」在下面的例子中

@Indexed(unique = true) 
@Size(min = 2, max = 100) 
@Column(length = 100) 
private String name; 

,但無法找到如何使我的DBREF領域獨樹一幟。

+0

出於興趣:請問該怎麼做索引查找數據庫級('colleciton.getIndexes()') – xeraa

+0

@xeraa - 請參閱下面的代碼。我用smeCode替換了名字不好的'name'字段。 DBRef所有者是違規變量。我用db.NT_SMES.getIndexes(),所有者沒有被索引。 smeCode和_id被列爲索引。 (對不起,沒有發佈代碼 - 我無法得到評論格式正確) – mikec

回答

0

我從映射的嗎啡那邊傳來的,但我想嘗試這樣的:

@CompoundIndexes({ 
    @CompoundIndex(name = "owner", def = "{'owner.id' : 1}", unique = true) 
}) 
+0

Thx @xeraa。複合指數不是我想要的。我想將一個DBRef變量標記爲唯一。 – mikec

+0

你試過了嗎?這應該進入正確的方向:http://stackoverflow.com/a/15819034/573153 既然你不想索引所有者,但所有者的ID,你需要一些解決方法。因此,您需要某種類關聯關係,因爲屬性關聯沒有達到它 – xeraa

+0

@xeraaI確實嘗試過,並在不同的地方放置了「獨特」。仍然不能讓'所有者'變得獨一無二。我將根據您的鏈接檢查MongoTemplate文檔。非常感謝 – mikec