Hi
I wanted to apply a custom @Converter
to all entity attributes of type Boolean
, so the first thing I tried was a converter with this signature:
@Converter(autoApply = true)
public class BooleanToIntegerConverter implements AttributeConverter<Boolean, Integer> {
...
}
and I added it to the persistence-unit
.
As you know autoApply
comes in handy when you want all your attributes magically pick the same converter, without the error prone task to add the @Convert
annotation to EVERY attribute of that type…
But when I tried this in a CUBA project, the application failed at startup with the following exception:
2018-12-15 00:06:05.346 DEBUG [localhost-startStop-1] com.haulmont.cuba.security.auth.AnonymousSessionHolder - Initialize anonymous session
2018-12-15 00:06:05.908 INFO [localhost-startStop-1/app-core/server] com.haulmont.cuba.core.app.ConfigStorage - Loading DB-stored app properties cache
2018-12-15 00:06:06.507 DEBUG [localhost-startStop-1/app-core/server] com.haulmont.cuba.security.auth.providers.AbstractAuthenticationProvider - Unable to find user: anonymous
2018-12-15 00:06:06.518 ERROR [localhost-startStop-1] com.haulmont.cuba.core.sys.AbstractWebAppContextLoader - Error initializing application
java.lang.RuntimeException: Unable to create anonymous session. It is required for system to start
at com.haulmont.cuba.security.auth.AnonymousSessionHolder.initializeAnonymousSession(AnonymousSessionHolder.java:77) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.security.auth.AnonymousSessionHolder.applicationStarted(AnonymousSessionHolder.java:45) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.core.sys.AppContext.startContext(AppContext.java:239) ~[cuba-global-6.10.5.jar:6.10.5]
at com.haulmont.cuba.core.sys.AppContext$Internals.startContext(AppContext.java:302) ~[cuba-global-6.10.5.jar:6.10.5]
at com.haulmont.cuba.core.sys.AbstractWebAppContextLoader.contextInitialized(AbstractWebAppContextLoader.java:85) ~[cuba-global-6.10.5.jar:6.10.5]
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4792) [catalina.jar:8.5.33]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5256) [catalina.jar:8.5.33]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.5.33]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754) [catalina.jar:8.5.33]
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730) [catalina.jar:8.5.33]
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) [catalina.jar:8.5.33]
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1140) [catalina.jar:8.5.33]
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1875) [catalina.jar:8.5.33]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_181]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: com.haulmont.cuba.security.global.LoginException: Login o password errati 'anonymous'
at com.haulmont.cuba.security.auth.providers.AnonymousAuthenticationProvider.authenticate(AnonymousAuthenticationProvider.java:63) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.security.auth.AuthenticationManagerBean.authenticateInternal(AuthenticationManagerBean.java:253) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.security.auth.AuthenticationManagerBean.login(AuthenticationManagerBean.java:118) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.security.auth.AnonymousSessionHolder.loginAnonymous(AnonymousSessionHolder.java:82) ~[cuba-core-6.10.5.jar:6.10.5]
at com.haulmont.cuba.security.auth.AnonymousSessionHolder.initializeAnonymousSession(AnonymousSessionHolder.java:72) ~[cuba-core-6.10.5.jar:6.10.5]
... 17 common frames omitted
If I simply remove autoApply = true
from the @Convert
annotation, the error disappears and the project starts just fine. The problem is that now I need to scan every Boolean
attribute in my entities, and put @Convert
on each of them: not a funny task
Is it a known issue, or do you know some workaround to let me use autoApply = true
?
UPDATE:
ok, it seems that the converter is now applied to all Boolean
attributes, even outside MY persistence-unit
, so it gets picked up even for the cuba
persistence unit.
And for that reason now queries on system entities (like User
) that rely on Boolean
attributes are failing, because my converter kicks in.
I was convinced that an autoApply
converter works in isolation inside the persistence-unit where it is registered, but it seems I was wrong…
So the real question now is: Do you know if I can isolate the converter only to my own persistence-unit, keeping it with autoApply=true ?
Thx
Paolo