0

I have a spring application which works with hibernate and annotations. A class X (that is outside the Spring context) acessing a class Y (that is in the Spring context).
This first communication I solved using a class that implements ApplicationContextAware according to this Stack Overflow aswer How to access a method a Spring Bean from a class not in the Spring Container

The class Y injects a @Service class Z that persist information in the database (and it has a @Transactional annotation in the method that persists the information).

The problem is that when a call from Y calls Z.merge() (x -> Y -> Z.merge()), the following exception occurs:

Caused by: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:282)
at com.sun.proxy.$Proxy48.merge(Unknown Source)

Is there any possibility for this work? I am usign Spring 4.3.3

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • You you at least add some pseudo-code? It is a little difficult to picture what you are describing. – woemler Jan 17 '17 at 18:42
  • Are you sure that your transaction configuration is setup properly? Your `X` class is a non-spring bean which accesses a non-transactional spring bean `Y` which calls another spring-bean, `Z` which is transactional. If transaction management is setup correctly and you're getting `Y` from from the application context, this should work. – Naros Jan 17 '17 at 18:47
  • I solved the problem making the Y class a Component (something like @Naros suggested). – Anderson Fachini Jan 17 '17 at 19:22
  • Right based on your description, the assumption was that `Y` was already annotated or declared as a spring-bean. If it didn't have `@Component` or equivalent annotation, then it doesn't sound like it was originally a spring-bean like your original description stated. – Naros Jan 17 '17 at 20:24

0 Answers0