查询包括OneToMany的特定字段失败

时间:2019-01-02 12:53:26

标签: java hibernate jpa spring-data jpa-2.0

@Entity
@Data
public class Soknadwrapper {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
    private Soknad soknad;

    @OneToMany(mappedBy = "soknadwrapper", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
    @OrderBy("kundereiseNr desc")
    private List<Paaminnelse> paaminnelser = new ArrayList<>();

    private LocalDateTime opprettetTidspunkt = LocalDateTime.now();
}

@Entity
@Data
public class Paaminnelse {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
    @JoinColumn(name = "paminnelser")
    private Soknadwrapper soknadwrapper;

    private String emneNokkel;
    private String innholdNokkel;
    private String beskrivelse;
}

给出以下查询:

"select distinct s.soknad, s.paaminnelser, s.opprettetTidspunkt from Soknadwrapper s " +
        "ORDER BY s.opprettetTidspunkt DESC"

它生成:

SELECT DISTINCT SOKNADWRAP0_.SOKNAD_ID AS COL_0_0_, .[*] AS COL_1_0_, SOKNADWRAP0_.OPPRETTETTIDSPUNKT AS COL_2_0_, SOKNAD1_.ID AS ID1_9_0_, PAAMINNELS2_.ID AS ID1_7_1_, SOKNAD1_.ANTALLMINDREAARIGEBARN AS ANTALLMI2_9_0_, SOKNAD1_.IPADRESSE AS IPADRESS3_9_0_, SOKNAD1_.KAMPANJEKODE AS KAMPANJE4_9_0_, SOKNAD1_.KAUSJONIST_ID AS KAUSJON23_9_0_, SOKNAD1_.KJOPTBOLIG_ID AS KJOPTBO24_9_0_, SOKNAD1_.LAANEDETALJER_AVDRAGSFRIEAAR AS LAANEDET5_9_0_, SOKNAD1_.LAANEDETALJER_FAKTURADATO AS LAANEDET6_9_0_, SOKNAD1_.LAANEDETALJER_FASTRENTEAAR AS LAANEDET7_9_0_, SOKNAD1_.LAANEDETALJER_LAANEBEHOV AS LAANEDET8_9_0_, SOKNAD1_.LAANEDETALJER_MELLOMFINANSIERING AS LAANEDET9_9_0_, SOKNAD1_.LAANEDETALJER_NEDBETALINGSTID AS LAANEDE10_9_0_, SOKNAD1_.LAANEDETALJER_PRODUKTKODE AS LAANEDE11_9_0_, SOKNAD1_.LAANEDETALJER_TILLEGGSLAANEBELOP AS LAANEDE12_9_0_, SOKNAD1_.LAANEDETALJER_TYPELAAN AS LAANEDE13_9_0_, SOKNAD1_.MARKEDSKODE AS MARKEDS14_9_0_, SOKNAD1_.MARKEDSSPORSMAAL AS MARKEDS15_9_0_, SOKNAD1_.MEDSOKER_ID AS MEDSOKE25_9_0_, SOKNAD1_.OKONOMI_EGENKAPITAL AS OKONOMI16_9_0_, SOKNAD1_.OKONOMI_HOVEDSOKER_ARBEIDSSTATUS AS OKONOMI17_9_0_, SOKNAD1_.OKONOMI_HOVEDSOKER_ESTIMERTBRUTTOAARSINNTEKT AS OKONOMI18_9_0_, SOKNAD1_.OKONOMI_MEDSOKER_ARBEIDSSTATUS AS OKONOMI19_9_0_, SOKNAD1_.OKONOMI_MEDSOKER_ESTIMERTBRUTTOAARSINNTEKT AS OKONOMI20_9_0_, SOKNAD1_.ONSKERBYGNINGSFORSIKRING AS ONSKERB21_9_0_, SOKNAD1_.SOKER_ID AS SOKER_I26_9_0_, SOKNAD1_.SOKNADSFORMAAL AS SOKNADS22_9_0_, PAAMINNELS2_.ANTALLDAGERETTERSISTOPPDATERT AS ANTALLDA2_7_1_, PAAMINNELS2_.ANTALLUTSENDELSERSENDTEPOST AS ANTALLUT3_7_1_, PAAMINNELS2_.ANTALLUTSENDELSERSENDTSMS AS ANTALLUT4_7_1_, PAAMINNELS2_.BESKRIVELSE AS BESKRIVE5_7_1_, PAAMINNELS2_.EMNENOKKEL AS EMNENOKK6_7_1_, PAAMINNELS2_.INNHOLDNOKKEL AS INNHOLDN7_7_1_, PAAMINNELS2_.KUNDEREISENR AS KUNDEREI8_7_1_, PAAMINNELS2_.SOKER AS SOKER9_7_1_, PAAMINNELS2_.PAMINNELSER AS PAMINNE11_7_1_, PAAMINNELS2_.UTSENDELSE AS UTSENDE10_7_1_ FROM SOKNADWRAPPER SOKNADWRAP0_ INNER JOIN SOKNAD SOKNAD1_ ON SOKNADWRAP0_.SOKNAD_ID=SOKNAD1_.ID INNER JOIN PAAMINNELSE PAAMINNELS2_ ON SOKNADWRAP0_.ID=PAAMINNELS2_.PAMINNELSER ORDER BY SOKNADWRAP0_.OPPRETTETTIDSPUNKT DESC "; expected "*, NOT, EXISTS, INTERSECTS, SELECT, FROM, WITH"; SQL statement:

select distinct soknadwrap0_.soknad_id as col_0_0_, . as col_1_0_, soknadwrap0_.opprettetTidspunkt as col_2_0_, soknad1_.id as id1_9_0_, paaminnels2_.id as id1_7_1_, soknad1_.antallMindreaarigeBarn as antallMi2_9_0_, soknad1_.ipAdresse as ipAdress3_9_0_, soknad1_.kampanjekode as kampanje4_9_0_, soknad1_.kausjonist_id as kausjon23_9_0_, soknad1_.kjoptBolig_id as kjoptBo24_9_0_, soknad1_.laanedetaljer_avdragsfrieAar as laanedet5_9_0_, soknad1_.laanedetaljer_fakturadato as laanedet6_9_0_, soknad1_.laanedetaljer_fastrenteAar as laanedet7_9_0_, soknad1_.laanedetaljer_laanebehov as laanedet8_9_0_, soknad1_.laanedetaljer_mellomfinansiering as laanedet9_9_0_, soknad1_.laanedetaljer_nedbetalingstid as laanede10_9_0_, soknad1_.laanedetaljer_produktkode as laanede11_9_0_, soknad1_.laanedetaljer_tilleggslaanebelop as laanede12_9_0_, soknad1_.laanedetaljer_typeLaan as laanede13_9_0_, soknad1_.markedskode as markeds14_9_0_, soknad1_.markedssporsmaal as markeds15_9_0_, soknad1_.medsoker_id as medsoke25_9_0_, soknad1_.okonomi_egenkapital as okonomi16_9_0_, soknad1_.okonomi_hovedsoker_arbeidsstatus as okonomi17_9_0_, soknad1_.okonomi_hovedsoker_estimertBruttoAarsinntekt as okonomi18_9_0_, soknad1_.okonomi_medsoker_arbeidsstatus as okonomi19_9_0_, soknad1_.okonomi_medsoker_estimertBruttoAarsinntekt as okonomi20_9_0_, soknad1_.onskerBygningsforsikring as onskerB21_9_0_, soknad1_.soker_id as soker_i26_9_0_, soknad1_.soknadsformaal as soknads22_9_0_, paaminnels2_.antallDagerEtterSistOppdatert as antallDa2_7_1_, paaminnels2_.antallUtsendelserSendtEpost as antallUt3_7_1_, paaminnels2_.antallUtsendelserSendtSms as antallUt4_7_1_, paaminnels2_.beskrivelse as beskrive5_7_1_, paaminnels2_.emneNokkel as emneNokk6_7_1_, paaminnels2_.innholdNokkel as innholdN7_7_1_, paaminnels2_.kundereiseNr as kunderei8_7_1_, paaminnels2_.soker as soker9_7_1_, paaminnels2_.paminnelser as paminne11_7_1_, paaminnels2_.utsendelse as utsende10_7_1_ from Soknadwrapper soknadwrap0_ inner join Soknad soknad1_ on soknadwrap0_.soknad_id=soknad1_.id inner join Paaminnelse paaminnels2_ on soknadwrap0_.id=paaminnels2_.paminnelser order by soknadwrap0_.opprettetTidspunkt DESC [42001-197]

我也尝试过使用投影,但是没有用。

"select distinct new com.foo.bar.repository.projections.PaaminnelseProj(s.soknad, s.paaminnelser, s.opprettetTidspunkt) from Soknadwrapper s " +
"ORDER BY s.opprettetTidspunkt DESC"

@AllArgsConstructor
@Data
public class PaaminnelseProj {
    private final Soknad soknad;
    private final SortedSet<Paaminnelse> paaminnelser;
    private final LocalDateTime opprettetTidspunkt;
}

但这也不起作用。我得到了几乎相同的错误。 我究竟做错了什么?基本上,我只希望返回这三个字段,而不是更多。

Soknadwrapper具有更多的字段,我为简洁起见而忽略了

0 个答案:

没有答案