Home > database >  Updating a "complex" Entity in JPA
Updating a "complex" Entity in JPA

Time:07-05

entity = MainDao.findByUUID(getEntityManager(), dto.getUuid());

            
Adress i = new AdressDao().findById(dto.getIdAdress());
i.setPhone(dto.getPhone());
entity.setAdress(i);
return MainDao.update(getEntityManager(), entity);

I have a main Entity in which there is a @ManytoOne relationship to Adress. I want to update the field "phone" inside adress, how do I do it? My code fails to do so.

Hope you can help me out, it seems there is no "patch" method inside JPA. I would love to know the best practices.

CodePudding user response:

By default @ManyToOne doesn't cascade the changes (as it refers to a parent which may be have other child associations).

You can do either of below,

  1. save the changes of Address entity via your AddressDao like addressDao.save(addressEntity)
  2. use @ManyToOne(cascade = CascadeType.ALL).

1st options is preferable.

Read about CascadeType to utilize wisefully.

  • Related