Weird behaviour creating table in schema.sql vs via H2-console? - java

I am trying to execute the following SQL via schema.sql as my Spring Boot application starts:
create table tbl_user(
id int auto_increment, name varchar2(50), primary key(id)
);
When I try to run an insert statement against this with H2-console I get:
NULL not allowed for column "ID"; SQL statement:
insert into tbl_user(name) values ('Bob') [23502-200] 23502/23502
However, if I drop the table and run the same creation and insertion using H2-console the insert succeeds without an error.
2021-01-30 20:17:24.325 INFO 38184 --- [ main] o.hibernate.jpa.internal.util.LogHelper :
HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-30 20:17:24.380 INFO 38184 --- [ main] org.hibernate.Version :
HHH000412: Hibernate ORM core version 5.4.27.Final
2021-01-30 20:17:24.543 INFO 38184 --- [ main] o.hibernate.annotations.common.Version :
HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-01-30 20:17:24.646 INFO 38184 --- [ main] org.hibernate.dialect.Dialect :
HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-01-30 20:17:25.129 INFO 38184 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator :
HHH000490: Using JtaPlatform implementation:
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=h2user
spring.datasource.password=h2password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.h2.console.enabled=true

I suggest you adding spring.jpa.hibernate.ddl-auto=none to avoid H2 database auto-creation done by spring boot.
You can read the post at Spring boot with H2 Database auto create and the spring boot documentation at https://docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/#howto-database-initialization.

Related

Spring Data JDBC - Could not safely identify store assignment for repository candidate

