Grails 쉘이 도메인 객체를 볼 수 없습니다
-
19-09-2019 - |
문제
저는 grails 초보자(그리고 멋진 초보자)이고, 몇 가지 grails 튜토리얼을 진행하고 있습니다.새로운 사용자로서 grails 쉘은 나에게 정말 유용한 작은 도구이지만, 내 클래스와 개체를 표시하는 방법을 알 수 없습니다.내가 시도하는 것은 다음과 같습니다.
% grails create-app test
% cd test
% grails create-domain-class com.test.TestObj
% grails shell
groovy:000> new TestObj()
ERROR org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, groovysh_evaluate: 2: unable to resolve class TestObj
나는 grails 셸이 모든 컨트롤러, 서비스 및 도메인 개체를 볼 수 있다는 인상을 받았습니다.이게 무슨 일이야?여기서 다른 작업을 수행해야 합니까?
나는 다른 것을 시도했습니다.
groovy:000> foo = new com.test.TestObj();
===> com.test.TestObj : null
groovy:000> foo.save
ERROR groovy.lang.MissingPropertyException: No such property: save for class: com.test.TestObj
내가 도대체 뭘 잘못하고있는 겁니까?
편집하다:알겠습니다. 전체 이름을 사용하고 다음을 사용하는 것에 대한 답변을 확인했습니다. .save()
대신에 .save
.하지만 이건 어때요?
groovy:000> new com.test.TestObj().save()
ERROR org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
이번에는 내가 뭘 잘못한 걸까?
해결책
나는 쉘 대신 콘솔을 사용하라는 Burt의 조언에 두 번째로 동의합니다.예외에 관하여:
groovy:000> new com.test.TestObj().save()
ERROR org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
트랜잭션을 사용하여 이 코드를 명시적으로 실행해 볼 수 있습니까?
import com.test.TestObj
TestObj.withTransaction{ status ->
TestObj().save()
}
다른 팁
서로 다른 패키지에 동일한 이름을 가진 두 개의 도메인 클래스를 갖는 것이 가능하기 때문에(좋은 생각은 아니지만) 패키지가 필요합니다.
두 번째 세션에서는 foo.save가 아니라 foo.save()여야 합니다.
저는 콘솔을 선호합니다. 작업하기가 훨씬 쉽습니다.'grails console'을 실행하면 Swing 앱이 시작됩니다.Spring 애플리케이션 컨텍스트인 암시적 'ctx' 변수를 사용할 수 있다는 점에서 일반 Groovy 콘솔과 약간 다릅니다.이를 사용하여 "ctx.getBean('fooService')"를 통해 서비스 및 기타 Spring Bean에 액세스할 수 있습니다.
너는해야 할거야 import com.test.TestObj
또는 참조하여 new com.test.TestObj()
당신이 보여준 것처럼.
참고하세요'save
'는 속성이 아니라 Grails가 런타임에 도메인 클래스를 장식하는 동적 메서드입니다.
groovy:000> foo = new com.test.TestObj();
===> com.test.TestObj : null
groovy:000> foo.save()
===> com.test.TestObj : 2
groovy:000>