Hi,
We are having some problems using recompute. We would like recalcule the "amountTotal" of 'Alpha and Sons' customer. To do that we are using the following code:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("OntimizeLogicTesting");
Set<String> entityNamesToCheck = new HashSet<String>();
entityNamesToCheck.add("buslogicdemo.data.pojo.Purchaseorder");
HashMap<String, String> entityCriteria = new HashMap<String, String>();
entityCriteria.put("buslogicdemo.data.pojo.Purchaseorder", "customer.name = 'Alpha and Sons'");
Configuration recomputeConfiguration = new Configuration();
recomputeConfiguration.setEntityNamesToCheck(entityNamesToCheck);
recomputeConfiguration.setEntityCriteria(entityCriteria);
Engine engine = new Engine(recomputeConfiguration, entityManagerFactory);
engine.fix();
This code throws an SQL Exception. This is the console log with the exception:
abl.recomp - Beginning fix
abl.depend - Creating new LogicAnalysisManager for Engine com.autobizlogic.abl.engine.LogicEngine@b54ef7
abl.depend - Analyzing dependencies for Java logic class : buslogicdemo.businesslogic.pojo.PurchaseorderLogic
Ordered rules:
Sum buslogicdemo.data.pojo.Purchaseorder#deriveAmountTotal, summing: amount over role lineitems
abl.recomp - Checking aggregate: Sum buslogicdemo.data.pojo.Purchaseorder#deriveAmountTotal, summing: amount over role lineitems
org.hibernate.SQL -
update
Purchaseorder
set
amount_total=coalesce((select
sum(lineitems1_.amount)
from
lineitem lineitems1_
where
Purchaseorder.order_number=lineitems1_.order_number),
0)
where
(
amount_total is null
)
and coalesce((select
sum(lineitems2_.amount)
from
lineitem lineitems2_
where
Purchaseorder.order_number=lineitems2_.order_number), 0)<>0
and purchaseor0_.customer_name='Alpha and Sons'
org.hibernate.util.JDBCExceptionReporter - User lacks privilege or object not found: PURCHASEOR0_.CUSTOMER_NAME
Exception in thread "main" java.lang.RuntimeException: org.hibernate.exception.SQLGrammarException: could not execute update query
at com.autobizlogic.recompute.fix.FixTask.run(FixTask.java:64)
at com.autobizlogic.recompute.Engine.fix(Engine.java:135)
at test.MainTest.recompute(MainTest.java:329)
at test.MainTest.main(MainTest.java:59)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:110)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:423)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:283)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1288)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:117)
at com.autobizlogic.recompute.fix.AggregateFix.executeExpression(AggregateFix.java:104)
at com.autobizlogic.recompute.fix.FixTask.fix(FixTask.java:90)
at com.autobizlogic.recompute.fix.FixTask.run(FixTask.java:61)
... 3 more
Caused by: java.sql.SQLException: User lacks privilege or object not found: PURCHASEOR0_.CUSTOMER_NAME
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:213)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:88)
... 10 more
Are we using the right criteria or is there a better way to recalculate this information?
Cheers,
Sira
Anonymous
Well, this sum is for Purchaseorder, but the where clause is for Customer. Could you try specifying it as:
entityCriteria.put("buslogicdemo.data.pojo.Purchaseorder", "p.customer.name = 'Alpha and Sons'");
because p is the alias we use in the HQL query. If this does not work for you, let me know and we'll figure something out.
Hi,
We have changed the criteria but we have gotten the same exception. To get more information, we've activated the HQL log4Java logger, and we were able to see the HQL code generated by Recompute.
We have following HQL sentence with the old criteria:
With your suggested criteria, we have this HQL sentence:
But the SQL generated by Hibernate is the same in both cases:
Cheers,
Sira
Hi,
I was reviewing this bug and I think I get the cause of the exception: It seems Hibernate doesn't generate the SQL sentence in right way from Recompute HQL sentence.
When we use Recompute with the following criteria:
Recompute generates this HQL sentence:
Then Hibernate generates the following SQL sentence:
When Hibernate executes that SQL sentence, it will get the following Exception:
...
I did a test executing the Recompute's HQL sentence in an independent code but I allways got the above exception. Then I changed the order of conditions, putting the Recompute criteria condition first and I get the following HQL sentence:
I don't know why but Hibernate generates a different SQL sentence to this HQL and it doesn't throw any exception. This is the generated SQL without exceptions:
This is the main method used to check the HQL sentence in our persistence unit. This method will throw an exception but if you change the order of HQL conditions it will run fine:
I hope this will help you to check the bug.
Cheers,
Sira