I made a few changes to a commit, and all of a sudden, my spring program can't seem to initialize entities and link them to repositories. When I start up spring, I get this as my log:
2022-07-10 16:39:17.137 INFO 18040 --- [ restartedMain] com.vendify.BackendDbApplication : Starting BackendDbApplication using Java 17.0.2 on DESKTOP-HSDNP64 with PID 18040 (applicaiton started by (my name) in directory)
2022-07-10 16:39:17.140 INFO 18040 --- [ restartedMain] com.vendify.BackendDbApplication : No active profile set, falling back to default profiles: default
2022-07-10 16:39:17.186 INFO 18040 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2022-07-10 16:39:17.187 INFO 18040 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2022-07-10 16:39:18.193 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-07-10 16:39:18.194 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.
2022-07-10 16:39:18.240 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.Auth0TokensCacheRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.242 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ItemIndexRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.244 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ItemsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.246 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.LoggingRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.248 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.SerialsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.251 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.TransactionsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.253 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.ValidUsersRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.255 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.VendorsRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
2022-07-10 16:39:18.256 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 58 ms. Found 0 JDBC repository interfaces.
2022-07-10 16:39:18.266 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2022-07-10 16:39:18.267 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-07-10 16:39:18.329 INFO 18040 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 51 ms. Found 8 JPA repository interfaces.
2022-07-10 16:39:19.172 INFO 18040 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2022-07-10 16:39:19.184 INFO 18040 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-10 16:39:19.184 INFO 18040 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.52]
2022-07-10 16:39:19.275 INFO 18040 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-07-10 16:39:19.276 INFO 18040 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2089 ms
2022-07-10 16:39:19.754 INFO 18040 --- [ restartedMain] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.7.3 by Redgate
2022-07-10 16:39:19.760 INFO 18040 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-07-10 16:39:20.192 INFO 18040 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-07-10 16:39:20.240 INFO 18040 --- [ restartedMain] o.f.c.i.database.base.DatabaseType : Database: jdbc:mysql://localhost:3306/vendify (MySQL 8.0)
2022-07-10 16:39:20.276 INFO 18040 --- [ restartedMain] o.f.core.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.015s)
2022-07-10 16:39:20.284 INFO 18040 --- [ restartedMain] o.f.core.internal.command.DbMigrate : Current version of schema `vendify`: 1
2022-07-10 16:39:20.285 INFO 18040 --- [ restartedMain] o.f.core.internal.command.DbMigrate : Schema `vendify` is up to date. No migration necessary.
2022-07-10 16:39:20.502 INFO 18040 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-07-10 16:39:20.564 INFO 18040 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
2022-07-10 16:39:20.705 INFO 18040 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-07-10 16:39:20.866 INFO 18040 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2022-07-10 16:39:21.555 INFO 18040 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-07-10 16:39:21.563 INFO 18040 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-07-10 16:39:22.307 WARN 18040 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-07-10 16:39:22.779 INFO 18040 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter#6c38469b, org.springframework.security.web.context.SecurityContextPersistenceFilter#52d983cb, org.springframework.security.web.header.HeaderWriterFilter#67a31d2b, org.springframework.security.web.authentication.logout.LogoutFilter#2c8dea82, org.springframework.security.web.authentication.www.BasicAuthenticationFilter#631bab6, org.springframework.security.web.savedrequest.RequestCacheAwareFilter#1f96917a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter#64458fc6, org.springframework.security.web.authentication.AnonymousAuthenticationFilter#46d290b3, org.springframework.security.web.session.SessionManagementFilter#1312d3d0, org.springframework.security.web.access.ExceptionTranslationFilter#37e85798, org.springframework.security.web.access.intercept.FilterSecurityInterceptor#2cc95e93]
2022-07-10 16:39:23.986 INFO 18040 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2022-07-10 16:39:23.995 INFO 18040 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 14 endpoint(s) beneath base path '/****{hidden for security}'
2022-07-10 16:39:24.057 INFO 18040 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2022-07-10 16:39:24.073 INFO 18040 --- [ restartedMain] com.vendify.BackendDbApplication : Started BackendDbApplication in 7.398 seconds (JVM running for 7.938)
Mainly, the errors I am concerned about are like this in the log:
2022-07-10 16:39:18.240 INFO 18040 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data JDBC - Could not safely identify store assignment for repository candidate interface com.vendify.Repositories.Auth0TokensCacheRepository. If you want this repository to be a JDBC repository, consider annotating your entities with one of these annotations: org.springframework.data.relational.core.mapping.Table.
this is an example of how I code my entity classes (All of the are like this, and I have not touched them for quite a while. They seemed to have worked before...)
package com.vendify.Entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.lang.NonNull;
import lombok.Getter;
import lombok.Setter;
#Entity
#Table(name = "Logging")
#Getter
#Setter
public class LoggingEntity {
//specific code removed for security
}
with a similar repository class like this:
package com.vendify.Repositories;
import java.util.Date;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import com.vendify.Entities.Auth0TokensCacheEntity;
#RepositoryRestResource(exported = false)
public interface Auth0TokensCacheRepository extends CrudRepository<Auth0TokensCacheEntity, String> {
}
Just as proof, my MySQL database structure tables look a bit like this:
Spring seems able to connect to the database, and for the longest time, it had no issues... I've added a few packages, and it messed with some other stuff and it seems like that is when it broke, so for some context, here is how my program is generally laid out:
Help would be greatly appreciated! Thank you so much and good luck with your project fellow devs!
Update: I tried the solution of replacing CRUDRepository with JPARepository and the other annotations like Dhana suggested (Thanks :)), and spring found all those repositories. however, all my API endpoints now result in no response, even the actuator endpoints. Also when adding #ComponentScan(basePackageClasses = {my controller classes}) I get an application error that spring cannot find the service classes. Please note, as of now, when I call an endpoint the function still runs properly (It can system.out.print the result properly, but for some reason, cannot return it properly to the webserver to be displayed.
Description:
Field auth0TokensCacheService in com.vendify.Controllers.{ControllerClass} required a bean of type 'com.vendify.Services.{ControllerClass}' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.vendify.Services.{ControllerClass}' in your configuration.
My Service class looks like this:
#Service
public class HiddenService {
#Autowired
HiddenRepository hiddenRepository;
//methods in here
}
And my Controller looks like this:
#RestController
public class HiddenController {
#GetMapping("/endpointURL")
//method here
}
Thanks!
Let me explain, from my understanding, when you have multiple spring-data modules within a project alongside Spring Data JDBC, if you try to create an interface for a specific entity that isn't annotated with specific persistence annotations by extending the interface from common modules. This will definitely not work because spring basically has no idea with which data storage your entity is associated. So, try to extend it to a specific repository like, if you are using MySQL, go for JpaRepository instead of CrudRepository, anyhow internally at one point of the hierarchy they'll be inter-linked still associating your entity with specific repository is advised
finally, make sure you add these to your respective files:
#Repository inside your custom repo
Add this to your application.properties
spring.data.jdbc.repositories.enabled=false
Hope it helps you!:)
If you're getting errors for unrecognized packages then you can try structuring your project like this.

Permission denied in getting spanner session create in a java spring boot project using spanner emulator

