2014-09-11 3 views
0

을 저장 한 후 해당 ID에 설정된 도메인 객체의 속성을 따라가 : Grails가Grails는 (StaleObjectStateException) - 나는 다음과 같은 도메인 클래스가 있다고 가정

@Entity 
class TestDomain { 

    public static final ID_PREFIX = "prefix-" 

    String uniqueId 

    constriaints = { 
     uniqueId nullable: true 
    } 
} 

는 도메인 클래스는 또한 id 속성을 가지고 않습니다 기본. 이 다음에 나는 uniqueId을 설정하여 새로운 TestDomain 개체를 만들 때 uniqueId 속성에 첫 번째 개체의 경우 prefix-1, 두 번째 개체의 경우 prefix-2 등이 포함됩니다.

내 접근 방식은 실현했다는 TestDomainControllersave 행동에서 :

def save(TestDomain testDomainInstance) { 
    if (testDomainInstance == null) { 
     notFound() 
     return 
    } 

    if (testDomainInstance.hasErrors()) { 
     respond testDomainInstance.errors, view:'create' 
     return 
    } 

    testDomainService.save(testDomainInstance) //.save flush:true 
    testDomainInstance.uniqueId = TestDomain.ID_PREFIX + testDomainInstance.id 
    testDomainService.save(testDomainInstance) //.save flush:true 

    request.withFormat { 
     form multipartForm { 
      flash.message = message(code: 'default.created.message', args: [message(code: 'TestDomain.label', default: 'Test Domain'), testDomainInstance.id]) 
      redirect testDomainInstance 
     } 
     '*' { respond testDomainInstance, [status: CREATED] } 
    } 
} 

그러나, 나는 다음과 같은 오류 얻을 객체 저장하면 : 이제

StaleObjectStateException occurred when processing request: ... 
Message 
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [test.TestDomain#1] 

을, 내가 무엇을 할 수를 이걸 달리는거야? 또는 더 아름답게 uniqueId의 값을 설정할 수 있습니까? 현재, 나는 조금 붙어있다.

도움 주셔서 감사합니다.

편집 :

지금 거기 uniqueId을 설정하기 위해 처음 save 후 다른 작업에 redirect에 노력했다. 그러나, 나는 동일한 오류가 발생합니다. testDomainService 안에 save(flush: true)을 사용해도 문제는 전혀 변경되지 않습니다.

다른 접근법은 첫 번째 저장, 변경 및 두 번째 저장을 testDomainServicesave 메서드에 넣었지만 성공하지 못했습니다. 동일한 오류가 발생했습니다. 이 서비스는 트랜잭션입니다.

일반적으로 도메인 개체를 저장하고 변경 한 다음 동일한 트랜잭션 내에서 다시 저장할 수 있습니까?

답변

0
@Entity 
class TestDomain { 
    public static final ID_PREFIX = "prefix-" 
    String uniqueId 
    constriaints = { 
     uniqueId nullable: true 
    } 
    def afterInsert() { 
    uniqueId = ID_PREFIX + id 
    } 
} 
+0

시도했지만 동일한 오류 (StaleObjectStateException)가 발생했습니다 ... 응용 프로그램에서 작동 했습니까? – gabriel

+0

org.hibernate.StaleObjectStateException는 동일한 객체가 동시에 두 개의 스레드에 의해 수정되는 경우에옵니다.이 문서를 읽으십시오 .http : //www.oodlestechnologies.com/blogs/What-is-Optimistic-and-Pessimistic-Locking%3F –

관련 문제