Entity not save - no transaction is in progress

Hi,

I have a list of a specified entity.
I would like to intercept modification to create new entity.

Now, I use the onChange event to run action-method. It works. I success to create a new object, but I cannot save it. This is the given error when save :

[p-nio-88-exec-3] 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:185)
	at com.axelor.meta.schema.actions.ActionMethod.evaluate(ActionMethod.java:76)
	at com.axelor.meta.schema.actions.Action.execute(Action.java:98)
	at com.axelor.meta.schema.actions.Action.wrap(Action.java:107)
	at com.axelor.meta.schema.actions.ActionGroup.evaluate(ActionGroup.java:230)
	at com.axelor.meta.schema.actions.Action.execute(Action.java:95)
	at com.axelor.meta.schema.actions.Action.wrap(Action.java:107)
	at com.axelor.meta.ActionHandler.execute(ActionHandler.java:453)
	at com.axelor.meta.ActionExecutor.execute(ActionExecutor.java:43)
	at com.axelor.meta.ActionExecutor$$EnhancerByGuice$$fd4b78b8.CGLIB$execute$0(<generated>)
	at com.axelor.meta.ActionExecutor$$EnhancerByGuice$$fd4b78b8$$FastClassByGuice$$65846e73.invoke(<generated>)
	at com.google.inject.internal.cglib.proxy.$MethodProxy.invokeSuper(MethodProxy.java:228)
Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress
	at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3461)
	at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1425)
	at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1421)
	at com.axelor.db.JPA.persist(JPA.java:128)
	at com.axelor.db.JPA.save(JPA.java:159)
	at com.axelor.db.JpaRepository.save(JpaRepository.java:85)
	at fr.elikill58.db.Generator.myAction(Generator.java:68)
	... 97 common frames omitted

My code :

public void myAction(ActionRequest request, ActionResponse response) {
     MyObjectRepository repo = Beans.get(MyObjectRepository.class);
     Map<String, Object> contentOfObject = new HashMap<>();
     MyObject obj = repo.save(repo.create(contentOfObject)); // (this line create error)
}

have you annotated it with @Transactional ?? also the repo file is added in module to replace default repo ?

Annotated what ? the method ?
I was using default repo

I find something (which not fully solve my problem) :

JPA.runInTransaction(() -> {
     repo.save(repo.create(contentOfObject));
}); 

This save the object in DB, and if I refresh the page, hey will be showed. But, I have to refresh the page:

  • the main object is not showed as saved
  • in the relation field, there is a field which correspond at an object which have not be saved, but if I save the main object, it will replace my own object that I create before.

The transaction operations should be done in a service, not in a controller.

On this service, you can simply add the @transactionnal annotation on your method.

1 « J'aime »

Ok so I have to do it when I run an event and not an action ?

And I found something which works :

  1. Use JPA.runInTransaction()
  2. Edit field thanks to response.setValue()

response.setValue() can be used to return a value to the view, then the user have to save manually the record.

@Transactional method can be used to save a change in database, then need to use response.setReload(true) to update the context of the view.

2 « J'aime »

Ce sujet a été automatiquement fermé après 30 jours. Aucune réponse n’est permise dorénavant.