2012-03-20 4 views
4
내가 여기에 최대 절전 모드 테스트입니다

상황과 코드입니다.는 최대 절전 모드 객체 상태

case Bsession.update(tag);의 주석을 달아주세요. 이제는 작동 중입니다. 객체가 case B 트랜잭션에 병합되지 않아 오류가 발생합니다.

우리는 우리가 필요 병합하지하는 이유입니다 factory.getCurrentSession()를 사용하지만, 경우에하는 factory.openSession();로 교체하고 (case B에 업데이 트를 호출하여)가 여전히 작동 각각의 경우 후 세션을 닫는 말할 수 있습니다. 그래서 어떤 의미에서 우리는 객체가 분리되어 있다고 부릅니까?

답변

3

사례 A : 세션이 닫히지 않았으며 객체 tag이 지속 상태이고 태그 객체가 현재 세션에 연결되어 있습니다.

사례 B : 여기서 세션은 첫 번째 트랜잭션과 동일 할 수 있으며, 지속 상태에있는 tag 개체의 값을 변경합니다. Persistent state represents existence of object in permanent storage. There will be connection between the object in memory and in database through the identifier. Any change in either of these two will be reflected in other (when transaction is committed). Persistent state is dependent on session object. First, session has to be open (unclosed), and second, the object has to be connected to the session. If either of these two is not true, then the object moves into either transient state or detached stage.

객체는 다음과 같은 경우에 분리 된 상태에서 [이 귀하의 경우 사실이다] :

Hibernate는 객체의 세 가지 상태 구분 : 객체의 상태에 대한 Detached state arises when a persistent state object is not connected to a session object. No connection may be because the session itself is closed or the object is moved out of session.

0

영구를, 과도기 및 파문.

  1. 개체의 일시적인 상태 - 절대로 Hiberbate 세션과 관련이없는 개체입니다. 일반적으로 이는 데이터베이스에 표현이없고 식별자 값이없는 영구 클래스의 새 인스턴스입니다.

  2. 개체의 영구 상태 - 현재 Hiberbate 세션과 연결되어 있으며 데이터베이스에 표시되며 식별자 값을 가진 개체입니다.

  3. 개체의 분리 된 상태 - 영구 상태에서 이동하고 데이터베이스에 표현 된 개체입니다. 세션이 닫히면 개체의 상태가 영구에서 변경된 상태로 변경됩니다.

예 :

... 
// Tag in a transient state 
Tag tag = new Tag(); 
tag.setName("A"); 

// Tag in a persistent state 
Long id = (Long) session.save(tag); 

// Tag in a detached state 
session.close(); 
...