I am trying to run a basic spring boot application with JPA by using spanner emulator. However, I am getting the below error after startup after hitting any exposed end point.
PERMISSION_DENIED: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
Project has been cloned from https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/tree/master/google-cloud-spanner-hibernate-samples/spring-data-jpa-sample
and the only change I have done is updated the application.properties file.
as per my understanding spanner emulator should not have any IAM related issues as it doesn't need any. I am not sure what is causing this issue here. I have verified that the emulator config is the active config and hence I would expect the code to connect to emulator.
# Application configuration to use Cloud Spanner with Spring Data JPA
# Spanner connection URL.
# - ${PROJECT_ID} Replace with your GCP project ID
# - ${INSTANCE_ID} Replace with your Spanner instance ID
# - ${DATABASE_NAME} Replace with your Spanner database name within your Spanner instance
spring.datasource.url=jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database
# Specify the Spanner JDBC driver.
spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver
# Specify the Spanner Hibernate dialect.
spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect
#spring.jpa.hibernate.ddl-auto=update //same error even if I uncomment this line
# Settings to enable batching statements for efficiency
spring.jpa.properties.hibernate.jdbc.batch_size=100
# You may display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
Please find the console logs below:
2021-10-06 12:59:12.549 INFO 9747 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-06 12:59:12.556 INFO 9747 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-06 12:59:12.556 INFO 9747 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.53]
2021-10-06 12:59:12.617 INFO 9747 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-06 12:59:12.617 INFO 9747 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 694 ms
2021-10-06 12:59:12.707 INFO 9747 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-10-06 12:59:12.737 WARN 9747 --- [ main] c.g.a.oauth2.DefaultCredentialsProvider : Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.
2021-10-06 12:59:14.558 INFO 9747 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (Network timeout is not supported)
2021-10-06 12:59:22.165 INFO 9747 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-10-06 12:59:22.197 INFO 9747 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-10-06 12:59:22.231 INFO 9747 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.29.Final
2021-10-06 12:59:22.337 INFO 9747 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-10-06 12:59:22.396 INFO 9747 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: com.google.cloud.spanner.hibernate.SpannerDialect
2021-10-06 12:59:22.851 INFO 9747 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-10-06 12:59:22.857 INFO 9747 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-10-06 12:59:23.060 WARN 9747 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-10-06 12:59:23.184 INFO 9747 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2021-10-06 12:59:23.290 INFO 9747 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-06 12:59:23.298 INFO 9747 --- [ main] com.example.CoffeeApplication : Started CoffeeApplication in 12.957 seconds (JVM running for 13.227)
2021-10-06 12:59:33.646 INFO 9747 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-06 12:59:33.646 INFO 9747 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-06 12:59:33.647 INFO 9747 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Hibernate:
select
customer0_.id as id1_1_,
customer0_.email as email2_1_,
customer0_.name as name3_1_
from
customer customer0_
2021-10-06 12:59:35.650 WARN 9747 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 7, SQLState: null
2021-10-06 12:59:35.650 ERROR 9747 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : PERMISSION_DENIED: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
2021-10-06 12:59:35.668 ERROR 9747 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
at io.grpc.Status.asRuntimeException(Status.java:535) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:533) ~[grpc-stub-1.40.1.jar:1.40.1]
at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40) ~[grpc-api-1.40.1.jar:1.40.1]
at com.google.cloud.spanner.spi.v1.SpannerErrorInterceptor$1$1.onClose(SpannerErrorInterceptor.java:100) ~[google-cloud-spanner-6.12.5.jar:6.12.5]
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:557) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:69) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:738) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:717) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133) ~[grpc-core-1.40.1.jar:1.40.1]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_281]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_281]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_281]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_281]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_281]
The error is a clear indication that your application is not trying to connect to the emulator, but to a real Spanner database. In order to connect to the emulator instead of 'real' Cloud Spanner, you should do one of the following:
Set the environment variable SPANNER_EMULATOR_HOST=localhost:9010
OR: Edit the JDBC connection URL to include the autoConfigEmulator=true property. Your JDBC URL would then become jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database;autoConfigEmulator=true
The advantage of the second alternative is that the autoConfigEmulator property will not only ensure that the JDBC driver is connecting to the emulator, it will also automatically create the test-instance and test-database on the emulator if they do not already exist.

EntityManagerFactory spring boot too slow

I'm having a problem with spring boot and hibernate.
To initialize is taking too long as you can see in the log (+20 minutes).
How can I have more information from log to know what the spring/hibernate is doing between those two steps?
Thank you
2020-03-23 15:27:16.199 INFO 6268 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-03-23 15:27:16.223 INFO 6268 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2020-03-23 16:06:19.187 INFO 6268 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Until now not solved.

