I wrote a response to the users list but not sure it came through. It kinda
belongs in this thread so here it goes.
So I ran into issues with the DTO mapper api and voiced my concerns in irc.
I saw this discussion and now I am trying the solution present in the
current SNAPSHOT. However I have one comment / question:
What if my Entity has a relationship to another Entity?
Like this:
public class User extends BaseAuditEntity {
@Column
private String name;
@Column
@ManyToOne
@JoinColumn(name="group_id")
private Group group;
This means my userDTO may come with a GroupDTO and how do I map that
relationship? Or to explain by code please fill in the question marks below
;) or if you feel my implementation is weird then I would gladly accept
comments on that too. But the way I see it I need to @Inject the
GroupMapper, use the EntityManager to find the Group then call
groupMapper.toEntity and then I think to myself that the API is worse now
because before I could call groupMapper.toEntity with only a dto argument.
At that point you had to use the entitymanager anyways to find "yourself"
and that feels clean compared to find your entities you form relationships
with.
@Override
protected User toEntity(final User user, final UserDTO userDTO) {
MapperUtil.toAuditEntity(user, userDTO);
user.setName(userDTO.getName());
user.setEmail(userDTO.getEmail());
user.setLocale(userDTO.getLocale());
user.setGroup(*?????*);
return user;
}
Cheers / Karl
On 17 May 2014 16:40, Romain Manni-Bucau <[email protected]> wrote:
> Yep, missed it.
>
> Works for me. Maybe the name should be closer to other methods,
> mapKey? But whatever you choose as name this solution works :).
>
>
> Romain Manni-Bucau
> Twitter: @rmannibucau
> Blog: http://rmannibucau.wordpress.com/
> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> Github: https://github.com/rmannibucau
>
>
> 2014-05-17 15:54 GMT+02:00 Thomas Hug <[email protected]>:
> > It's the PK, not the Entity ;) In SimpleQueryInOutMapperBase, it could be
> > something like
> >
> > @Inject
> > private QueryInvocationContext context;
> >
> > protected abstract Object getPrimaryKey(Dto dto);
> >
> > protected E findEntity(Object pk)
> > {
> > return (E) context.getEntityManager().find(context.getEntityClass(),
> > pk);
> > }
> >
> > @Override
> > public Object mapParameter(final Object parameter)
> > {
> > Object pk = getPrimaryKey((Dto) parameter);
> > if (pk != null)
> > {
> > E entity = findEntity(pk);
> > return toEntity(entity, (Dto) parameter);
> > }
> > return toEntity(newEntity(), (Dto) parameter);
> > }
> >
> >
> > On Sat, May 17, 2014 at 1:57 PM, Romain Manni-Bucau
> > <[email protected]>wrote:
> >
> >> would work while return is <E> and not Object ;)
> >>
> >>
> >> Romain Manni-Bucau
> >> Twitter: @rmannibucau
> >> Blog: http://rmannibucau.wordpress.com/
> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> Github: https://github.com/rmannibucau
> >>
> >>
> >> 2014-05-17 11:47 GMT+02:00 Thomas Hug <[email protected]>:
> >> > Or a protected abstract Object getPrimaryKey(Dto dto). We can get the
> EM
> >> > over an injected QueryInvocationContext.
> >> >
> >> >
> >> > On Thu, May 15, 2014 at 9:06 PM, Romain Manni-Bucau
> >> > <[email protected]>wrote:
> >> >
> >> >> I think a protected findEntity(id) in the mapper can be enough.
> >> >>
> >> >>
> >> >> Romain Manni-Bucau
> >> >> Twitter: @rmannibucau
> >> >> Blog: http://rmannibucau.wordpress.com/
> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> >> Github: https://github.com/rmannibucau
> >> >>
> >> >>
> >> >> 2014-05-07 22:29 GMT+02:00 Thomas Hug <[email protected]>:
> >> >> > Hi Romain,
> >> >> > See your point. But if we only get the DTO - with what would we
> call
> >> the
> >> >> > find? Could even be that the PK is a DTO or encoded / encrypted and
> >> needs
> >> >> > to go through the mapper first. Maybe we can provide some
> convenience
> >> >> > methods in the base mapper?
> >> >> >
> >> >> >
> >> >> > On Tue, May 6, 2014 at 7:41 PM, Romain Manni-Bucau <
> >> >> [email protected]>wrote:
> >> >> >
> >> >> >> Hi guys,
> >> >> >>
> >> >> >> DTO feature is awesome but doesn't work in update mode since isNew
> >> >> >> doesn't use a managed entity.
> >> >> >>
> >> >> >> When using a mapper we should call find and pass it to the mapper
> (or
> >> >> >> create a new unmanaged entity if not found). So mapper signature
> >> >> >> should be Entity toEntity(Entity, DTO) no?
> >> >> >>
> >> >> >> Otherwise users need to do the find in the mapper...almost
> eveytime.
> >> >> >>
> >> >> >> wdyt?
> >> >> >>
> >> >> >>
> >> >> >> Romain Manni-Bucau
> >> >> >> Twitter: @rmannibucau
> >> >> >> Blog: http://rmannibucau.wordpress.com/
> >> >> >> LinkedIn: http://fr.linkedin.com/in/rmannibucau
> >> >> >> Github: https://github.com/rmannibucau
> >> >> >>
> >> >>
> >>
>