JPA-我得到多行而不是1

时间:2019-06-03 20:23:57

标签: sql hibernate jpa spring-data-jpa

我花了很多时间解决我的问题,但没有成功。我想实现这样的功能(但用一行而不是两个): enter image description here

我的数据库:

CREATE TABLE odo.d_kryterium_naruszen (
    id                          bigserial primary key,
    kryterium                   text not null,
    data_wpr                    TIMESTAMP not null DEFAULT clock_timestamp(),
    opr                         bigint not null
);

CREATE TABLE odo.d_czynnik_naruszen (
    id                          bigserial primary key,
    czynnik                     text not null,
    id_kryterium_naruszen       bigint not null references odo.d_kryterium_naruszen(id),
    stopien                     NUMERIC(10,2) not null,
    data_wpr                    TIMESTAMP not null DEFAULT clock_timestamp(),
    opr                         bigint not null
);

CREATE TABLE odo.d_dotkliwosc_naruszenia (
    id                          bigserial primary key,
    zakres                      numrange not null,
    ocena                       text not null,
    opis                        text not null,
    wymagane_dzialanie          text not null,
    data_wpr                    TIMESTAMP not null DEFAULT clock_timestamp(),
    opr                         bigint not null
);

CREATE TABLE odo.ocena_naruszenia_wynik (
    id                              bigserial primary key,
    wartosc_dotkliwosci_naruszenia  NUMERIC(10,2) not null,
    status_id                       bigint not null references odo.d_status_oceny_naruszenia(id),
    ocena_naruszenia_id             bigint not null references odo.ocena_naruszenia(id),
    data_wpr                        TIMESTAMP not null DEFAULT clock_timestamp(),
    opr                             bigint not null
);

create table odo.czynnik_naruszen_wynik(
    id                              bigserial primary key,
    ocena_naruszenia_wynik_id       bigint not null references odo.ocena_naruszenia_wynik(id),
    czynnik_naruszen_id             bigint not null references odo.d_czynnik_naruszen(id),
    komentarz                       text,
    czynnik_wybrany                 boolean not null default false
    wartosc_wybrana                 NUMERIC(10,2) not null,
    data_wpr                        TIMESTAMP not null DEFAULT clock_timestamp(),
    opr                             bigint not null
);

这里是我的实体:

@Data
@Entity
@Table(schema = "odo", name = "d_kryterium_naruszen")
public class ViolationCriterion extends BaseEntity {

    @Column(name = "kryterium")
    private String criterion;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "id_kryterium_naruszen")
    private List<ViolationFactor> violationFactors;

}

@Data
@Entity
@Table(schema = "odo", name = "d_czynnik_naruszen")
public class ViolationFactor extends BaseEntity {

    @Column(name = "czynnik")
    private String factor;

    @Column(name = "stopien")
    private float degree;

    @OneToMany
    @JoinColumn(name = "czynnik_naruszen_id")
    private List<IncidentAssessmentFactor> incidentAssessmentFactor;
}

@Data
@Entity
@Table(schema = "odo", name = "czynnik_naruszen_wynik")
public class IncidentAssessmentFactor extends BaseEntity {

    @Column(name="komentarz")
    private String comment;

    @Column(name="czynnik_wybrany")
    private Boolean factorIsSelected;

    @Column(name = "wartosc_wybrana")
    private Float value;

    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="ocena_naruszenia_wynik_id", updatable=false, insertable=false)
    private IncidentAssessment incidentAssessment;
}

@Data
@Entity
@Table(schema = "odo", name = "ocena_naruszenia_wynik")
public class IncidentAssessment extends BaseEntity {

    @Column(name="ocena_naruszenia_id")
    private Long incidentAssessmentId;

    @Column(name = "wartosc_dotkliwosci_naruszenia")
    private Float severityDegreeValue;

我的存储库:

@Repository
public interface ViolationCriterionRepository extends JpaRepository<ViolationCriterion, Long> {

    //  @Query("select vc from ViolationCriterion vc inner join vc.violationFactors vf inner join vf.incidentAssessmentFactor iaf inner join iaf.incidentAssessment ia where ia.incidentAssessmentId = ?1 group by vc ")
    @Query("select vc from ViolationCriterion vc inner join vc.violationFactors vf inner join vf.incidentAssessmentFactor iaf inner join iaf.incidentAssessment ia where ia.incidentAssessmentId = ?1 group by vc ")

    //  @Query(value = "select kn.kryterium from odo.d_kryterium_naruszen kn join odo.d_czynnik_naruszen cn on kn.id = cn.id_kryterium_naruszen join odo.czynnik_naruszen_wynik cnw on cnw.czynnik_naruszen_id = cn.id join odo.ocena_naruszenia_wynik onw on cnw.ocena_naruszenia_wynik_id = onw.id where onw.ocena_naruszenia_id = ?1 group by kn.id, cn.id, cnw.id, onw.id", nativeQuery = true)
//  @Query(value = "select kn.id, kn.kryterium, kn.data_wpr, kn.opr, cn.id, cn.czynnik, cn.stopien, cn.opr, cn.data_wpr, cnw.id, cnw.data_wpr, cnw.opr, cnw.komentarz, cnw.czynnik_wybrany, cnw.wartosc_wybrana, onw.id, onw.data_wpr, onw.opr, onw.ocena_naruszenia_id, onw.wartosc_dotkliwosci_naruszenia from odo.d_kryterium_naruszen kn join odo.d_czynnik_naruszen cn on kn.id = cn.id_kryterium_naruszen join odo.czynnik_naruszen_wynik cnw on cnw.czynnik_naruszen_id = cn.id join odo.ocena_naruszenia_wynik onw on cnw.ocena_naruszenia_wynik_id = onw.id where onw.ocena_naruszenia_id = ?1 group by kn.id, cn.id, cnw.id, onw.id", nativeQuery = true)
    List<ViolationCriterion> findIncidentAssessmentByIncidentAssessmentId(Long incidentId);
//  List<ViolationCriterion> findByViolationFactorsIncidentAssessmentFactorIncidentAssessmentIncidentAssessmentIdGroupByViolationCriterionCriterion(Long id);
}

在这里,我叫我的仓库:

List<ViolationCriterion> violationCriteria = violationCriterionRepository.findIncidentAssessmentByIncidentAssessmentId(id);//vi

在表czynnik_naruszen_wynik中,我有2行,因为在ocena_naruszenia_wynik表中有2行。问题是我有多个实体IncidentAssessmentFactor值,而不是1

0 个答案:

没有答案