我在该表中使用表product
作为外键dimension_id
之一。因此,在服务层编写测试用例时,它会显示错误。
这是我的测试用例
@Transactional(propagation = Propagation.REQUIRED)
private ProductBO getProductBO() {
SellerBO sellerBO = getSellerBO();
ProductBO productBO = new ProductBO();
productBO.setCategoryId(1);
productBO.setDateAvailable("20/00/0000");
productBO.setImage("a15cb5e");
productBO.setLocation("for getting product details");
productBO.setMinimum(26.00);
productBO.setModel("service");
productBO.setPoints(5);
productBO.setPrice(12.2f);
productBO.setQuantity("2");
productBO.setSellerBO(sellerBO);
productBO.setShipping(2);
productBO.setSku("aqaqaq");
productBO.setSortOrder("aes");
productBO.setStatus(1);
productBO.setStockStatusId("20");
productBO.setProductName("Micromax");
productBO.setSubtract(20.0001);
productBO.setUpc("asd");
productBO.setViewed(2);
productBO.setWeight("25");
productBO.setWeightClassBO(getWeightClassBO(productBO));
productBO.setDimensionBO(getDimension());
return productBO;
}
public DimensionBO getDimension() {
DimensionBO dimensionBO = new DimensionBO();
dimensionBO.setHeight(12);
dimensionBO.setLength(23);
dimensionBO.setWidth(14);
dimensionBO.setLengthClassBO(getLengthClass());
try {
manageDimensionServiceImpl.addDimension(dimensionBO);
} catch (CrafartServiceException csExp) {
csExp.printStackTrace();
Assert.fail();
}
return dimensionBO;
}
这是我的dimensionimpl
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addDimension(DimensionBO dimensionBO) throws CrafartServiceException {
DimensionDO dimensionDO = beanMapper.mapDimensionBOToDO(dimensionBO, new DimensionDO(), beanMapper.mapLengthClassBOToDO(dimensionBO.getLengthCassBO(), new LengthClassDO()));
try {
dimensionDAOImpl.addDimension(dimensionDO);
dimensionBO.setDimensionId(dimensionBO.getDimensionId());
//LengthClassDO lengthClassDO = lengthClassDAOImpl.getLengthClass(dimensionBO.getLengthCassBO().getLengthClassId());
//dimensionDO.setLengthClassDO(lengthClassDO);
} catch (CrafartDataException e) {
throw new CrafartServiceException("Error while adding dimension", e);
}
}
这是我的尺寸DAOimpl
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void addDimension(DimensionDO dimensionDO) throws CrafartDataException {
try {
Session session = this.getSessionFactory().getCurrentSession();
session.persist(dimensionDO);
} catch (HibernateException hExp) {
throw new CrafartDataException("DB Error while adding dimension details in table", hExp);
}
}
}
DO to BO mapping
public DimensionBO mapDimensionDOTOBO(DimensionDO dimensionDO,DimensionBO dimensionBO,LengthClassBO lengthClassBO) {
dimensionBO.setDimensionId(dimensionBO.getDimensionId());
dimensionBO.setHeight(dimensionBO.getHeight());
dimensionBO.setLength(dimensionBO.getLength());
dimensionBO.setWidth(dimensionBO.getWidth());
dimensionBO.setLengthClassBO(lengthClassBO);
return dimensionBO;
}
BO到DO映射
public DimensionDO mapDimensionBOToDO(DimensionBO dimensionBO, DimensionDO dimensionDO ,LengthClassDO lengthClassDO) {
dimensionDO.setDimensionId(dimensionDO.getDimensionId());
dimensionDO.setHeight(dimensionDO.getHeight());
dimensionDO.setLength(dimensionDO.getLength());
dimensionDO.setWidth(dimensionDO.getWidth());
dimensionDO.setLengthClassDO(lengthClassDO);
return dimensionDO;
}
答案 0 :(得分:0)
您获得Not-null property references a transient value transient instance must be saved before current operation
的异常意味着当您持久化dimensionDO
对象时,该对象的关系引用非持久化(是瞬态的)object,因此无法管理外键。
您的代码并未显示此关系的位置。也许当您从dimensioBO
映射到dimensionDO
时,您需要创建一个必须在之前保留的新实例。
另一方面,在productBO
中,您有两个对新实例的引用,一个用于sellerBO
,另一个用于dimensionBO
。但是,您不会显示产品持久性的代码......