SAP / olingo-jpa-processor-v4

The JPA Processor fills the gap between Olingo V4 and the database, by providing a mapping between JPA metadata and OData metadata, generating queries and supporting the entity manipulations.
Apache License 2.0
127 stars 79 forks source link

@OneToMany with @Fetch is not working properly #79

Closed samakovuluk closed 11 months ago

samakovuluk commented 5 years ago

I have two entity A and B, which B is attributes of A(one-to-many). And in entity A I put pagination with size 50. Problem is that the hibernate is fetching all data, actually, it must fetch those collections which is related to entity A. Here the code and hibernate request.

@OneToMany( fetch = FetchType.LAZY)
@JoinColumn(name="test_lot_id",)
@Fetch(value = FetchMode.SUBSELECT)
private List<TestAttribute> testLotAttributes=new ArrayList<>();
Hibernate: 
   select
         testlot0_. test_lot_id as col_0_0_,
         .......................... 
    from
         test. test_lots  testlot0_ limit ?
Hibernate: 
    select
        distinct  testlot0_. test_lot_id as col_0_0_,
        attributev1_.lot_attribute_name as col_1_0_,
        attributev1_.lot_attribute_value as col_2_0_ 
    from
         test. test_lots  testlot0_ 
    inner join
        odata.lot_attributes_exp attributev1_ 
            on  testlot0_. test_lot_id=attributev1_. test_lot_id 
    order by
         testlot0_. test_lot_id asc

Correct Hibernate request must be like this.

Hibernate: 
    select ...
    from mkyong.stock stock0_

Hibernate: 
    select ...
    from
        mkyong.stock_daily_record stockdaily0_ 
    where
        stockdaily0_.STOCK_ID in (
            select
                stock0_.STOCK_ID 
            from
                mkyong.stock stock0_
        )
wog48 commented 11 months ago

Due to clean-up action. All issues created before 2022 get closed.

Additional remake: @Fetch is a Hibernate specific annotation. As the JPA Processor is independent from the JPA implementation, such annotations get ignored. The JPA Processor generates its own queries mainly to avoid the n + 1 select problem, but also because the requirements could be fulfilled otherwise.