Screen controller, of a custom screen.
My application’s objective is to administrate Magazine’s issues.
That screen is used to upload a PDF file, then the PDF file is processed and I create a list of Page object. I just want to set that list on Issue object (passed as a parameter for the screen) and save the Issue (then the pages must be automatic saved). First I tried to use DataManager, but I got the detached difficult. Then I tried to create a service and call it, but I got the SecurityException. Now I’m recreating all from zero. What do you think that is the best approach for my case?
Thank you.
Invoking a service from a screen controller and working with data via EntityManager is absolutely normal. Your code is correct except that you don’t need to call persistence.getEntityManager().persist(issue); because changes in managed objects are saved on transaction commit. But it has nothing to do with the security exception.
Could you provide the whole stack trace?
2016-04-12 12:04:39.808 ERROR [Thread-38] com.haulmont.cuba.core.sys.ServiceInterceptor - Exception:
java.lang.SecurityException: No security context bound to the current thread
at com.haulmont.cuba.core.sys.ServiceInterceptor.getUserSession(ServiceInterceptor.java:90) ~[cuba-core-6.1.2.jar:6.1.2]
at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:70) ~[cuba-core-6.1.2.jar:6.1.2]
at sun.reflect.GeneratedMethodAccessor86.invoke(Unknown Source) ~[na:na]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:621) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:610) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:68) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) [spring-aop-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at com.sun.proxy.$Proxy148.updatePages(Unknown Source) [na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke(LocalServiceInvokerImpl.java:86) [cuba-core-6.1.2.jar:6.1.2]
at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(LocalServiceProxy.java:132) [cuba-web-6.1.2.jar:6.1.2]
at com.sun.proxy.$Proxy44.updatePages(Unknown Source) [na:na]
at net.gocoders.magazineappbackend.web.issue.IssueUploadPdf$1PDFWorker.run(IssueUploadPdf.java:165) [app-web-0.1-SNAPSHOT.jar:na]
The last line is interesting:
at net.gocoders.magazineappbackend.web.issue.IssueUploadPdf$1PDFWorker.run(IssueUploadPdf.java:165) [app-web-0.1-SNAPSHOT.jar:na]
I can guess that you invoke the service from a separate thread. If so, you should pass the security context to the new thread.
Use either background tasks (they set security context automatically) or pass it manually as explained here.
I’m trying to use CASCADE for delete Page’s when a Issue obj is deleted. I also want to use orphanRemoval. Nothing is working. Page’s are never removed.