使用JPA CascadeType更新根实体时,更新嵌套实体(ManyToOne)

时间:2018-09-19 12:35:06

标签: jpa spring-data-jpa spring-data crud

我创建了一些表来存储有关 personel personel_egitim_durum 的信息,这些信息是非常嵌套的,并且实现了BaseEntity 。因此,数据库中的表也是嵌套的。 顶层实体称为“人员”。

当我将 personel 的字段更新为 aktif (来自BaseEntity 的)时,我希望 personel_egitim_durum aktif 来自BaseEntity )的字段会自动更新。

如下,

    @MappedSuperclass
    public abstract  class BaseEntity {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Column(name="eklenme_tarihi")
        @CreationTimestamp
        private Date eklenmeTarihi;

        @Column(name="guncellenme_tarihi")
        @UpdateTimestamp
        private Date guncellenmeTarihi;

        @Column(name="aktif")
        private int aktif;

        @Column(name="ekleyen_personel_id")
        private Long ekleyenPersonelId;

        @Column(name="guncelleyen_personel_id")
        private Long guncelleyenPersonelId;

//getter and setter
}

@Entity
@Table(name="personel")
public class Personel extends BaseEntity {

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="kisi_id")
    private Kisi kisi;

    @Column(name="personel_tipi",length = 2,nullable = false)
    private int personelTipi;

    @Column(name="sicil_no",length = 100,nullable = false)
    private String sicilNo;

    @Column(name="khk_onay",length = 1)
    private int khkOnay;
    //getter and setter
    }

    @Entity
@Table(name = "personel_egitim_durum")
public class PersonelEgitimDurum extends BaseEntity {

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="personel_id",nullable = false)
    private Personel personel;

    @Column(name = "ogrenim_durumu")
    private String ogrenimDurumu;

    @Column(name = "okul_id", length = 3)
    private Long okulId;

    @Column(name = "universite_bolum_id", length = 4)
    private Long universiteBolumId;

    @Column(name = "mezuniyet_tarihi")
    private Date mezuniyetTarihi;

    @Column(name = "aciklama", length = 500)
    private String aciklama;

 //getter and setter
        }

@RestController
@RequestMapping(value = "personel")
public class PersonelController {


    private final PersonelEgitimDurumService personelEgitimDurumService;

    @Autowired
    public PersonelController(PersonelEgitimDurumService personelEgitimDurumService) {
        this.personelEgitimDurumService = personelEgitimDurumService;
    }
        @RequestMapping(value = "/demo" , method = RequestMethod.POST)
        public PersonelDTO func(@RequestBody PersonelDTO personelDTO) {

            if(per.getAktif()==1) {
                personelDTO.setAktif(0);
            }
            else
                personelDTO.setAktif(1);
            personelDTO = personelService.guncelle(personelDTO);//**UPDATE**

            List<PersonelEgitimDurumDTO> percalList = personelEgitimDurumService.findPersonelEgitimDurumByPersonelId(personelDTO.getId());
            return personelDTO;
        }
}

 @Service
@Transactional
public class PersonelEgitimDurumServiceImpl implements PersonelEgitimDurumService {

    private final PersonelEgitimDurumDAO personelEgitimDurumDAO;

    private PersonelEgitimDurumMapper mapper = Mappers.getMapper(PersonelEgitimDurumMapper.class);

@Override
    public List<PersonelEgitimDurumDTO> findPersonelEgitimDurumByPersonelId(Long personelId) {
        List<PersonelEgitimDurum> personelEgitimDurumList=personelEgitimDurumDAO.findPersonelEgitimDurumByPersonelId(personelId);
        List<PersonelEgitimDurumDTO> personelEgitimDurumDtoList=personelEgitimDurumList.stream()
                .filter(p->p.getAktif()==1)
                .map(personelEgitimDurum->mapper.toDto(personelEgitimDurum))
                .collect(Collectors.toList());
        return personelEgitimDurumDtoList;
    }
}

@Service
@Transactional
public interface PersonelEgitimDurumService extends BaseService<PersonelEgitimDurumDTO>{
    List<PersonelEgitimDurumDTO> findPersonelEgitimDurumByPersonelId(Long personelId);
}

    @Service
@Transactional
public interface PersonelService extends BaseService<PersonelDTO>,CustomQueryService<PersonelDTO> {
    PersonelDTO findPersonelByKimlikNo(String kimlikNo);
    List<PersonelDTO> findPersonelByPersonelFilter(PersonelFilter personelFilter);

}

    @Service
@Transactional
public class PersonelServiceImpl implements PersonelService {
    /*@Autowired
    private PersonelDAO personelDAO;*/

    private final PersonelDAO personelDao;

@Override
    public PersonelDTO guncelle(PersonelDTO personelDTO) {
        Personel personel=mapper.toEntity(personelDTO);
        personel=personelDao.save(personel);
        personelDTO=mapper.toDto(personel);
        return personelDTO;
    }
}

    public interface BaseService<T extends BaseDTO>{
    T kaydet(T t);
    T guncelle(T t);
    List<T> findAll();
    List<T> findAllByAktif(int aktif);
    T deleteByAktif(T t);
}

    @Repository
public interface PersonelEgitimDurumDAO extends CrudRepository<PersonelEgitimDurum,Long>,BaseDAO<PersonelEgitimDurum> {

    List<PersonelEgitimDurum> findPersonelEgitimDurumByPersonelId(Long personelId);
}

    @Repository
public interface PersonelDAO extends CrudRepository<Personel,Long>,BaseDAO<Personel>,PersonelDAOCustom,CustomQueryDAO<Personel>,QuerydslPredicateExecutor<Personel> {
    Personel findPersonelByKisi(Kisi kisi);
    Personel findPersonelBySicilNo(String sicilNo);
}

1 个答案:

答案 0 :(得分:0)

    @Entity
@Table(name="personel")
public class Personel extends BaseEntity {

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="kisi_id")
    private Kisi kisi;

    @Column(name="personel_tipi",length = 2,nullable = false)
    private int personelTipi;

    @Column(name="sicil_no",length = 100,nullable = false)
    private String sicilNo;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "personel", cascade =CascadeType.ALL,orphanRemoval = true)
  private List<PersonelEgitimDurum> personelEgitimDurumList;
}



 @Entity
@Table(name = "personel_egitim_durum")
public class PersonelEgitimDurum extends BaseEntity {

    @ManyToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="personel_id",nullable = false, updatable = true)
    private Personel personel;
}