Spring 6 PostGIS integration - java

This question was migrated from Geographic Information Systems Stack Exchange because it can be answered on Stack Overflow.
Migrated 14 days ago.
I have been trying to port an existing application using PostGIS from Spring 5 to Spring 6.
I get an error saying that org.hibernate.spatial.dialect.postgis.PostgisDialect is not more available:
Task :packages:discover-app-back-api:bootRun FAILED
Caused by: org.hibernate.service.spi.ServiceException: Unable to create requested service org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [org.hibernate.spatial.dialect.postgis.PostgisDialect] as strategy [org.hibernate.dialect.Dialect]
Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.spatial.dialect.postgis.PostgisDialect]
Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.spatial.dialect.postgis.PostgisDialect
Spring boot 3.0.2
Spring security 6.0.1
Hibernate 6.1.6
Indeed, this class is no more available in the version 6.X.
What configuration should I use?

You no longer need to specify the dialect:
The PostgisDialect class was first replaced by PostgisPG9xDialect / PostgisPG10Dialect classes, but now these are also deprecated ("A SpatialDialect is no longer required.").
So the correct dialect is the default (org.hibernate.dialect.PostgreSQLDialect).
But since it is the default it shouldn't be necessary to specify it in your Spring-Boot application, both the default driver and dialect can be derived from what you specify in spring.datasource.url

Related

Unable to define properties which start with keycloak

Unable to define properties which start with keycloak. keycloak-spring-boot-starter starter jar fails to load the properties if it observes keycloak properties which aren't known to the jar. The issue gets resolved if we prefix "sso" to the properties, is there a way to avoid this error even when keeping the keycloak prefixes.
keycloak.admin.username=
keycloak.admin.password=
sso.keycloak.admin.username=
sso.keycloak.admin.password=
this is the error seen with Spring Boot 2.5.0 and Keycloak 13.0.0
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 21 more
Caused by: java.lang.NullPointerException
at org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilterAtOffsetOf(HttpSecurity.java:2654)
at org.springframework.security.config.annotation.web.builders.HttpSecurity.addFilterAfter(HttpSecurity.java:2645)
at org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter.configure(KeycloakWebSecurityConfigurerAdapter.java:123)
at com.foo.config.KeycloakSecurityConfig.configure(KeycloakSecurityConfig.java:36)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:217)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:315)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:93)
at com.foo.config.KeycloakSecurityConfig$$EnhancerBySpringCGLIB$$501f46fb.init(<generated>)
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:338)
at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:300)
at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38)
at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:127)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
This error was introduced in version 5.5.0 of Spring Security which comes with Spring Boot 2.5.0.
This is happening because KeyCloak adds two filters successively after another, like this:
#Override
protected void configure(HttpSecurity http) throws Exception {
http
...
.addFilterAfter(keycloakSecurityContextRequestFilter(), SecurityContextHolderAwareRequestFilter.class)
.addFilterAfter(keycloakAuthenticatedActionsRequestFilter(), KeycloakSecurityContextRequestFilter.class)
...
}
And there's a bug right now in which the custom filter order is not being persisted, thus resulting in a NullPointerException when trying to add a filter relative to another custom filter recently added.
What I advise you to do right now is to use a lower version of Spring Boot, like 2.4.x until the issue in GitHub gets resolved.
From the Keycloak code it seems they have hardwired the properties to specific properties only.
From KeycloakSpringBootProperties
/* this is a dummy property to avoid re-rebinding problem with
property keycloak.config.resolver
when using spring cloud - see KEYCLOAK-2977 */
From their code, its seems we can make use of a workaround they have to include additional properties.
But to do that we have to add 'config' in it.
keycloak.config.admin.username=asd
keycloak.config.admin.password=sss

Overriding Hibernate ConnectionProvider in Spring Boot 2.1

I am upgrading a kotlin Spring Boot 1.x application to use Spring Boot 2.1.6
Previously I used EntityManagerFactoryBeanCallback to set the hibernate.connection.provider_class to our custom implementation of ConnectionProvider. This was deprecated and removed in spring boot 2.0 and 2.1 respectively.
I am now attempting to use a HibernatePropertiesCustomizer
#Bean
fun hibernatePropertiesCustomizer() = HibernatePropertiesCustomizer {
it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name
}
The application throws an error now:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by:
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by:
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
If I comment out the it["hibernate.connection.provider_class"] = ScopedConnectionProvider::class.java.name line everything works except obviously the custom ConnectionProvider
For reference I am using the ScopedConnectionProvider to enable our multitenancy by setting a variable on each connection representing the customers scope ID. Any alternate solution that allows me to run a SET #scope_id = ? query on every connection would also be fine.
Relevant version information:
Spring Boot: 2.1.6
Spring: 5.1.8
Hibernate: 5.3.10
Kotlin: 1.3.21
It turns out that the Datasource was not being injected into my ScopedConnectionProvider correctly anymore. I changed it so that I was inheriting from DatasourceConnectionProviderImpl instead of implementing my own ConnectionProvider from scratch. This seems to have removed the need to specify the dialect.

