Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not-null property references a transient value transient instance must be saved before current operation [duplicate]

Tags:

java

hibernate

i am having the table product in 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

@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

@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

@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 to DO mapping

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;
    }
like image 218
vijaykumar g Avatar asked Nov 23 '25 17:11

vijaykumar g


1 Answers

The exception that you are getting Not-null property references a transient value transient instance must be saved before current operation means that while you are persisting dimensionDO object, 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.

Your code doesn't show where it can be this relation. Maybe when you map from dimensioBO to dimensionDO you create a new instance that you have to persist before.

On the other hand, in productBO you have two reference to new instances, one for sellerBO and one for dimensionBO. But you don't show code for product persistence ...

like image 88
malaguna Avatar answered Nov 26 '25 11:11

malaguna