Replies: 2 comments
-
|
This is indeed a good suggestions @drahkrub. This should be a continuation of #3380 I would say. Since using
|
Beta Was this translation helpful? Give feedback.
-
I wouldn't call it a "disguise", I think it's a viable use case - if not the main use case - for an
The problem is e.g. described in the mentioned SO link Updating existing bean instances for child collection, but I would like to give a complete and simplified example: (with getters/setters left out) public class IdOnly {
private Integer id;
}public class IdOnlyDto {
private Integer id;
}public class Bean extends IdOnly {
private List<Bean> list;
}public class Dto extends IdOnlyDto {
private List<Dto> list;
}@Mapper
public abstract class MyMapper {
public abstract Bean mapDtoOnBean(Dto dto, @MappingTarget Bean bean);
@ObjectFactory
public <T extends IdOnly> T resolve(final IdOnlyDto dto, final @TargetType Class<T> clazz) {
return dto != null
? resolveById(dto.getId(), clazz)
: null;
}
public <T extends IdOnly> T resolveById(final Integer id, final @TargetType Class<T> clazz) {
try {
// the way with Spring/Hibernate:
// return id != null
// ? entityManager.getReference(clazz, id)
// : BeanUtils.instantiateClass(clazz);
// "faked" without Spring/Hibernate:
T newInstance = clazz.getDeclaredConstructor().newInstance();
newInstance.setId(id);
return newInstance;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}This creates the following implementation: public class MyMapperImpl extends MyMapper {
@Override
public Bean mapDtoOnBean(Dto dto, Bean bean) {
if ( dto == null ) {
return bean;
}
bean.setId( dto.getId() ); // redundant, no matter
if ( bean.getList() != null ) {
List<Bean> list = dtoListToBeanList( dto.getList() );
if ( list != null ) {
bean.getList().clear();
bean.getList().addAll( list );
}
else {
bean.setList( null );
}
}
else {
List<Bean> list = dtoListToBeanList( dto.getList() );
if ( list != null ) {
bean.setList( list );
}
}
return bean;
}
protected List<Bean> dtoListToBeanList(List<Dto> list) {
if ( list == null ) {
return null;
}
List<Bean> list1 = new ArrayList<Bean>( list.size() );
for ( Dto dto : list ) {
list1.add( dtoToBean( dto ) );
}
return list1;
}
protected Bean dtoToBean(Dto dto) {
if ( dto == null ) {
return null;
}
Bean bean = resolve( dto, Bean.class );
bean.setId( dto.getId() ); // redundant, no matter
bean.setList( dtoListToBeanList( dto.getList() ) ); // oh, no!
return bean;
}
}The update method Of course this problem can be solved by providing public Bean dtoToBean(Dto dto) {
return mapDtoOnBean(dto, resolve( dto, Bean.class ));
}in But why even make the distinction between update and normal mapping methods (at least here)? Without an Or to put it another way: Why should public Bean dtoToBean(Dto dto) {
return mapDtoOnBean(dto, new Bean());
}work different than some generated method |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When working with Hibernate and/or JPA its "difficult" to map
@OneToManyrelations withorphanRemoval=true(How does orphanRemoval work with JPA and Hibernate), see e.g. issue #1830.If a collection or a map of some bean which is connected to a Hibernate session (or
EntityManager) isthen an exception is thrown when the session gets closed:
A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance.I think that should be easy to avoid by introducing a new
NullValuePropertyMappingStrategy.CLEARfor Maps and Collections (Iterables, see discussion #3380) such that instead ofthe following code is created:
Or optimized:
Would that be easy to realise or is there something against it?
One more addition: The code above is generated for update mapping methods (i.e. methods using
@MappingTarget), see e.g. SO: Updating existing bean instances for child collection](https://stackoverflow.com/a/77029004/194900).Methods without
@MappingTargetcreate the following code:which again produces the exception
A collection with cascade=”all-delete-orphan” was no longer referenced by the owning entity instance.It would be nice if this could be changed to the (optimized) version above:
Beta Was this translation helpful? Give feedback.
All reactions