javajpaspring查找表的多重查询

tzxcd3kk  于 2021-07-06  发布在  Java
关注(0)|答案(2)|浏览(340)

我有一个带有模式的查找表

在我的控制器中,我根据“查找类型”调用此表

FormProdIndex f = new FormProdIndex();
    model.addAttribute(AuthenticationBase.MODEL_FORMPRODINDEX, f);

    // Lookup Models
    System.out.println("TEST 1");
    model.addAttribute(AuthenticationBase.LOOKUP_FACTORY, this.appLookupDAO.getFactoryList());

此查找模型与我的主模型“formprodindex”关联

@Entity
@Table(name = "Form_Prod_Index")
public class FormProdIndex {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "index_id", nullable = false)
    private Long indexId;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "factory_Id", referencedColumnName = "lookup_Id")
    private AppLookup lookupFactory;

    // getter and setters
}

以及我的查找模型

@Entity
@Table(name = "App_Lookup")
public class AppLookup {

    @Id
    @GeneratedValue
    @Column(name = "Lookup_Id", nullable = false)
    private Long lookupId;

    @Column(name = "Lookup_type", length = 30, nullable = false)
    private String lookupType;

    @Column(name = "Lookup_Name", length = 30, nullable = false)
    private String lookupName;

    @OneToOne(mappedBy = "lookupFactory")
    @Fetch(FetchMode.JOIN)
    private FormProdIndex FormProdIndexFactory;

    // getter and setter
}

正如您在下面的java日志中所看到的,我的主模型“formprodindex”的select查询被多次调用。

TEST 1
Hibernate: select applookup0_.lookup_id as lookup_i1_0_, applookup0_.lookup_desc as lookup_d2_0_, applookup0_.lookup_name as lookup_n3_0_, applookup0_.lookup_type as lookup_t4_0_ from app_lookup applookup0_ where applookup0_.lookup_type='factory' order by applookup0_.lookup_id
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?
Hibernate: select formprodin0_.index_id as index_id1_3_1_, formprodin0_.approved_by as approved2_3_1_, formprodin0_.approved_date as approved3_3_1_, formprodin0_.break_time_mins as break_ti4_3_1_, formprodin0_.created_by as created_5_3_1_, formprodin0_.created_date as created_6_3_1_, formprodin0_.end_date as end_date7_3_1_, formprodin0_.end_time as end_time8_3_1_, formprodin0_.interval_mins as interval9_3_1_, formprodin0_.is_deleted as is_dele10_3_1_, formprodin0_.factory_id as factory21_3_1_, formprodin0_.pack_size as pack_si11_3_1_, formprodin0_.pack_supplier as pack_su12_3_1_, formprodin0_.reject_id as reject_13_3_1_, formprodin0_.rejected_by as rejecte14_3_1_, formprodin0_.rejected_date as rejecte15_3_1_, formprodin0_.remarks as remarks16_3_1_, formprodin0_.session_id as session17_3_1_, formprodin0_.start_date as start_d18_3_1_, formprodin0_.start_time as start_t19_3_1_, formprodin0_.status as status20_3_1_, applookup1_.lookup_id as lookup_i1_0_0_, applookup1_.lookup_desc as lookup_d2_0_0_, applookup1_.lookup_name as lookup_n3_0_0_, applookup1_.lookup_type as lookup_t4_0_0_ from form_prod_index formprodin0_ left outer join app_lookup applookup1_ on formprodin0_.factory_id=applookup1_.lookup_id where formprodin0_.factory_id=?

根据我的理解,我的主模型的select查询不应该运行,因为这是一个新对象。这只是一种查找类型,如果我添加“line”和“pack\u mat”,查询会变长,页面加载也会变长。你知道怎么解决吗?

ifsvaxew

ifsvaxew1#

你需要使用 EntityGraph 以减少查询的数量。

@Entity
@Table(name = "App_Lookup")
@NamedEntityGraph(name = "graph.AppLookup.FormProdIndex", attributeNodes = { @NamedAttributeNode(value = "FormProdIndexFactory") })
public class AppLookup {

    @Id
    @GeneratedValue
    @Column(name = "Lookup_Id", nullable = false)
    private Long lookupId;

    @Column(name = "Lookup_type", length = 30, nullable = false)
    private String lookupType;

    @Column(name = "Lookup_Name", length = 30, nullable = false)
    private String lookupName;

    @OneToOne(mappedBy = "lookupFactory")
    @Fetch(FetchMode.JOIN)
    private FormProdIndex FormProdIndexFactory;

    // getter and setter
}

在您的存储库中使用了此图表

@Repository
public interface AppLookupRepository extends CrudRepository<AppLookup, Long> {

    @EntityGraph(value = "graph.AppLookup.FormProdIndex", type = EntityGraphType.FETCH)
    public Optional<AppLookup> findByLookupTypeIsAndLookupIdIs(String lookupType, Long lookupId);

}

你可以在这里多吃点
https://www.baeldung.com/spring-data-jpa-named-entity-graphs
为什么我们需要使用实体图?

vlf7wbxs

vlf7wbxs2#

@OneToOne 默认情况下为 FetchType.EAGER 也就是说当你在查询 AppLookup ,hibernate也获取 FormProdIndex .
我也不确定你的亲戚是不是 @OneToOne 因为hibernate正在执行多个select语句来获取 FormProdIndex .
尽管如此,您可以添加 FetchType.LAZY 不取 FormProdIndex 提取时 AppLookup

相关问题