2013-08-18 2 views
1

Grails MongoDB 플러그인을 사용하여 MongoDB에 포함 된 문서/개체 목록을 저장하는 데 문제가 있습니다. 제 3 장의 documentation에 주어진 정보를 사용했지만, 단지 하나의 객체가 임베드되어 있습니다.MongoDB에 포함 된 문서 목록을 Grails에 저장하는 방법은 무엇입니까?

테스트 목적으로 새로운 Grails 프로젝트에서 Person과 Address라는 두 개의 도메인 객체를 만들었습니다. 그들은 다음과 같이 :

class Person { 
    ObjectId id 
    String firstName 
    String lastName 
    Address address 
    List  otherAddresses = [] 
    static embedded = ['address', 'otherAddresses'] 
} 

class Address { 
    String street 
    String postCode 
    String city 
} 

나는 그것이 MongoDB를 두 사람 객체를 저장 Bootstrap.groovy에 다음 줄을 실행

- 모두가 올바른 주소를 가지고 있지만 "[널]"PERSON1 otherAddresses 목록이와 PERSON2에 otherAddresses List는 "[{"street ":"Second Street. (164) ","도시 ":"뉴욕 ","우편 번호 ":"13579 "}]"

def address = new Address(street: "Mainstreet. 164", city: "New York", postCode:"12345") 
def person1 = new Person(firstName: "John", lastName: "Doe") 
person1.address = address 
person1.otherAddresses.add(address) 
println person1.otherAddresses // Result: "[mongoembeddedlisttest.Address : (unsaved)]" 
person1.save() 
person1.errors.allErrors.each { println it } // no errors 

def person2 = new Person(firstName: "Jane", lastName: "Doe") 
person2.otherAddresses += ['street': 'Second Street. 164', 'city': 'New York', 'postCode':'13579'] 
println person2.otherAddresses // Result: "[[street:Second Street. 164, city:New York, postCode:13579]]" 
person2.save() 

결과 데이터베이스 항목 :

{ "_id" : { "$oid" : "521089461a150b20390d61c2"} , "address" : { "city" : "New York" , "postCode" : "12345" , "street" : "Mainstreet. 164"} , "firstName" : "John" , "lastName" : "Doe" , "otherAddresses" : [ null ] , "version" : 0} 
{ "_id" : { "$oid" : "521089461a150b20390d61c3"} , "firstName" : "Jane" , "lastName" : "Doe" , "otherAddresses" : [ { "street" : "Second Street. 164" , "city" : "New York" , "postCode" : "13579"}] , "version" : 0} 

또한 주 :

  • 나는 순수한 mongodb 접근법을 사용하고있다. (하이버 네이트와 함께 하이브리드는 없다.)

  • 나는 윈도우 8 머신 Grails 2.2.1을 사용하여 mongo db를 실행합니다. 2.4.4

  • Person은/grails-app/domain의 도메인 객체이고 Address는/src/groovy의 "일반"groovy 클래스입니다 (도메인 폴더 grails.gorm.default.constraints = { "*"(널 : TRUE)}

  • BuildConfig.groovy가 보유하지만

  • 모든 것은 Config.groovy 파일에 널로 설정된다) 아무 효과가 없다 플러그인 입력 : 컴파일 ": mongodb : 1.3.0"

내가 뭘 잘못하고 있니? 어떻게 Grails 메커니즘을 사용하여 임베디드 객체의 목록을 저장할 수 있습니까?

답변

0

주소 개체 대신 두 번째 사람에게 맵을 추가한다고 생각합니다. 각 사람마다 otherAddress를 추가하는 이유가 있습니까? 나는 그것을 테스트하지 않았습니다 있지만

나는이 일을해야한다고 생각 :

def address = new Address(street: "Mainstreet. 164", city: "New York", postCode:"12345") 
def person1 = new Person(firstName: "John", lastName: "Doe") 
person1.address = address 
person1.otherAddresses.add(address) 
println person1.otherAddresses // Result: "[mongoembeddedlisttest.Address : (unsaved)]" 
person1.save() 
person1.errors.allErrors.each { println it } // no errors 

def person2 = new Person(firstName: "Jane", lastName: "Doe") 
person2.otherAddresses += new Address('street': 'Second Street. 164', 'city': 'New York', 'postCode':'13579') 
println person2.otherAddresses 
person2.save() 
관련 문제