0

I needed to clone the hybris bean "ProductData", and I couldn't find something OOTB from Hybris that helps with this.

And since hybris beans don't implement Cloneable and don't have constructors, it seems to me that there are only two ways left, either by creating a custom cloning method witch require a lot of dev, or through serialization/deserialization.

Is there another way to do that? and does Hybris provides something OOTB for this? or else is the serialization/deserialization a good approach (in term of performance) for this since there is the SerializationUtils from apache that I can use?

younes elmorabit
  • 343
  • 3
  • 14
  • 1
    Can't you repopulate a new `productData` from the original product instead? cause I can't see another way than creating a custom clone service for that, you can do that by reflection and iterate on `productData` attributes see: https://stackoverflow.com/questions/7746158/how-to-read-object-attribute-dynamically-in-java, and use `modelService.clone` to clone attributes of type itemModel – Benkerroum Mohamed Apr 02 '21 at 11:35
  • Yes, I can create a custom population for this, I was just wondering if there is something from hybris that helps with that, but it seems there is only the modelService.clone that you mentioned and it is for models. what do you think of SerializationUtils.clone is it a good way since it requires no custom dev? – younes elmorabit Apr 02 '21 at 12:32
  • Yep, and you can also use BeanUtils if you just need shallow cloning: https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html – Benkerroum Mohamed Apr 02 '21 at 13:01

2 Answers2

1

Based on which hybris version you use, you can also use ma.glasnost.orika.MapperFactory like:

MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
MapperFacade mapper = mapperFactory.getMapperFacade();
ProductData clonedData = mapper.map(productData, ProductData.class);
Sali Manaf
  • 166
  • 1
  • 9
1

Hi You can directly Inject dataMapper bean in your controller or in service.

import de.hybris.platform.webservicescommons.mapping.DataMapper;
@Autowired
private DataMapper dataMapper;

dataMapper.map(e, ProductData.class)

enter image description here

Raushan Kumar
  • 1,195
  • 12
  • 21