2012-08-10 11 views
-1

Grail 도메인 클래스를 채우기위한 간단한 코드와 db 내용을 보여주는 코드를 작성했지만 작동하지 않습니다. 첨부 된 내 코드입니다! 어떤 도움을 주셔서 감사합니다.Grails 도메인 클래스 채우기

def populateDB(int nofelements) 
    { 
     def instance 
     for (int i=1;i<=nofelements;i++){ 
      instance=new Avendpoint() 
      instance.avName=org.apache.commons.lang.RandomStringUtils.random(9, true, true) 
      instance.bridge=org.apache.commons.lang.RandomStringUtils.random(9, true, true) 
      instance.callerID=org.apache.commons.lang.RandomStringUtils.random(9, true, true) 
      instance.con=false 
      instance.state=AvendpointState.ONE_WAY 
      instance.uid=org.apache.commons.lang.RandomStringUtils.random(5, true, true) 
      instance.save(flush: true) 
     } 

     render "The database has been populated successfully!" 

    } 
    def showDB(){ 
     def instance 
     String res 
     res+=Integer.toString(Avendpoint.count()) 
     for(int i=1; i<Avendpoint.count(); i++){ 
     instance=Avendpoint.get(i) 
     res+=instance.avName+"<br>"+instance.bridge+"<br>"+instance.callerID+"<br>"+ 
     instance.con+"<br>"+instance.state+"<br>"+instance.uid+"<br>" 
     } 
     render res 
    } 
+2

한숨 ... "작동하지 않습니다"는 의미는 무엇입니까? 오류가 있습니까? – hvgotcodes

+0

그것은 무엇을합니까? 오류가 발생 했습니까? Grails의 어떤 버전입니까? 'populateDB'를 어떻게 호출하고 있습니까? 일반적인 경우 BootStrap.groovy에서 개발 모드에 있는지 확인하고 거기에 데이터를 삽입하십시오. 또한 유효성 검사 문제 일 수 있으므로'instance.save (failOnError : true, flush : true) '를 시도해보십시오. –

+0

아니요, 오류는 없지만 db에는 아무 것도 없습니다! – Reza

답변

1

우선 사용하려고입니다 : instance.save(flush: true,failOnError:true). 기본적으로 Grails는 도메인이 필드 제약 조건을 갖지 않을 때 예외를 throw하지 않습니다. 어쩌면 인스턴스가 유효성 검사 중에 거부되고 저장되지 않을 수도 있습니다.

관련 문제