Unable to connect my spring boot application to mysql

I have got this access denied exception :
java.sql.SQLException: Access denied for user ''#'localhost' (using
password: NO)
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to
obtain JDBC Connection; nested exception is java.sql.SQLException:
Access denied for user ''#'localhost' (using password: NO)
018-08-23 00:55:21.791 INFO 1260 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container
EntityManagerFactory for persistence unit 'default' 2018-08-23
00:55:21.813 INFO 1260 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [ name: default ...] 2018-08-23 00:55:22.180
INFO 1260 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.2.17.Final} 2018-08-23 00:55:22.181
INFO 1260 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-08-23 00:55:22.221
INFO 1260 --- [ main] o.hibernate.annotations.common.Version
: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-08-23
00:55:22.351 INFO 1260 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-08-23 00:55:23.445 ERROR 1260 --- [ main]
com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception
during pool initialization.
application properties file:
spring.thymeleaf.cache=false
#===============================
#DATA SOURCE
#===============================
#set here configurations for the database connection
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredatabase?verifyServerCertificate=false&useSSL=true
#username and secret
spring.datasource.data.username=root
spring.datasource.data.password=
#keep connection alive if idle for long time
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#===================
#jpa/hibernate
#===================
#use spring.jpa.properties.* for hibernate native properties
#stripped before adding them to the entity manager
#show or not log for each sql query
spring.jpa.show-sql=true
#jdbc driver class
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#the project
spring.jpa.hibernate.ddl-auto=create
#allows hibernate to generat sql optimised for particular dbms
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
my java file:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class BookstoreApplication {
public static void main(String[] args) {
SpringApplication.run(BookstoreApplication.class, args);
}
}
It seems that your database username and password are not set with correct property names, as in spring-boot the name of the properties are predefined and only those should be used while giving configuration information. Just try below property keys for username and password :
spring.datasource.username =
spring.datasource.password =
Provide appropriate values..
java.sql.SQLException: Access denied for user ''#'localhost' (using password: NO)
It seems your user password is incorrect. Try to login MySQL with same credentials and it will display the same error.

spring.jpa.hibernate.ddl-auto=create is not working in Hibernate 5 with SpringBoot 2.0

