Обновлять вложенный объект (ManyToOne), когда обновлять корневой объект с помощью JPA CascadeType - PullRequest
0 голосов
/ 19 сентября 2018

Я создал таблицы для хранения информации о personel и personel_egitim_durum , которые очень вложены и реализуют BaseEntity .Поэтому таблицы в моей базе данных тоже являются вложенными. Объект верхнего уровня называется Personel.

Когда я обновляю поле personel как Актив ( из BaseEntity ), Я хочу, чтобы поле personel_egitim_durum как Актив ( из 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 голосов
/ 21 сентября 2018
    @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;
}
...