Can a @LazyToOne relationship be overriden to a eager fetch


I’m using byte code enhancement for a child side of @OneToOne relationship with @LazyToOne(LazyToOneOption.NO_PROXY).

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “id_node”, nullable = false)
private Node node;

In a use case, I want to eager fetch this relationship. Even if SQL logs show me that the join is done, it doesn’t look like the session is handling it correctly. When I access the Node entity, its’ generating a lot of small queries and I’m getting a N+1 query problem.

        dsnodeback0_.id_ds_node_backw_curve as id_ds_no1_16_0_,
        node1_.id_node as id_node1_51_1_,
        dsnodeback0_.calc_h_v_ratio as calc_h_v2_16_0_,
        dsnodeback0_.corrc_polc as corrc_po3_16_0_,
        dsnodeback0_.description as descript4_16_0_,
        dsnodeback0_.filtr_factr as filtr_fa5_16_0_,
        dsnodeback0_.max_vol_opt as max_vol_6_16_0_,
        dsnodeback0_.usr_h_v_ratio as usr_h_v_7_16_0_,
        node1_.description as descript2_51_1_, as name3_51_1_,
        node1_.personalized_order as personal4_51_1_,
        node1_.area as area5_51_1_,
        node1_.constant_flow as constant6_51_1_,
        node1_.displayed_color as displaye7_51_1_,
        node1_.displayed_x as displaye8_51_1_,
        node1_.displayed_y as displaye9_51_1_,
        node1_.displayed_size as display10_51_1_,
        node1_.end_block_type as end_blo11_51_1_,
        node1_.ground_elevation as ground_12_51_1_,
        node1_.has_tide_gate as has_tid13_51_1_,
        node1_.height_init_policy as height_14_51_1_,
        node1_.high_level as high_le15_51_1_,
        node1_.initial_water_height as initial16_51_1_,
        node1_.invert_elevation as invert_17_51_1_,
        node1_.is_flow_input as is_flow18_51_1_,
        node1_.is_in_ctrl_network as is_in_c19_51_1_,
        node1_.is_interest as is_inte20_51_1_,
        node1_.is_simplified_model as is_simp21_51_1_,
        node1_.node_type as node_ty22_51_1_,
        node1_.real_x as real_x23_51_1_,
        node1_.real_y as real_y24_51_1_,
        node1_.simplified_model_src_type as simplif25_51_1_,
        node1_.very_high_level as very_hi26_51_1_,
        node1_.water_surface_elevation as water_s27_51_1_ 
        ds_node_backw_curve dsnodeback0_ 
    left outer join
        node node1_ 
            on dsnodeback0_.id_node=node1_.id_node 

I’m getting a lot of those in a loop accessing the Node (parent) from the child entity

        dsnodeback_.id_node as id_node8_16_ 
        ds_node_backw_curve dsnodeback_ 

I’m using hibernate 5.2.16 with hibernate-enhance-maven-plugin

Those queries could come from different data access methods. You need to add the entity classes involved in this relationship, as well as the data access code and the Hibernate log that’s generated when accessing those data access methods.

Or, try to replicate this issue using this test case template so we can take a better look on it.