I created a SpringBoot 1.5.11.RELEASE and Hibernate 5 project. Everything was working fine.
However, when I changed SpringBoot version to 2.0.0.RELEASE the spring.jpa.hibernate.ddl-auto=create does not work. I have tried this stackoverflow post, but no luck.
Here is the stack trace of SpringBoot 1.5.11.RELEASE :
2018-04-06 16:27:52.512 INFO 6792 --- [ main]
j.LocalContainerEntityManagerFactoryBean : Building JPA container
EntityManagerFactory for persistence unit 'default' 2018-04-06
16:27:52.526 INFO 6792 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [ name: default ...] 2018-04-06 16:27:52.587
INFO 6792 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.0.12.Final} 2018-04-06 16:27:52.589
INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-04-06 16:27:52.591
INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000021: Bytecode provider name : javassist 2018-04-06
16:27:52.735 INFO 6792 --- [ main]
o.hibernate.annotations.common.Version : HCANN000001: Hibernate
Commons Annotations {5.0.1.Final} 2018-04-06 16:27:52.833 INFO 6792
--- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-04-06 16:27:53.180 INFO 6792 --- [ main]
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl
schema export Hibernate: drop table if exists person Hibernate: create
table person (id integer not null auto_increment, name varchar(255),
primary key (id)) 2018-04-06 16:27:53.619 INFO 6792 --- [
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema
export complete 2018-04-06 16:27:53.646 INFO 6792 --- [
main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA
EntityManagerFactory for persistence unit 'default' SessionFactory
Created 2018-04-06 16:27:54.528 WARN 6792 --- [ main]
org.hibernate.orm.connections : HHH10001002: Using
Hibernate built-in connection pool (not for production use!)
2018-04-06 16:27:54.529 INFO 6792 --- [ main]
org.hibernate.orm.connections : HHH10001005: using driver
[com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel]
2018-04-06 16:27:54.529 INFO 6792 --- [ main]
org.hibernate.orm.connections : HHH10001001: Connection
properties: {user=root, password=****} 2018-04-06 16:27:54.529 INFO
6792 --- [ main] org.hibernate.orm.connections :
HHH10001003: Autocommit mode: false 2018-04-06 16:27:54.532 INFO 6792
--- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1) 2018-04-06
16:27:54.539 INFO 6792 --- [ main]
org.hibernate.dialect.Dialect : HHH000400: Using dialect:
org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:54.855 INFO 6792
--- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup Hibernate: insert into
person (name) values (?) Saved!! 2018-04-06 16:27:54.954 INFO 6792
--- [ main] com.hib.Hibernate5Application : Started Hibernate5Application in 4.051 seconds (JVM running for 4.802)
Here is the stack trace of SpringBoot 2.0.0.RELEASE :
SessionFactory Created 2018-04-06 16:24:17.011 INFO 5092 --- [
main] org.hibernate.Version : HHH000412: Hibernate
Core {5.2.14.Final} 2018-04-06 16:24:17.013 INFO 5092 --- [
main] org.hibernate.cfg.Environment : HHH000206:
hibernate.properties not found 2018-04-06 16:24:17.915 INFO 5092 ---
[ main] o.hibernate.annotations.common.Version :
HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-04-06
16:24:17.989 WARN 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001002: Using
Hibernate built-in connection pool (not for production use!)
2018-04-06 16:24:17.994 INFO 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001005: using driver
[com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel]
2018-04-06 16:24:17.994 INFO 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001001: Connection
properties: {user=root, password=****} 2018-04-06 16:24:17.994 INFO
5092 --- [ main] org.hibernate.orm.connections.pooling :
HHH10001003: Autocommit mode: false 2018-04-06 16:24:17.996 INFO 5092
--- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1)
2018-04-06 16:24:18.230 INFO 5092 --- [ main]
org.hibernate.dialect.Dialect : HHH000400: Using dialect:
org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:24:19.041 INFO 5092
--- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2018-04-06 16:24:19.058 INFO 5092 --- [
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start
completed.
2018-04-06 16:24:19.419 INFO 5092 --- [ main]
o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX
exposure on startup 2018-04-06 16:24:19.420 INFO 5092 --- [
main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name
'dataSource' has been autodetected for JMX exposure 2018-04-06
16:24:19.427 INFO 5092 --- [ main]
o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource':
registering with JMX server as MBean
[com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-04-06
16:24:19.441 INFO 5092 --- [ main]
com.hib.Hibernate5Application : Started
Hibernate5Application in 3.742 seconds (JVM running for 4.541)
Hibernate: insert into person (name) values (?) Saved!!
Hibernate 5 configuration :
#Configuration
public class HinernateConfig {
private StandardServiceRegistry standardServiceRegistry;
private SessionFactory sessionFactory;
#Bean
public SessionFactory sessionFactory() {
try {
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
} catch (Exception e) {
System.out.println("Exception occured while creating SessionFactory");
e.printStackTrace();
if (standardServiceRegistry != null) {
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
return sessionFactory;
}
#PostConstruct
public void post() {
System.out.println("SessionFactory Created");
}
}
Main application :
#SpringBootApplication
public class Application implements CommandLineRunner {
private SessionFactory sessionFactory;
public Application(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String... args) throws Exception {
Session session = sessionFactory.openSession();
session.beginTransaction();
Person person = new Person("Mehraj Malik");
session.save(person);
System.out.println("Saved!!");
session.getTransaction().commit();
session.close();
}
}
Hibernate Xml config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hotel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping class="com.hib.entity.Person"/>
</session-factory>
</hibernate-configuration>
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/Hotel
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
In Boot 2.0 stack trace the below part does not appear :
Running hbm2ddl schema export Hibernate: drop table if exists person
Hibernate: create table person (id integer not null auto_increment,
name varchar(255), primary key (id))
Github : https://github.com/Mehraj123/Hib5Boot2.0
As mentioned by M.Deinum I've deleted the following classes and resource file and it works :
HinernateConfig
hibernate.cfg.xml
And changes in Main application
#SpringBootApplication
public class Hibernate5Application implements CommandLineRunner {
private EntityManagerFactory managerFactory;
public Hibernate5Application(EntityManagerFactory managerFactory) {
this.managerFactory = managerFactory;
}
public static void main(String[] args) {
SpringApplication.run(Hibernate5Application.class, args);
}
#Override
#Transactional
public void run(String... args) throws Exception {
EntityManager entityManager = managerFactory.createEntityManager();
entityManager.getTransaction().begin();
Person person = new Person("Mehraj Malik");
entityManager.persist(person);
System.out.println("Saved!!");
entityManager.close();
}
}
I've updated my github repo

Categories

Resources