A service configured in spring / resources.groovy does not have a Hibernate session

We have an application with a plugin that contains a service:

public class TaskService {

    public void doSomething( Task task ) {
        // do something with task
        task.save();
    }
}

This works great.


For our "special" client with its special requirements, we have a second application that contains a plug-in from the first application and another plug-in with a special service for this client, which extends the original service and cancels some methods:

public class SpecialTaskService extends TaskService{

    @Override
    public void doSomething( Task task ) {
        // do something special with task
        task.save();
    }
}

In every place in the second application where taskService is entered, we want to have the SpecialTaskService function (also in the plugin from the first application). Thus, we added a special service to .groovy resources under grails-app / conf / spring:

beans = {
        taskService( SpecialTaskService )
}

HibernateException, "task.save()" : org.hibernate.HibernateException: Hibernate, , .

, SessionFactory SpecialService, sessionFactory.currentSession, .

.groovy, .

"hibernateSessionAware", save() merge() ?

+3
1

, Hibernate ( , ). , Spring, taskService(SpecialTaskService)

- ( , ):

import org.springframework.transaction.annotation.Transactional

@Transactional
class SpecialTaskService extends TaskService {

   @Override
   void doSomething(Task task) {
      // do something special with task
      task.save()
   }
}

Transaction:

class SpecialTaskService extends TaskService {

   @Override
   void doSomething(Task task) {
      Task.withTransaction { status ->
         // do something special with task
         task.save()
      }
   }
}
+5

Source: https://habr.com/ru/post/1764349/


All Articles