java 非空属性引用一个瞬态值瞬态实例必须在当前操作之前保存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32794600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not-null property references a transient value transient instance must be saved before current operation
提问by vijaykumar g
i am having the table productin that table one of the foreign key dimension_id. so while writing my test case in service layer it shows the error.
this is my test case
我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;
}
this is my dimensionimpl
这是我的维度impl
@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);
}
}
this is my dimensionDAOimpl
这是我的维度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
DO 到 BO 映射
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 to DO mapping
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;
}
回答by malaguna
The exception that you are getting Not-null property references a transient value transient instance must be saved before current operationmeans that while you are persisting dimensionDOobject, there is a relation of thi object that references a non-persisted (that is a transient) object, and thus it can not manage foreign key.
你得到的例外Not-null property references a transient value transient instance must be saved before current operation意味着当你持久化dimensionDO对象时,有一个引用非持久化(即瞬态)对象的对象的关系,因此它无法管理外键。
Your code doesn't show where it can be this relation. Maybe when you map from dimensioBOto dimensionDOyou create a new instance that you have to persist before.
您的代码没有显示这种关系的位置。也许当你映射dimensioBO到dimensionDO你时,你创建了一个你必须坚持的新实例。
On the other hand, in productBOyou have two reference to new instances, one for sellerBOand one for dimensionBO. But you don't show code for product persistence ...
另一方面,productBO您有两个对新实例的引用,一个 forsellerBO和一个 for dimensionBO。但是您没有显示产品持久性的代码......

