首页 > 解决方案 > Hibernate 在我的实体关系上生成太多外部连接

问题描述

我有几个实体共享一对多和多对一的关系,我将在下面显示,我遇到的问题是我做了一个简单的本机查询,它可以工作,但是用 h2 db 进行集成测试,我称之为查询我得到语法错误并查看休眠生成的查询多长时间。谁能指出我到底错在哪里?

@Getter
@Setter
@Entity
public class FinancingOffer extends BaseEntity {

    @ManyToOne
    @JoinColumn(name = "person_id")
    private Person person;

    @ManyToOne
    @JoinColumn(name = "financing_price_list_mod_ver_id")
    private FinancingPriceListModVer financingPriceListModVer;

    @ManyToOne
    @JoinColumn(name = "financing_offer_status_id")
    private FinancingOfferStatus financingOfferStatus;

    private String name;

// 为简洁起见省略了一些简单的 int 变量

}

@Entity
@Getter
@Setter
public class FinancingPriceListModVer extends BaseEntity {

    @OneToOne
    private ModVer modVer;

    @ManyToOne
    @JoinColumn(name = "financing_price_list_id")
    private FinancingPriceList financingPriceList;

    @ManyToOne
    @JoinColumn(name = "financing_price_list_dnpmt_id")
    private FinancingPriceListDnpmt financingPriceListDnpmt;

    @ManyToOne
    @JoinColumn(name = "financing_price_list_millage_id")
    private FinancingPriceListMillage financingPriceListMillage;

    @ManyToOne
    @JoinColumn(name = "financing_price_list_periods_id")
    private FinancingPriceListPeriods financingPriceListPeriods;

    private Integer installment;

}


@Entity
@Getter
@Setter
public class FinancingOfferStatus extends BaseEntity {

    private String name;
    private String description;

    @OneToMany(mappedBy = "financingOfferStatus")
    private List<FinancingOffer> financingOffers;

}

我的存储库是

@Repository
public interface FinancingOfferRepository extends JpaRepository<FinancingOffer, Long> {

    @Query(value = "select * from financing_offer fo left join financing_price_list_mod_ver fp on fo.financing_price_list_mod_ver_id  = fp.id \n" +
            "where fo.person_id  = ?1 and fp.mod_ver_id = ?2", nativeQuery = true)
    List<FinancingOffer> findByFinancingOfferIfExistByPersonIdAndModVer(Long personId, Long modVerId);

}

现在这个查询在运行应用程序时有效,但今天我正在编写 IT 测试

我试图执行一个在里面使用这个查询的测试,我得到了这个,错误消息无法准备语句

