0

I am trying to fetch an entity using criteria API.

Here is what header entity looks like

public class Header{
    @OneToMany(mappedBy = "header", cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @JsonManagedReference
    private List<Item> items;
}

Can I restrict not to load items? I tried this. Please Help

Criteria criteria = session.createCriteria(Header.class);
criteria.add(Restrictions.eq("id", id));
criteria.setFetchMode("header.items",FetchMode.LAZY);
return (Header) criteria.uniqueResult();

logged the mysql queries it looks like

/* MyService Health Check */ SELECT 1
        3 Query /* criteria query */ select this_.id as id1_0_0_, this_.created_at as created_2_0_0_, this_.updated_at as updated_3_0_0_, this_.created_by as created_4_0_0_, this_.description as descript5_0_0_, this_.due_date as due_date6_0_0_, this_.party_id_from as party_id7_0_0_, this_.party_id_to as party_id8_0_0_, this_.reference_id as referenc9_0_0_, this_.sub_type as sub_typ10_0_0_, this_.total_amount_with_tax as total_a11_0_0_, this_.total_amount_without_tax as total_a12_0_0_, this_.type as type13_0_0_ from headers this_ where this_.id=1
        3 Query SHOW WARNINGS
        3 Query select items0_.header_id as header_i7_0_0_, items0_.id as id1_2_0_, items0_.id as id1_2_1_, items0_.created_at as created_2_2_1_, items0_.updated_at as updated_3_2_1_, items0_.amount as amount4_2_1_, items0_.header_id as header_i7_2_1_, items0_.ignore_reco as ignore_r5_2_1_, items0_.type as type6_2_1_ from items items0_ where items0_.header_id=1
akash
  • 1,653
  • 5
  • 22
  • 38
  • How do you know that the items are loaded? Is there something in the query log, or is your issue that you can access them later? Or do you want to serialize it and it should not be added in that case? – mh-dev Mar 10 '16 at 07:52
  • i put a debugger at the last line `return (Header) criteria.uniqueResult();` . The returning response has items in it. – akash Mar 10 '16 at 07:54
  • The items list in that case is a proxy which will get loaded on access. So if your access it with your debuger the data should be loaded. It would only be wrong if the query log would load the data with the uniqueResult request. – mh-dev Mar 10 '16 at 07:55
  • So you mean to say .. that because i may be serialising Header to Response with subItems in it. It will fire query.? – akash Mar 10 '16 at 08:02
  • The serialization will access the items list that will trigger the data "lazy" fetching and the data will get loaded from the database. If you do not want this behaviour, you need to detach the entity from the persistence context before the serialization happens. – mh-dev Mar 10 '16 at 08:29
  • I tried this header.setItems(null); before serialization but it firing the query on db. And moreover i checked criteria.uniqueResult() , this itself is firing query on items. I dont think it is doing lazyloading – akash Mar 10 '16 at 08:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105874/discussion-between-akash-and-mh-dev). – akash Mar 10 '16 at 08:36

1 Answers1

0

You can define the initialization while declaring the relationship with the entity. Your code will look like

public class Header{
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="items")
    private List<Item> items;
}

and your fetch code will look like

Criteria criteria = session.createCriteria(Header.class);
criteria.add(Restrictions.eq("id", id));
return (Header) criteria.uniqueResult();

Fetch type lazy initializes a proxy for loading the entities as soon as they are requested. So when you try to access the items within session lifecycle you will be able to fetch the items.

But if you try to access outside the session lifecycle, you will get the LazyInitializationException.

Sandeep Sukhija
  • 1,072
  • 14
  • 26
  • what if i want to load the items ? what should be the restriction in criteria? – akash Mar 10 '16 at 12:07
  • If you want to load all the items, you have to make sure you are doing so in the active session. This link http://stackoverflow.com/a/2192256/873976 can help. – Sandeep Sukhija Mar 10 '16 at 13:21