Search code examples
javahibernatejakarta-eemappingmany-to-one

Why can't I use two lists on the same entity class using FetchType.EAGER


I am implementing an entity class which has 2 relations one-to-many but I can't use fetch = FetchType.EAGER for the two list. I should use FetchType.LAZY for both.

Is there a solution to use EAGER for both?

@OneToMany(mappedBy="fournisseur",fetch = FetchType.EAGER,cascade={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE} )
private List<Carburant_entree> carburant_entrees; 

@OneToMany(mappedBy="user",fetch = FetchType.EAGER,cascade={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE} )
private List<Users> theusers; 

Solution

  • Just use Set in place of List

    @OneToMany(mappedBy="fournisseur",fetch = FetchType.EAGER,cascade={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE} )
    private Set<CarburantEntree> carburantEntrees; 
    
    @OneToMany(mappedBy="user",fetch = FetchType.EAGER,cascade={ CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE} )
    private Set<Users> users;
    

    Please, follow the Java Naming Conventntion.