2017-03-20 9 views
1

복합 개체를 저장할 때이 예외가 계속 발생합니다. 그럼에도 불구하고 그것을 극복하는 방법을 figre 수 없습니다 .. 것은 여기 내 매핑 :org.hibernate.TransientObjectException : 일대일 관계를 올바르게 정의하는 방법?

여기
@Entity 
@Table(name = "store_house") 
public class StoreHouse implements Serializable { 

    // constructors 

    @Id 
    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({org.hibernate.annotations.CascadeType.ALL}) 
    @JoinColumn(name="ING_ID", unique=true, nullable=false, updatable=false) 
    private Ingredient ingredient; 

    @Column(name = "QUANTITY") 
    private double quantity; 
} 

// getters and setters 

내가이 예외가 DAO 방법입니다 :

@Transactional 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"/test-context.xml", "/test-data.xml"}) 
public class StoreHouseDAOTest { 

    @Autowired 
    private SessionFactory sessionFactory; 

    @Autowired 
    private StoreHouseDAO storeHouseDAO; 

    @Autowired 
    private StoreHouse expectedStoreHouse; 

    @Test 
    public void itShouldPerformCRUDSmoothly() { 
      // CREATE 
      storeHouseDAO.insert(expectedStoreHouse); 
      sessionFactory.getCurrentSession().flush(); 
      // READ 
      StoreHouse actualStoreHouse = storeHouseDAO.getByIngredient(expectedStoreHouse.getIngredient()); 
      assertEquals(actualStoreHouse.getIngredient().getId(), expectedStoreHouse.getIngredient().getId()); 
      // DELETE 
      storeHouseDAO.delete(expectedStoreHouse); 
      sessionFactory.getCurrentSession().flush(); 
      StoreHouse emptyStoreHouse = storeHouseDAO.getByIngredient(expectedStoreHouse.getIngredient()); 
      assertNull(emptyStoreHouse); 
    } 
} 
: 여기
@Override 
    public void insert(StoreHouse sh) { 
     sessionFactory.getCurrentSession().persist(sh); 
    } 

내 테스트 CALSS입니다

정의 된 테스트 데이터 조각 :

<bean id="expectedIngredient" class="com.restaurant.model.Ingredient"> 
     <property name="name" value="TestIngredient"/> 
     <property name="unit" value="expectedUnit"/> 
    </bean> 

    <bean id="expectedStoreHouse" class="com.restaurant.model.StoreHouse"> 
     <property name="ingredient" ref="expectedIngredient"/> 
     <property name="quantity" value="10"/> 
    </bean> 

여기에 계단식을 정의 할 때 나는 무척이나 상처를 입었습니다.하지만 그것을 바로 잡도록 도와 주시겠습니까?

+0

@Cascade ({org.hibernate.annotations.CascadeType.ALL})를 정의한대로 expectedIngredient는 expectedStoreHouse와 함께 삭제됩니다. 따라서 StoreHouse를 제거한 후에 expectedStoreHouse.getIngredient()를 호출 할 수 없습니다. 삭제 캐스케이드 유형을 제외하거나 단위 테스트를 변경해야합니다. – xl0e

+0

답변 해 주셔서 감사합니다! 나는 테스트 섹션을 삭제하고 동일한 문제 ((. 로그는 삽입 메소드가 실패하고 처음에 성분을 삽입하지 않는다는 것을 보여줍니다. 객체는 저장되지 않은 일시적인 인스턴스를 참조합니다.) - 일시적인 인스턴스 beforeQuery를 저장합니다. 플러시 : com.restaurant .model.Ingredient. 문제는 다른 곳에서 숨기고, 아마도 캐스케이드지도 작성에서 예상대로 작동하지 않습니다 ... –

답변

0

필자는 모델을 단순화하는 비용으로 작동하도록했습니다.

@Entity 
@Table(name = "store_house") 
public class StoreHouse implements Serializable { 

    // constructors 

    @Id 
    @GeneratedValue(generator = "increment") 
    @GenericGenerator(name = "increment", strategy = "increment") 
    @Column(name = "SH_ID") 
    private Long id; 

    @OneToOne(fetch = FetchType.EAGER) 
    @Cascade({org.hibernate.annotations.CascadeType.ALL}) 
    @JoinColumn(name="ING_ID", unique=true, nullable=false, updatable=false) 
    private Ingredient ingredient; 

    @Column(name = "QUANTITY") 
    private double quantity; 

    // getters and setters 
    } 

실제로는 별도의 id 필드를 requieres, 그래서 그것을 만들었습니다. 그리고 그것은 부드럽게 작동하도록 statred. 다른 해결책을 찾지 못했기 때문에 아마 함께 갈 것입니다.