ConnectorRuntimeException: Invalid resource

I am creating an enterprise java application using netbeans 8.0.1 java EE6 and glassfish server 4.1.1 attached with netbeans.
When I have created the db in mysql and connected successfully and created entity class from database and session bean for that entity class and I am going to deploy the application it gives this error:
Caused by: com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : java:module/custordermgt__pm
at org.glassfish.jdbcruntime.service.JdbcDataSource.validateResource(JdbcDataSource.java:81)
at org.glassfish.jdbcruntime.service.JdbcDataSource.setResourceInfo(JdbcDataSource.java:62)
at org.glassfish.jdbcruntime.JdbcRuntimeExtension.lookupDataSourceInDAS(JdbcRuntimeExtension.java:136)
at com.sun.enterprise.connectors.ConnectorRuntime.lookupDataSourceInDAS(ConnectorRuntime.java:589)
What caused this exception?
This is probably due to a NetBeans bug. Take a look at the solution here: https://stackoverflow.com/a/56921913/1429984

Tomee - Hibernate Invalid and non-convertable constructor parameter

im trying to deploy my application in TOMEE with Hibernate 4, I already copz all the hibernate libraries in /lib but im getting the next error.
Is there any special configuration to make hibernate work with TomEEE?,
Thanks in advance
Caused by: java.lang.ClassCastException: org.apache.openejb.hibernate.OpenEJBJtaPlatform cannot be cast to org.hibernate.service.jta.platform.spi.JtaPlatform

Cassandra Cannot locate storage-conf.xml

I was trying to start Cassandra on FreeBSD with /usr/local/share/cassandra/bin/cassandra.
But it gives me the following error messages, any idea how to get around the problem? Any suggestions will be GREATLY appreciated. Thank you.
INFO 21:16:38,666 JNA not found. Native methods will be disabled.
ERROR 21:16:38,684 Exception encountered during startup.
java.lang.ExceptionInInitializerError
at org.apache.cassandra.thrift.CassandraDaemon.setup(CassandraDaemon.java:73)
at org.apache.cassandra.thrift.CassandraDaemon.main(CassandraDaemon.java:224)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Cannot locate storage-conf.xml via storage-config system property or classpath lookup.
at org.apache.cassandra.config.DatabaseDescriptor.(DatabaseDescriptor.java:584)
... 2 more
Caused by: java.lang.RuntimeException: Cannot locate storage-conf.xml via storage-config system property or classpath lookup.
at org.apache.cassandra.config.DatabaseDescriptor.getStorageConfigPath(DatabaseDescriptor.java:180)
at org.apache.cassandra.config.DatabaseDescriptor.(DatabaseDescriptor.java:187)
... 2 more
Exception encountered during startup.
java.lang.ExceptionInInitializerError
at org.apache.cassandra.thrift.CassandraDaemon.setup(CassandraDaemon.java:73)
at org.apache.cassandra.thrift.CassandraDaemon.main(CassandraDaemon.java:224)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Cannot locate storage-conf.xml via storage-config system property or classpath lookup.
at org.apache.cassandra.config.DatabaseDescriptor.(DatabaseDescriptor.java:584)
... 2 more
Caused by: java.lang.RuntimeException: Cannot locate storage-conf.xml via storage-config system property or classpath lookup.
at org.apache.cassandra.config.DatabaseDescriptor.getStorageConfigPath(DatabaseDescriptor.java:180)
at org.apache.cassandra.config.DatabaseDescriptor.(DatabaseDescriptor.java:187)
... 2 more
Is this cassandra 0.6.x? You probably want to upgrade to the latest cassandra, 0.7.4
The error is because Cassandra can't locate storage-conf.xml. This file should be in your cassandra conf directory, see http://wiki.apache.org/cassandra/StorageConfiguration
If you upgrade to cassandra 0.7.x, you will need a cassandra.yaml file instead.

Categories