Hi. Here is my test shows me that javax.persistence.fetchgraph does not work as documented in java docs(Attributes explicitly specified as AttributeNodes are treated as FetchType.EAGER,attributes that are not specified are treated as FetchType.LAZY:
Client entity:
{code} @Entity @Table(name = "client") @NamedEntityGraphs({ @NamedEntityGraph(name = Client.ACCOUNTS_GRAPH, attributeNodes = @NamedAttributeNode(value = "accounts")) }) @Getter @Setter @ToString(of = "name") public class Client extends BaseEntity<Integer> {
public static final String ACCOUNTS_GRAPH = "Client.accounts";
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "c_seq") @SequenceGenerator(name = "c_seq", sequenceName = "c_seq", allocationSize = 1) @Column(name = "ID_CLIENT") public Integer id;
@Column(name = "NAME") private String name;
@Column(name = "age") private int age;
@OneToMany(mappedBy = "client", cascade = CascadeType.PERSIST) @Setter(AccessLevel.NONE) private List<Account> accounts = new ArrayList<>();
@OneToOne(mappedBy = "client", fetch = FetchType.EAGER) private Passport passport;
@OneToMany(mappedBy = "client", fetch = FetchType.EAGER) private List<Address> addresses = new ArrayList<>();
public void addAccount(Account account) { accounts.add(account); account.setClient(this); } } {code}
Main: {code} Map<String, Object> hints = new HashMap<>(); hints.put(QueryHints.HINT_FETCHGRAPH, em.getEntityGraph(Client.ACCOUNTS_GRAPH)); em.find(Client.class, 1, hints); {code}
Console output: {code} Hibernate: select client0_.id_client as id_clien1_4_0_, client0_.age as age2_4_0_, client0_.name as name3_4_0_, accounts1_.id_client as id_clien3_4_1_, accounts1_.id_account as id_accou1_0_1_, accounts1_.id_account as id_accou1_0_2_, accounts1_.amount as amount2_0_2_, accounts1_.id_client as id_clien3_0_2_ from client client0_ left outer join account accounts1_ on client0_.id_client=accounts1_.id_client where client0_.id_client=1 Hibernate: select passport0_.id_passport as id_passp1_7_0_, passport0_.number as number2_7_0_ from passport passport0_ where passport0_.id_passport=1 Hibernate: select addresses0_.id_client as id_clien4_4_0_, addresses0_.id_address as id_addre1_1_0_, addresses0_.id_address as id_addre1_1_1_, addresses0_.city as city2_1_1_, addresses0_.id_client as id_clien4_1_1_, addresses0_.street as street3_1_1_ from address addresses0_ where addresses0_.id_client=1 {code} As we can see LAZY does not work. If I change FETCH_GRAPH to LOAD_GRAPH:
{code} select client0_.id_client as id_clien1_4_0_, client0_.age as age2_4_0_, client0_.name as name3_4_0_, accounts1_.id_client as id_clien3_4_1_, accounts1_.id_account as id_accou1_0_1_, accounts1_.id_account as id_accou1_0_2_, accounts1_.amount as amount2_0_2_, accounts1_.id_client as id_clien3_0_2_, addresses2_.id_client as id_clien4_4_3_, addresses2_.id_address as id_addre1_1_3_, addresses2_.id_address as id_addre1_1_4_, addresses2_.city as city2_1_4_, addresses2_.id_client as id_clien4_1_4_, addresses2_.street as street3_1_4_, passport3_.id_passport as id_passp1_7_5_, passport3_.number as number2_7_5_ from client client0_ left outer join account accounts1_ on client0_.id_client=accounts1_.id_client left outer join address addresses2_ on client0_.id_client=addresses2_.id_client left outer join passport passport3_ on client0_.id_client=passport3_.id_passport where client0_.id_client=1 {code} |
|