JPA ddl-auto=create/update with schema= mentioned in Entity not working with spring boot 2.1 - spring-boot

I have a spring boot application, with following User entity class:
#Entity
#Table(name="user")
public class User {
...
and I'm using ddl-auto=update (or create) to auto-generate the schema in the database. The database used is H2 (also tried with HSQL).
Now all works well (the required table is automatically generated when application is launched), until the entity class is changed to the following (added schema=):
#Entity
#Table(name="user", schema="myschm")
public class User {
...
Now it gives the error when creating the table: Schema "MYSCHM" not found. It seems JPA is expecting the schema MYSCHM to be present and not creating it automatically.
I started observing this issue after using Spring Boot 2.1.5. This used to work when I was using Spring Boot 1.5.3.
Is there any change made in Spring Boot 2+ that affects this? Is there any configuration change I need to do to make this work?
Thanks

The schema is not auto created by H2.
You have to add:
jdbc:h2:mem:test;INIT=CREATE SCHEMA IF NOT EXISTS MYSCHM

Related

How to use multiple JPA implementations in a Spring Boot project?

Is it possible to use multiple JPA implementations in a Spring Boot project ? For example Hibernate for one data source/database and Eclipse Link for another data source/database. If it is possible then how can we use it.
Using Hibernate I successfully handle multiple data source in a single spring boot project. But I don't know how to use multiple JPA in a project .
Yes It is possible you can implements as many times you want jpa interface
You need to create different repository for each entity and implements the JpaInterface in each repository
for example-:
#Repository
public interface CommentRepository extends JpaRepository<Comment,Long> {
}
NOw to use with different class just replace
1- Comment as that class Name
2- and Long as that class id datatype

Spring boot Jpa is not working with Spring batch and spring integration

I am working with spring batch. I needed to add some jpa repositories. So previously i was using JDBCTemplate which was working fine.
But when I started working with JPA, the spring boot application could not find the repos. Which were there.
#Autowired
ClassLevelConfigRepo clcr;
I checked these things as the best practices.
Added #EnableJpaRepositories in springBoot application class.
Added #Repostiories to the repository interfaces.
extended the interfaces with JpaRepository<Account, String>
Added #Entity to the entity classes and defined the #Table and # Column annotations properly.
But I am still getting below error.
Field clcr in com.cloudtask.batchconfig.util.LhmUtility required a bean of type 'com.cloudtask.batchconfig.repo.ClassLevelConfigRepo' that could not be found.
I tried checking all the dependencies in pom.xml it was as per recommended. And I have all the tables defined properly in data base.
I was expecting the application to return the Autowired clcr object propely.
Edit 1 : spring boot application annotations
#SpringBootApplication
#ComponentScan({"com.cloudtask"})
#EnableAsync
#IntegrationComponentScan({"com.cloudtask"})
#EnableIntegrationManagement(defaultLoggingEnabled = "true")
#EnableJpaRepositories
#EntityScan
public class imclassApplication ```
When you work with Spring Data Jpa with those basic points you should also keep track of below points.
you have added spring-boot-starter-data-jpa in your pom.xml
you have added the entity and repo package below one level of the application package.
If you the package is at same level you should specify the exact package details in the annotation. in your case it should be like :
#EnableJpaRepositories("com.cloudtask.batchconfig.repo")
#EntityScan(basePackages = {"com.cloudtask.batchconfig.entity"})
Happy programming!

How to configure Spring Boot to not connect to database

Please note: this question is not a dupe, and I even reference the other similar question below. I assert that my situation is categorically different than the other question referenced.
I am standing up a Spring Boot web service that is not backed by any database, JDBC or RDBMS data source.
At startup I get and error:
Cannot determine embedded database driver class for database type NONE
Here on SO I see a very similar question here where the accepted answer states:
"You haven't provided Spring Boot with enough information to auto-configure a DataSource"
...and goes on to explain how to set the appropriate values in the app config file:
spring.datasource.url = ...
spring.datasource.driver-class-name = ...
But what if I don't want any data sources?! In the other question, the user was connecting to NoSQL via DataNucleus. In my case, I'm not interested (at least at the present moment) in connecting to any type of data source (all data for this service will come from other cloud-based REST services).
What's the fix here?
According to the documentation, you can turn this behavior off by excluding DataSourceAutoConfiguration from the auto-configuration that Spring Boot does for you.
#Configuration
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
// ...
}
Update To Answer Questions: By using #SpringBootApplication, it automatically pulls in #EnableAutoConfiguration, and if it sees the jdbc jars on the classpath, will add/execute DataSourceAutoConfiguration. The code above turns off this behavior.
If you are using #SpringBootApplication decorator you can exclude by using the DataSourceAutoConfiguration as done in the #EnableAutoConfiguration decorator as well. Documentation ref
#Configuration
#SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class MyApplication {
// ...
}

Spring boot property injection to JPA entity definitions

Is it possible to use spring boot property placeholder notations in JPA entity defitinions ?
For instance following is not working :
#Entity
#Table(schema=#Value("schemaname") , name="tablename")
public class InterfaceModel {
Is there a way to make this work ?
No there isn't. Check #829 in the Spring Boot issue tracker for more details.

Using #ConfigurationProperties in Spring Boot Application doesn't work

I am using Spring Boot V 1.4.1 for a new application.
My app requires two JDBC data sources and I was following the example at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources how to set it up.
My Spring beans configuration class is annotated with #EnableConfigurationProperties and my first bean is defined as
#Primary
#Bean
#ConfigurationProperties(prefix = "first.database")
DataSource qivsDB() {
return DataSourceBuilder.create().build();
}
, the second one accordingly. My application.properties file has properties defined like
first.database.url=jdbc:[redacted]
first.database.username=[redacted]
first.database.password=[redacted]
For reasons I not transparent to me during debugging this is failing to initialize: Cannot determine embedded database driver class for database type NONE - debug showed me that the builder does not have any properties set when calling build().
What did I miss here?
Before you do all the debugging part, you should have a look to the auto-configuration report. If you define your own DataSource there's no reason for Spring Boot to start looking at what it can do for your app. So, for some reasons, that definition of yours is not applied in your app and the default in Spring Boot still applies, doesn't find any JDBC url in the default namespace and attempt to start an embedded database. You should see in the auto-config report that the DataSourceAutoConfiguration still matches.
I am not sure the public keyword has anything to do with it, though you won't get custom meta-data for that key since we only scan for public methods.

Resources