Spring 5 – для текущего потока нет EntityManager с фактической транзакциейJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Spring 5 – для текущего потока нет EntityManager с фактической транзакцией

Сообщение Anonymous »

Дорогие мои, я пытался активировать автоматическое управление транзакциями, используя @EnableTransactionManagement в моих классах конфигурации и @Transactional в моих классах обслуживания. Я также проверил критерии, согласно которым классы должны иметь общедоступные методы и вызываться извне компонента. К сожалению, мне пока не удалось найти причину, по которой транзакции не запускаются автоматически.

Сообщение об ошибке:
Нет EntityManager с фактической транзакцией, доступной для текущего потока - невозможно надежно обработать вызов слияния;

Есть идеи, что я мог бы попробовать? Заранее спасибо.

Я использую
  • spring.version 5.1.4.RELEASE
  • spring.data.version 2.1.3.RELEASE
  • hibernate.version>5.4.1.Final
  • java.version 1.8
StackTrace:

[2020-03-18 20:40:28] [http-nio-8080-exec-7] ERROR c.o.c.w.r.c.e.RestResponseEntityExceptionHandler - 500 Status Code
org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - c a n n o t r e l i a b l y p r o c e s s ' m e r g e ' c a l l < b r / > a t o r g . s p r i n g f r a m e w o r k . o r m . j p a . E n t i t y M a n a g e r F a c t o r y U t i l s . c o n v e r t J p a A c c e s s E x c e p t i o n I f P o s s i b l e ( E n t i t y M a n a g e r F a c t o r y U t i l s . j a v a : 4 0 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . o r m . j p a . v e n d o r . H i b e r n a t e J p a D i a l e c t . t r a n s l a t e E x c e p t i o n I f P o s s i b l e ( H i b e r n a t e J p a D i a l e c t . j a v a : 2 5 5 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . o r m . j p a . A b s t r a c t E n t i t y M a n a g e r F a c t o r y B e a n . t r a n s l a t e E x c e p t i o n I f P o s s i b l e ( A b s t r a c t E n t i t y M a n a g e r F a c t o r y B e a n . j a v a : 5 2 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a o . s u p p o r t . C h a i n e d P e r s i s t e n c e E x c e p t i o n T r a n s l a t o r . t r a n s l a t e E x c e p t i o n I f P o s s i b l e ( C h a i n e d P e r s i s t e n c e E x c e p t i o n T r a n s l a t o r . j a v a : 6 1 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a o . s u p p o r t . D a t a A c c e s s U t i l s . t r a n s l a t e I f N e c e s s a r y ( D a t a A c c e s s U t i l s . j a v a : 2 4 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a o . s u p p o r t . P e r s i s t e n c e E x c e p t i o n T r a n s l a t i o n I n t e r c e p t o r . i n v o k e ( P e r s i s t e n c e E x c e p t i o n T r a n s l a t i o n I n t e r c e p t o r . j a v a : 1 5 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . j p a . r e p o s i t o r y . s u p p o r t . C r u d M e t h o d M e t a d a t a P o s t P r o c e s s o r $ C r u d M e t h o d M e t a d a t a P o p u l a t i n g M e t h o d I n t e r c e p t o r . i n v o k e ( C r u d M e t h o d M e t a d a t a P o s t P r o c e s s o r . j a v a : 1 3 5 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . i n t e r c e p t o r . E x p o s e I n v o c a t i o n I n t e r c e p t o r . i n v o k e ( E x p o s e I n v o c a t i o n I n t e r c e p t o r . j a v a : 9 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . d a t a . r e p o s i t o r y . c o r e . s u p p o r t . S u r r o u n d i n g T r a n s a c t i o n D e t e c t o r M e t h o d I n t e r c e p t o r . i n v o k e ( S u r r o u n d i n g T r a n s a c t i o n D e t e c t o r M e t h o d I n t e r c e p t o r . j a v a : 6 1 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . J d k D y n a m i c A o p P r o x y . i n v o k e ( J d k D y n a m i c A o p P r o x y . j a v a : 2 1 2 ) < b r / > a t c o m . s u n . p r o x y . $ P r o x y 1 3 6 . m e r g e ( U n k n o w n S o u r c e ) < b r / > a t c o m . m y a p p . p e r s i s t e n c e . s e r v i c e . q u e s t i o n . a n s w e r . A n s w e r S e r v i c e I m p l . c r e a t e E n t i t y ( A n s w e r S e r v i c e I m p l . j a v a : 7 0 ) < b r / > a t c o m . m y a p p . p e r s i s t e n c e . s e r v i c e . q u e s t i o n . a n s w e r . A n s w e r S e r v i c e I m p l . c r e a t e E n t i t y ( A n s w e r S e r v i c e I m p l . j a v a : 3 0 ) < b r / > a t c o m . o n t i u s . c o r e . p e r s i s t e n c e . s e r v i c e . A b s t r a c t S e r v i c e . c r e a t e ( A b s t r a c t S e r v i c e . j a v a : 2 9 4 ) < b r / > a t c o m . o n t i u s . c o r e . p e r s i s t e n c e . s e r v i c e . A b s t r a c t S e r v i c e . c r e a t e O r U p d a t e ( A b s t r a c t S e r v i c e . j a v a : 3 4 4 ) < b r / > a t c o m . m y a p p . p e r s i s t e n c e . s e r v i c e . q u e s t i o n . a n s w e r . A n s w e r S e r v i c e I m p l . c r e a t e O r U p d a t e A n s w e r ( A n s w e r S e r v i c e I m p l . j a v a : 1 3 0 ) < b r / > a t c o m . m y a p p . p e r s i s t e n c e . s e r v i c e . q u e s t i o n . a n s w e r . A n s w e r S e r v i c e I m p l . c r e a t e O r U p d a t e A n s w e r ( A n s w e r S e r v i c e I m p l . j a v a : 1 4 3 ) < b r / > a t c o m . m y a p p . r e s t . c o n t r o l l e r . r e s t . U s e r C o n t r o l l e r . s a v e A n s w e r s ( U s e r C o n t r o l l e r . j a v a : 1 5 7 ) < b r / > a t c o m . m y a p p . r e s t . c o n t r o l l e r . r e s t . U s e r C o n t r o l l e r $ $ F a s t C l a s s B y S p r i n g C G L I B $ $ 7 d c 3 1 b e 3 . i n v o k e ( & l t ; g e n e r a t e d & g t ; ) < b r / > a t o r g . s p r i n g f r a m e w o r k . c g l i b . p r o x y . M e t h o d P r o x y . i n v o k e ( M e t h o d P r o x y . j a v a : 2 1 8 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . C g l i b A o p P r o x y $ C g l i b M e t h o d I n v o c a t i o n . i n v o k e J o i n p o i n t ( C g l i b A o p P r o x y . j a v a : 7 4 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 6 3 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . a c c e s s . i n t e r c e p t . a o p a l l i a n c e . M e t h o d S e c u r i t y I n t e r c e p t o r . i n v o k e ( M e t h o d S e c u r i t y I n t e r c e p t o r . j a v a : 6 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . R e f l e c t i v e M e t h o d I n v o c a t i o n . p r o c e e d ( R e f l e c t i v e M e t h o d I n v o c a t i o n . j a v a : 1 8 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . a o p . f r a m e w o r k . C g l i b A o p P r o x y $ D y n a m i c A d v i s e d I n t e r c e p t o r . i n t e r c e p t ( C g l i b A o p P r o x y . j a v a : 6 8 8 ) < b r / > a t c o m . m y a p p . r e s t . c o n t r o l l e r . r e s t . U s e r C o n t r o l l e r $ $ E n h a n c e r B y S p r i n g C G L I B $ $ b f b 4 0 8 4 . s a v e A n s w e r s ( & l t ; g e n e r a t e d & g t ; ) < b r / > a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e 0 ( N a t i v e M e t h o d ) < b r / > a t s u n . r e f l e c t . N a t i v e M e t h o d A c c e s s o r I m p l . i n v o k e ( N a t i v e M e t h o d A c c e s s o r I m p l . j a v a : 6 2 ) < b r / > a t s u n . r e f l e c t . D e l e g a t i n g M e t h o d A c c e s s o r I m p l . i n v o k e ( D e l e g a t i n g M e t h o d A c c e s s o r I m p l . j a v a : 4 3 ) < b r / > a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( M e t h o d . j a v a : 4 9 8 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . m e t h o d . s u p p o r t . I n v o c a b l e H a n d l e r M e t h o d . d o I n v o k e ( I n v o c a b l e H a n d l e r M e t h o d . j a v a : 2 1 5 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . m e t h o d . s u p p o r t . I n v o c a b l e H a n d l e r M e t h o d . i n v o k e F o r R e q u e s t ( I n v o c a b l e H a n d l e r M e t h o d . j a v a : 1 4 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . S e r v l e t I n v o c a b l e H a n d l e r M e t h o d . i n v o k e A n d H a n d l e ( S e r v l e t I n v o c a b l e H a n d l e r M e t h o d . j a v a : 1 0 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . R e q u e s t M a p p i n g H a n d l e r A d a p t e r . i n v o k e H a n d l e r M e t h o d ( R e q u e s t M a p p i n g H a n d l e r A d a p t e r . j a v a : 8 9 5 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . a n n o t a t i o n . R e q u e s t M a p p i n g H a n d l e r A d a p t e r . h a n d l e I n t e r n a l ( R e q u e s t M a p p i n g H a n d l e r A d a p t e r . j a v a : 8 0 0 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . m v c . m e t h o d . A b s t r a c t H a n d l e r M e t h o d A d a p t e r . h a n d l e ( A b s t r a c t H a n d l e r M e t h o d A d a p t e r . j a v a : 8 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . D i s p a t c h e r S e r v l e t . d o D i s p a t c h ( D i s p a t c h e r S e r v l e t . j a v a : 1 0 3 8 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . D i s p a t c h e r S e r v l e t . d o S e r v i c e ( D i s p a t c h e r S e r v l e t . j a v a : 9 4 2 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . p r o c e s s R e q u e s t ( F r a m e w o r k S e r v l e t . j a v a : 9 9 8 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . d o P o s t ( F r a m e w o r k S e r v l e t . j a v a : 9 0 1 ) < b r / > a t j a v a x . s e r v l e t . h t t p . H t t p S e r v l e t . s e r v i c e ( H t t p S e r v l e t . j a v a : 6 6 0 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . w e b . s e r v l e t . F r a m e w o r k S e r v l e t . s e r v i c e ( F r a m e w o r k S e r v l e t . j a v a : 8 7 5 ) < b r / > a t j a v a x . s e r v l e t . h t t p . H t t p S e r v l e t . s e r v i c e ( H t t p S e r v l e t . j a v a : 7 4 1 ) < b r / > a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . i n t e r n a l D o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 2 3 1 ) < b r / > a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . d o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 1 6 6 ) < b r / > a t o r g . a p a c h e . t o m c a t . w e b s o c k e t . s e r v e r . W s F i l t e r . d o F i l t e r ( W s F i l t e r . j a v a : 5 3 ) < b r / > a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . i n t e r n a l D o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 1 9 3 ) < b r / > a t o r g . a p a c h e . c a t a l i n a . c o r e . A p p l i c a t i o n F i l t e r C h a i n . d o F i l t e r ( A p p l i c a t i o n F i l t e r C h a i n . j a v a : 1 6 6 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . F i l t e r C h a i n P r o x y $ V i r t u a l F i l t e r C h a i n . d o F i l t e r ( F i l t e r C h a i n P r o x y . j a v a : 3 2 0 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . a c c e s s . i n t e r c e p t . F i l t e r S e c u r i t y I n t e r c e p t o r . i n v o k e ( F i l t e r S e c u r i t y I n t e r c e p t o r . j a v a : 1 2 7 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . a c c e s s . i n t e r c e p t . F i l t e r S e c u r i t y I n t e r c e p t o r . d o F i l t e r ( F i l t e r S e c u r i t y I n t e r c e p t o r . j a v a : 9 1 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . F i l t e r C h a i n P r o x y $ V i r t u a l F i l t e r C h a i n . d o F i l t e r ( F i l t e r C h a i n P r o x y . j a v a : 3 3 4 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . a c c e s s . E x c e p t i o n T r a n s l a t i o n F i l t e r . d o F i l t e r ( E x c e p t i o n T r a n s l a t i o n F i l t e r . j a v a : 1 1 9 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . F i l t e r C h a i n P r o x y $ V i r t u a l F i l t e r C h a i n . d o F i l t e r ( F i l t e r C h a i n P r o x y . j a v a : 3 3 4 ) < b r / > a t o r g . s p r i n g f r a m e w o r k . s e c u r i t y . w e b . s e s s i o n . S e s s i o n M a n a g ementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at com.ontius.core.web.security.jwt.filter.JwtAuthenticationFilter.doFilterInternal(JwtAuthenticationFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:357)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:270)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:860)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1598)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
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:292)
at com.sun.proxy.$Proxy116.merge(Unknown Source)
at com.ontius.core.persistence.repository.AbstractJpaRepository.merge(AbstractJpaRepository.java:34)
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 org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:359)
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:644)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:608)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
... 99 common frames omitted


Классы конфигурации

public class ApplicationInitializer extends AbstractMetricsApplicationInitializer {

@Override
protected Class[] getRootConfigClasses() {
return new Class[] { ApplicationConfiguration.class };
}

@Override
protected Class[] getServletConfigClasses() {
return null;
}

@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}

}


@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
"com.myapp.rest.controller",
"com.myapp.rest.mapper"
})
@Import({
PersistenceJpaConfiguration.class,
WebSecurityJwtConfiguration.class,
})
public class ApplicationConfiguration {

}



@Configuration
@EnableJpaRepositories(basePackages = "com.myapp.persistence.repository")
@ComponentScan(basePackages = "com.myapp.persistence")
@EnableTransactionManagement
public class PersistenceJpaConfiguration {

...

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) throws Exception {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(dataSource);
entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
entityManagerFactory.setPackagesToScan(this.persistencePropertyContainer.getPackagesToScan());
entityManagerFactory.setJpaProperties(this.persistencePropertyContainer.getJpaProperties());
return entityManagerFactory;
}

@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(this.persistencePropertyContainer.getDriverClassName());
dataSource.setUrl(this.persistencePropertyContainer.getDataSourceUrl());
dataSource.setUsername(this.persistencePropertyContainer.getDatabaseUsername());
dataSource.setPassword(this.persistencePropertyContainer.getDatabasePassword());
dataSource.setInitialSize(this.persistencePropertyContainer.getInitialSize());
dataSource.setMaxTotal(this.persistencePropertyContainer.getMaxTotal());
dataSource.setMaxIdle(this.persistencePropertyContainer.getMaxIdle());
return dataSource;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabase(this.persistencePropertyContainer.getDatabase());
jpaVendorAdapter.setGenerateDdl(this.persistencePropertyContainer.getGenerateDdl());
return jpaVendorAdapter;
}
}


@Configuration
@EnableWebSecurity(debug = true)
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, jsr250Enabled = true)
@EnableTransactionManagement
@ComponentScan(basePackages = {
"com.myapp.web.security.service",
"com.myapp.web.security.setup"
})

public class WebSecurityJwtConfiguration extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(this.authenticationProvider());
}

protected void configure(HttpSecurity http) throws Exception {
((HttpSecurity)((HttpSecurity)((HttpSecurity)http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()).cors().and()).httpBasic().realmName(this.securityPropertyContainer.getRealmName()).and()).csrf().disable();
}

@Bean
public AuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(this.userDetailsService);
authenticationProvider.setPasswordEncoder(this.passwordEncoder());
return authenticationProvider;
}

@Bean
public GrantedAuthorityDefaults grantedAuthorityDefaults() {
return new GrantedAuthorityDefaults(this.getRolePrefix());
}

@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderUtility.createPasswordEncoder();
}

protected String getRolePrefix() {
return "";
}

}



Контроллер

@RestController
@RequestMapping("/api/users")
@CrossOrigin
@Slf4j
public class UserController extends AbstractController {
...
@Autowired
private AnswerService answerService;
...

@RequestMapping(value = "/data", method = RequestMethod.POST)
@PreAuthorize("hasRole('ADMIN') or hasRole('USER')")
@ResponseStatus(HttpStatus.OK)
public AuthUserData saveAnswers(...) {
Answer answer = answerService.createOrUpdateAnswer(...);
...
}

}


Обслуживание

@Service
@Transactional
@Slf4j
public class AnswerServiceImpl extends AbstractService implements AnswerService {

...

@Override
@Transactional
public Answer createOrUpdateAnswer(Object answerValue, Long userId, Long answerSetId, Long questionGroupId, Long questionId) {
AnswerSet answerSet = (answerSetId != null) ? answerSetService.findById(answerSetId) : null;
QuestionGroup questionGroup = (questionGroupId != null) ? questionGroupService.findById(questionGroupId) : null;
Question question = (questionId != null) ? questionService.findById(questionId) : null;
return createOrUpdateAnswer(answerValue, userId, answerSet, questionGroup, question);
}

@Override
@Transactional
public Answer createOrUpdateAnswer(Object answerValue, long userId, AnswerSet answerSet, QuestionGroup questionGroup, Question question) {
User user = userService.findById(userId);

verifyObjectNotNull(user, User.class);
verifyObjectNotNull(question, Question.class);
verifyObjectNotNull(questionGroup, QuestionGroup.class);

if(answerSet == null) {
answerSet = new AnswerSet();
answerSet.setName(String.format("answer-set-%s-%s-%s", user.getId(), questionGroup.getName(), UUID.randomUUID().toString()));
answerSet.setQuestionGroup(getPersistedEntity(questionGroup, questionGroupService, QuestionGroup.class));
answerSet.setUser(user);
}

Answer answer = new Answer();
answer.setUser(user);
answer.setQuestion(getPersistedEntity(question, questionService, Question.class));
answer.setAnswerSet(getPersistedEntity(answerSet, answerSetService, AnswerSet.class));

Answer answerEntity = find(answer);

if(answerEntity != null) {
setAnswerValue(answerEntity, question, answerValue);
return createOrUpdate(answerEntity);
} else {
setAnswerValue(answer, question, answerValue);
return createOrUpdate(answer);
}
}

@Override
@Transactional
public Answer createEntity(Answer entity) {
if(isAnswerExisting(entity, false)) {
String message = "An answer with the given constraint already exists." + entity.getQuestion().getName();

log.error(message);
throw new EntityExistsException(message);
}

setAssociations(entity);

return getRepository().merge(entity); //

Подробнее здесь: https://stackoverflow.com/questions/607 ... ent-thread
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»