1
複合オブジェクトを保存するときにこの例外が発生し続けます。ここでorg.hibernate.TransientObjectException:1対1の関係を正しく定義する方法?
@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メソッドである:ここで
@Override
public void insert(StoreHouse sh) {
sessionFactory.getCurrentSession().persist(sh);
}
は私のテストcalssです:
@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);
}
}
まだここ が私のマッピング..ですそれを克服する方法をfigreすることはできません
定義済みテストデータのフラグメント:
<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>
私はここでカスケードを定義するときに嫌な気がします。しかし、それを修正する手助けはできますか?
@Cascade({org.hibernate.annotations.CascadeType.ALL})を定義したとき、expectedIngredientはexpectedStoreHouseと共に削除されます。したがって、StoreHouseを削除した後にexpectedStoreHouse.getIngredient()を呼び出すことはできません。削除カスケードタイプを除外するか、単体テストを変更する必要があります。 – xl0e
お返事ありがとうございます!私はテストでセクションを削除し、同じ問題を持っています(。ログは、挿入メソッドが失敗し、実際に最初の場所に成分を挿入しないことを示しています:オブジェクトは未保存の一時的なインスタンスを参照しています。 .model.Ingredient。だから、カスケードのマップでは、問題が隠れてしまい、単に期待通りには動作しません... –