grails gorm 문제 : 객체 참조 구원받지 않은 과도 인스턴스
문제
아래의 성배 코드는 시도 할 때 다음과 같은 예외를 던집니다. .save()
Foo Object :
org.hibernate.TransientObjectException/
org.springframework.dao.InvalidDataAccessApiUsageException:
object references an unsaved transient instance -
save the transient instance before flushing: Bar
HTTP 매개 변수에서 도메인 객체를 자동으로 채우는 것과 관련하여 Gorm 시맨틱 중 일부가 누락 된 것 같습니다.
내 질문은 단순히 다음과 같습니다.
- 예외를 얻지 않고 Foo 객체를 채우고 저장하는 올바른 방법은 무엇입니까?
모델:
class Foo {
Bar bar
}
보다:
<g:form id="${foo.id}">
<g:select name="foo.bar.id" from="${Bar.list()}" />
</g:form>
제어 장치:
class FooController {
def fooAction = {
Foo foo = new Foo(params)
foo.save()
[ foo: foo ]
}
}
해결책
Foo의 맥락에서만 'bar'가 존재하는 경우 Bar.groovy에 다음 줄을 추가하십시오.
class Bar {
static belongsTo = Foo
}
'바'가 다른 맥락에서 사용되는 경우 foo.groovy에서 사용할 수 있습니다.
class Foo {
Bar bar
static mapping = {
bar cascade:'all-delete-orphan'
}
}
제휴하지 않습니다 StackOverflow