I first created an issue at spring-data-mongo https://jira.springsource.org/browse/DATAMONGO-488 and it appears that the issue come from the mongo Java Driver.
Following my test code :
Mongo mongo = new Mongo("localhost" , 27017); DB db = mongo.getDB("test"); DBCollection sites = db.getCollection("sites"); DBCollection users = db.getCollection("users"); BasicDBObject site = new BasicDBObject(); ObjectId siteId = new ObjectId(); site.put("_id", site); site.put("name", "site"); WriteResult result = sites.insert(site); BasicDBObject user = new BasicDBObject(); ObjectId userId = new ObjectId(); user.put("_id", userId); user.put("name", "user"); user.put("site", new DBRef(db, site)); // throws a StackoverflowError
Is this a proper use of DBRef or does cycle reference not allowed ?
The incriminated stacktrace :
java.lang.StackOverflowError at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:362) at java.util.LinkedHashMap$LinkedHashIterator.<init>(LinkedHashMap.java:362) at java.util.LinkedHashMap$KeyIterator.<init>(LinkedHashMap.java:400) at java.util.LinkedHashMap$KeyIterator.<init>(LinkedHashMap.java:400) at java.util.LinkedHashMap.newKeyIterator(LinkedHashMap.java:413) at java.util.HashMap$KeySet.iterator(HashMap.java:887) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1170) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) at com.mongodb.DBCollection._checkKeys(DBCollection.java:1174) ...