0

I want to use criteria with some conditions of its one to many field.

Introducing my model briefly, one item has multiple options.
Because I want to get item with filtered options, and pass it to JSON Parser (with no session), I have to get options when I( query it.

I have coded like below before adding restrictions to options, it works for me fine.

Item item = (Item)session.createCriteria(Item.class)                                        
                .setFetchMode("options", FetchMode.JOIN)
                .add(Restrictions.eq("id", id))
                .uniqueResult();

But when I added restrictions to options it occurs an error (exactly, when a called function tries to access item, searching works fine.)

Item item = (Item)session.createCriteria(Item.class)
                .setFetchMode("options", FetchMode.JOIN)
                .add(Restrictions.eq("id", id))
                .createCriteria("options").add(Restrictions.eq("status", ItemStatus.ABLE))
                .uniqueResult();

An error is :

failed to lazily initialize a collection of role: options, could not initialize proxy - no Session

I have added setFetchMode to the end of sub criteria, the result was same.

What happened!? Could you tell the solution?

Genzotto
  • 1,954
  • 6
  • 26
  • 45
crazy_rudy
  • 533
  • 2
  • 4
  • 19
  • I tried with removing `.setFetchMode("options", FetchMode.JOIN)` then also I am getting proper result. – Amogh Jun 18 '14 at 06:26
  • @Amogh searching data is not my problem, my problem is to access `options` after session is closed. – crazy_rudy Jun 18 '14 at 06:29
  • @Amogh the reason that i have set fetch mode is for accessing `options` after session is closed. – crazy_rudy Jun 18 '14 at 06:30
  • @Amogh but when i added restrictions to `options`, an error is occured. although i have set fetch mode as join to options. – crazy_rudy Jun 18 '14 at 06:31
  • 1
    Setting fetch Mode is not used to access that association after session closed. It tells about how the association get fetched. – Amogh Jun 18 '14 at 06:35
  • Go through this link https://community.jboss.org/wiki/AShortPrimerOnFetchingStrategies – Amogh Jun 18 '14 at 06:50
  • @Amogh, i just solved this problem by adding `@Fetch(FetchMode.JOIN)` annotation to options field. but i have more curiosity about why setFetchMode doesn't work with sub criteria... it's not make sense.... – crazy_rudy Jun 18 '14 at 07:04

1 Answers1

0

I think you are getting error when you trying to access options after session is closed. according to hibernate collections are lazy-loaded by default. If you want to fetch options along with item then you have to set FetchType EAGER (FetchType.EAGER) in annotation or mapping file whatever you are using.

Can you try with this Code

Item item = (Item)session.createCriteria(Item.class)
            .setFetchMode("options", FetchMode.SELECT)
            .add(Restrictions.eq("id", id))
            .createCriteria("options").add(Restrictions.eq("options.status", ItemStatus.ABLE))
            .uniqueResult();
Amogh
  • 4,453
  • 11
  • 45
  • 106
  • That's not an answer i want, before adding restrictions using sub criteria, it works fine. – crazy_rudy Jun 18 '14 at 06:11
  • 1
    can you able to debug your code. so that we can get know at which line you getting exception. Because as per error it says you are trying to load options when session get closed or may be your session management configuration is set to close session when you commit transaction.I done the same in my code it works fine. – Amogh Jun 18 '14 at 06:18
  • @user1372488 yes, my program accesses `options` after closing session. searching data in both cases are fine, but my problem is how to access `options` after closing session. accessing `options` after closing session is fine with first code. because it's set fetch mode as join. (without setting fetch mode, it occurs the same error) but second code does not work, i did just add restrictions to `options`! – crazy_rudy Jun 18 '14 at 06:26
  • `status` is property of `option` or `item`? – Amogh Jun 18 '14 at 06:28
  • @crazy_rudy Restrictions added by you for `status` will be applied to item alias not to option.Use alias to apply on option like `options.status` – Amogh Jun 18 '14 at 06:38