select financingp0_.id as id1_36_0_, financingp0_.rec_change_date as rec_chan2_36_0_, financingp0_.rec_create_date as rec_crea3_36_0_, financingp0_.financing_price_list_id as financin5_36_0_, financingp0_.financing_price_list_dnpmt_id as financin6_36_0_, financingp0_.financing_price_list_millage_id as financin7_36_0_, financingp0_.financing_price_list_periods_id as financin8_36_0_, financingp0_.installment as installm4_36_0_, financingp0_.mod_ver_id as mod_ver_9_36_0_, financingp1_.id as id1_33_1_, financingp1_.rec_change_date as rec_chan2_33_1_, financingp1_.rec_create_date as rec_crea3_33_1_, financingp1_.active as active4_33_1_, financingp1_.archived as archived5_33_1_, financingp1_.description as descript6_33_1_, financingp1_.downpayment_cnt as downpaym7_33_1_, financingp1_.financing_supplier_id as financi11_33_1_, financingp1_.millage_cnt as millage_8_33_1_, financingp1_.name as name9_33_1_, financingp1_.periods_cnt as periods10_33_1_, financings2_.id as id1_40_2_, financings2_.rec_change_date as rec_chan2_40_2_, financings2_.rec_create_date as rec_crea3_40_2_, financings2_.description as descript4_40_2_, financings2_.name as name5_40_2_, financingp3_.id as id1_34_3_, financingp3_.rec_change_date as rec_chan2_34_3_, financingp3_.rec_create_date as rec_crea3_34_3_, financingp3_.downpayment_amt as downpaym4_34_3_, financingp3_.downpayment_percent as downpaym5_34_3_, financingp3_.financing_price_list_id as financin6_34_3_, financingp4_.id as id1_33_4_, financingp4_.rec_change_date as rec_chan2_33_4_, financingp4_.rec_create_date as rec_crea3_33_4_, financingp4_.active as active4_33_4_, financingp4_.archived as archived5_33_4_, financingp4_.description as descript6_33_4_, financingp4_.downpayment_cnt as downpaym7_33_4_, financingp4_.financing_supplier_id as financi11_33_4_, financingp4_.millage_cnt as millage_8_33_4_, financingp4_.name as name9_33_4_, financingp4_.periods_cnt as periods10_33_4_, financingp5_.id as id1_35_5_, financingp5_.rec_change_date as rec_chan2_35_5_, financingp5_.rec_create_date as rec_crea3_35_5_, financingp5_.financing_price_list_id as financin5_35_5_, financingp5_.millage_limit as millage_4_35_5_, financingp6_.id as id1_33_6_, financingp6_.rec_change_date as rec_chan2_33_6_, financingp6_.rec_create_date as rec_crea3_33_6_, financingp6_.active as active4_33_6_, financingp6_.archived as archived5_33_6_, financingp6_.description as descript6_33_6_, financingp6_.downpayment_cnt as downpaym7_33_6_, financingp6_.financing_supplier_id as financi11_33_6_, financingp6_.millage_cnt as millage_8_33_6_, financingp6_.name as name9_33_6_, financingp6_.periods_cnt as periods10_33_6_, financingp7_.id as id1_37_7_, financingp7_.rec_change_date as rec_chan2_37_7_, financingp7_.rec_create_date as rec_crea3_37_7_, financingp7_.financing_price_list_id as financin5_37_7_, financingp7_.period as period4_37_7_, financingp8_.id as id1_33_8_, financingp8_.rec_change_date as rec_chan2_33_8_, financingp8_.rec_create_date as rec_crea3_33_8_, financingp8_.active as active4_33_8_, financingp8_.archived as archived5_33_8_, financingp8_.description as descript6_33_8_, financingp8_.downpayment_cnt as downpaym7_33_8_, financingp8_.financing_supplier_id as financi11_33_8_, financingp8_.millage_cnt as millage_8_33_8_, financingp8_.name as name9_33_8_, financingp8_.periods_cnt as periods10_33_8_, modver9_.id as id1_47_9_, modver9_.rec_change_date as rec_chan2_47_9_, modver9_.rec_create_date as rec_crea3_47_9_, modver9_.description1 as descript4_47_9_, modver9_.description2 as descript5_47_9_, modver9_.status_id as status_i6_47_9_, modver9_.vehicle_class_id as vehicle_7_47_9_, modverstat10_.id as id1_50_10_, modverstat10_.rec_change_date as rec_chan2_50_10_, modverstat10_.rec_create_date as rec_crea3_50_10_, modverstat10_.name as name4_50_10_, vehiclecla11_.id as id1_71_11_, vehiclecla11_.rec_change_date as rec_chan2_71_11_, vehiclecla11_.rec_create_date as rec_crea3_71_11_, vehiclecla11_.description as descript4_71_11_, vehiclecla11_.name as name5_71_11_, vehiclecla11_.order as order6_71_11_ from financing_price_list_mod_ver financingp0_ left outer join financing_price_list financingp1_ on financingp0_.financing_price_list_id=financingp1_.id left outer join financing_supplier financings2_ on financingp1_.financing_supplier_id=financings2_.id left outer join financing_price_list_dnpmt financingp3_ on financingp0_.financing_price_list_dnpmt_id=financingp3_.id left outer join financing_price_list financingp4_ on financingp3_.financing_price_list_id=financingp4_.id left outer join financing_price_list_millage financingp5_ on financingp0_.financing_price_list_millage_id=financingp5_.id left outer join financing_price_list financingp6_ on financingp5_.financing_price_list_id=financingp6_.id left outer join financing_price_list_periods financingp7_ on financingp0_.financing_price_list_periods_id=financingp7_.id left outer join financing_price_list financingp8_ on financingp7_.financing_price_list_id=financingp8_.id left outer join mod_ver modver9_ on financingp0_.mod_ver_id=modver9_.id left outer join mod_ver_status modverstat10_ on modver9_.status_id=modverstat10_.id left outer join vehicle_class vehiclecla11_ on modver9_.vehicle_class_id=vehiclecla11_.id where financingp0_.id=? [42001-200]

我认为它具有多对一映射,但有什么想法吗?

标签: hibernate

解决方案


@ManyToOne默认情况下,@OneToOne映射是渴望的,这体现在连接中。如果你不想要那个,并且相信我可能没有人真正想要那个,你必须使用fetch = FetchType.LAZY.

由于您没有共享错误详细信息,因此我无法说明该错误。


推荐阅读