[rules-users] Hibernate session and 'from'

Gareth Evans evans.g.p at gmail.com
Fri Apr 8 02:33:20 EDT 2011


Hello,

I am using Hibernate lazy loading in the facts I am inserting and am
noticing some odd behaviour when I use 'from'.

Here is a simplified example of my problem. In the first rule I just
want to populate the working memory with the Child objects, it uses
Hibernate lazy loading fine and inserts the facts, however in the
second rule I get an exception when accessing the GrandGrandChild
saying that the object does not have a session. If I add some debug
code to the first rule to access the GrandGrandChild I get the same
error.

This is using session-per-request and the Parent object was inserted
in the current session.

rule "get children - broken"
       when
               $parent : Parent( )
               $child : Child( ) from $parent.children
       then
               insert($child);
end

rule "do stuff"
       when
               $child : Child( )
       then
               GrandChild grandChild = $child.getGrandChild();
               # it blows up on the next line
               GrandGrandChild grandGrandChild =
grandChild.getGrandGrandChild();
end

If I replace the first rule with the following everything works as
expected and I can match on Child objects and access whatever I wish,
so I know there should be a session available in the "do stuff" rule.

rule "get children - working"
       when
               $parent : Parent( )
       then
               for(Child child : $parent.getChildren()) {
                       insert(child);
               }
end

I am able to use the "working" version in my code however the "broken"
version seems like the more correct way to do this in Drools. I'd like
to understand what is going on in case it is my fault or an issue I
need to be aware of. Could it be something to do with the from keyword
or the "cast" to Child( )? Is there some "magic" done in Drools which
could remove/invalidate the Hibernate proxy?

Thanks,
Gareth



More information about the rules-users mailing list