First off, the query should be:

from Customers where TN = :tn and balance > 0

And that would be only if you mapped the tn property to the name "TN". HQL 
queries are case sensitive when it comes to property names: 
http://www.hibernate.org/hib_docs/v3/reference/en-US/html_single/#queryhql-casesensitivity

Second, I don't know if that whole "hibernateSession.createQuery" expression 
would compile in a DRL file.  Have you tried it?  Anyone?

Finally, this rule will fire for every Customer in working memory and look up a 
matching Customer in the database.  Seems a bit inneficient, especially if you 
have many rules of this type. Would it be possible to load the Customer from 
the database and just insert it into working memory  with it's "balance" 
property already set?  That would be far simpler.  Then the rule would be:

global org.hibernate.Session hibernateSession;

when 
    cust1:Customer(balance > 0)
then
    cust1.setIsAllowed(false);
    hibernateSession.save(cust1);
end

(Ignoring the transaction management, here.)

You could even skip the save() on the hibernate session in the rule action.  
You could keep the hibernate session open and save the Customers when you've 
completed all changes.

--- On Tue, 3/31/09, a...@work <[email protected]> wrote:

> From: a...@work <[email protected]>
> Subject: [rules-users] Guvnor, database and rule language
> To: [email protected]
> Date: Tuesday, March 31, 2009, 3:04 PM
> I am trying to use drools to evaluate the following rule:
> 
> Given a telephone number, if the telephone number already
> exists in database
> and has a balance > 0, set isAllowed = false. I cannot
> figure out how to
> write the rule. 
> 
> This is my object
> 
> public class Customer implements Serializable {
>       private static final long serialVersionUID = 1L;
>       private String tn;
>       private Double balance;
>         private Boolean isAllowed;
> 
>         // getters/ setters
> }
> 
> Can anyone help me with the rule below? The TN is set in
> the input fact. Is
> the query below correct? It looks incorrect. 
> 
> global org.hibernate.Session hibernateSession;
> 
> when 
>     cust1:Customer()
>     cust2:Customer() from
> hibernateSession.createQuery("Select * from
> Customers Where TN = :tn and balance >
> 0).setProperties({"tn":cust1.getTn()})list()
> 
> then
>     cust1.setIsAllowed(false);
> 
> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/Guvnor%2C-database-and-rule-language-tp22810206p22810206.html
> Sent from the drools - user mailing list archive at
> Nabble.com.
> 
> _______________________________________________
> rules-users mailing list
> [email protected]
> https://lists.jboss.org/mailman/listinfo/rules-users


      
_______________________________________________
rules-users mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/rules-users

Reply via email to