Update request problem

Hi,

I have a problem when I run my query if I use the database table name, I get the error that the table is not mapped. And if I use the Entity class I have a null pointer as you can see. This is my first update request, did I miss something? Thanks in advance :
My code :

    String queryTrue = "";
String query2True = "";
String queryFalse = "";
String query2False = "";
Integer countUpdate = 0;
Integer countUpdate2 = 0;

// Requete SQL
queryTrue =
    String.format(
        "UPDATE Project SET archived = true WHERE Project.contractE = :idContrat");

queryFalse =
    String.format(
        "UPDATE Project SET archived = false WHERE Project.contractE = :idContrat");

if (etatArchive) {
  javax.persistence.Query q = JPA.em().createQuery(queryTrue);
  q.setParameter("idContrat", idContrat);
  countUpdate = q.executeUpdate();
} else {
  javax.persistence.Query q = JPA.em().createQuery(queryFalse);
  q.setParameter("idContrat", idContrat);
  countUpdate = q.executeUpdate();
}

My Error :

    2022-06-16 21:31:22.132 ERROR 11532 --- [nio-8080-exec-5] com.axelor.meta.ActionHandler            : null
java.lang.reflect.InvocationTargetException: null
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.axelor.meta.ActionHandler.call(ActionHandler.java:256)
	at com.axelor.meta.schema.actions.ActionMethod.evaluate(ActionMethod.java:76)
	at com.axelor.meta.schema.actions.Action.execute(Action.java:100)
	at com.axelor.meta.schema.actions.Action.wrap(Action.java:109)
	at com.axelor.meta.schema.actions.ActionGroup.evaluate(ActionGroup.java:231)
	at com.axelor.meta.schema.actions.Action.execute(Action.java:96)
	at com.axelor.meta.schema.actions.Action.wrap(Action.java:109)
	at com.axelor.meta.ActionHandler.execute(ActionHandler.java:524)
	at com.axelor.meta.ActionExecutor.execute(ActionExecutor.java:57)

I’m moving forward now when I run this query :

queryFalse =
        String.format(
            "UPDATE Project AS p SET p.archived = false WHERE p.contract.id = :idContrat ");

 javax.persistence.Query q = JPA.em().createQuery(queryFalse);
      q.setParameter("idContrat", idContrat);
      countUpdate = q.executeUpdate();

I get this error, why? please :

Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query
	at org.hibernate.query.internal.AbstractProducedQuery.executeUpdate(AbstractProducedQuery.java:1496)

Have you used @Transactional annotation on your method ?

I’ll try I saw in the doc that it could be the cause. I’ll test it and let you know if it worked. Thank you for your help.

PHPierre ,

I have to use this annotation from which package import? please ?

I took this one and it returns a null pointer exception :

import com.google.inject.persist.Transactional;

Yeah that good, can you show the full stacktrace ?

2022-06-20 08:10:45.436 ERROR 12516 — [nio-8080-exec-5] com.axelor.meta.ActionHandler : null
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.axelor.meta.ActionHandler.call(ActionHandler.java:256)
at com.axelor.meta.schema.actions.ActionMethod.evaluate(ActionMethod.java:76)
at com.axelor.meta.schema.actions.Action.execute(Action.java:100)
at com.axelor.meta.schema.actions.Action.wrap(Action.java:109)
at com.axelor.meta.schema.actions.ActionGroup.evaluate(ActionGroup.java:231)
at com.axelor.meta.schema.actions.Action.execute(Action.java:96)
at com.axelor.meta.schema.actions.Action.wrap(Action.java:109)
at com.axelor.meta.ActionHandler.execute(ActionHandler.java:524)
at com.axelor.meta.ActionExecutor.execute(ActionExecutor.java:57)
at com.axelor.meta.ActionExecutor$$EnhancerByGuice$$90d7ab77.CGLIB$execute$0()
at com.axelor.meta.ActionExecutor$$EnhancerByGuice$$90d7ab77$$FastClassByGuice$$b3c9dd3b.invoke()
at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)
at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:76)
at com.axelor.rpc.ResponseInterceptor.invoke(ResponseInterceptor.java:58)
at com.google.inject.internal.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:78)
at com.google.inject.internal.InterceptorStackCallback.intercept(InterceptorStackCallback.java:54)
at com.axelor.meta.ActionExecutor$$EnhancerByGuice$$90d7ab77.execute()
at com.axelor.web.service.ActionService.execute(ActionService.java:107)
at com.axelor.web.service.ActionService$$EnhancerByGuice$$6e3a19da.CGLIB$execute$0()
at com.axelor.web.service.ActionService$$EnhancerByGuice$$6e3a19da$$FastClassByGuice$$85164d8.invoke()
at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)

the following :

Caused by: java.lang.NullPointerException: null
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1192)
at java.lang.String$CaseInsensitiveComparator.compare(String.java:1186)
at java.util.TreeMap.getEntryUsingComparator(TreeMap.java:376)
at java.util.TreeMap.getEntry(TreeMap.java:345)
at java.util.TreeMap.get(TreeMap.java:278)
at org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:45)
at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:369)
at org.hibernate.hql.internal.ast.tree.IdentNode.getDataType(IdentNode.java:374)
at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:652)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:5003)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1286)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4707)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:4175)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2138)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:815)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.updateStatement(HqlSqlBaseWalker.java:390)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:269)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:266)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:189)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:115)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:77)

As much for me the problem of the null pointer came from another request.
Thank you for your help. I close the topic.

1 J'aime