Spring boot and flyway integration - init order - java

I have an EntityManager (and a DataSource) created by spring boot and i want to add flyway. but EntityManager must be created after flyway and flyway after the DataSource. do i have to create whole EntityManager manually only to add 'depends-on' or is there some simpler way?

Boot doesn't create a Hibernate SessionFactory so probably you mean a JPA EntityManager. If you create your own Flyway it will always be initialized before the Boot autoconfig beans. Is that not what you see? Maybe you need to share a project.

The problem is in the usage of spring.jpa.hibernate.ddl-auto=validate (Hibernate wants to validate the DataSource before the migrations are executed, see here). A simple workaround is to switch to spring.jpa.hibernate.ddl-auto=none.

Related

Multi-tenency with Spring JPA

We are building a multi-tenant application using spring boot, JPA (Eclipselink). We have done below steps.
Defined AbstractRoutingDatasource
Defined new Bean of LocalContainerEntityManagerFactoryBean for each tenant
Now in our code, we have two types of flavour.
#Persistencecontext
Both do not have a qualifier. Problem is we can not add qualifier example persistence unit name as my tenants will be dynamic.
Is there any way we can solve it. Means on runtime, it will attach the right entity manager with out hardcoding unitname in the persitenceContext
Thanks a lot

Spring Boot Datasource Setup

In Spring Boot - is there anyway to only connect to the database when it is required the first time?
For example - lazy load the database setup?
I understand this is not the usual pattern but would interested in hearing if there is a solution to this
Thanks
Damien
n Spring Boot - is there anyway to only connect to the database when it is required the first time? For example - lazy load the database setup?
Spring Data and Hibernate can do that setup.
I wonder if you can use #Configuration and #Lazy at the same time, and the documentation suggests it is doable but it will create all the bean lazily.
Whereas, if you want to selectively create datasource bean lazily among other beans, then, in that case you need to that use #Lazy on datasource bean
#Configuration
#Lazy
public class YourDataSourceConfigClass {//datasource bean}

How to ignore not existing bean if some other bean depends on it?

I use Flyway in my application and it is configured as Spring bean, which performs migration as init-method(example configuration can be found here in the bottom). Of course migration should be performed before any app's interaction with database.
That's why my datasource bean depend on Flyway bean. But i create Flyway bean conditionally using #Profile, because i do not want to create it using the same app's context in integration tests.
The problem is that when i use test profile which does not create Flyway bean, datasource instantiation fails with NoSuchBeanDefinitionException, because it depends-on="flyway" which is not created with current profile. Can i somehow make spring to ignore this depends-on dependency, if related bean does not exist? If no, what is the way to decouple Flyway bean from context when i use it under test profile?

Add Hibernate to SpringBoot project

I have a simple Spring Boot project (already mentioned here: Replace hsqldb with MySQL)
I would like to configure Hibernate to work with this project. In another project I used to get EntityManager like so:
#PersistenceContext(unitName = "orm-unit")
private EntityManager em;
but there I also have persistence.xml with required configuration.
In Spring Boot I don't even know where to place any configuration files.
How to make Hibernate work in this case?
Read the Spring Boot documentation. Looking over 31. Working with SQL databases you will see that you need to configure a DataSource.
DataSource configuration is controlled by external configuration
properties in spring.datasource.*. For example, you might declare the
following section in application.properties:
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
You can also configure a datasource in a #Configuration mapped class which implements EnvironmentAware.
JHipster generates a cool database configuration using HikariCP. You can check it out the sample here.
For Hibernate you can configure JPA properties.
You can set spring.jpa.hibernate.ddl-auto explicitly and the standard Hibernate property values are none, validate, update, create, create-drop. Spring Boot chooses a default value for you based on whether it thinks your database is embedded (default create-drop) or not (default none).
For example to create and drop tables you can add the following to your application.properties.
spring.jpa.hibernate.ddl-auto=create-drop
As for EntityManager when you EnableAutoConfiguration you will trigger a JpaBaseConfiguration which will create an entity manager for you.
You can also use a custom EntityManagerFactory.
To take full control of the configuration of the EntityManagerFactory,
you need to add a #Bean named ‘entityManagerFactory’. Spring Boot
auto-configuration switches off its entity manager based on the
presence of a bean of that type.
And btw you can also use a traditional persistence.xml

Access EntityManagerFactory in grails

How can I access EntityManagerFactory in a grails web application or any other persistence unit?
I could access it with name by below code if a had a persistence.xml config but grails hides most of configurations.
EntityManagerFactory emf = Persistence
.createEntityManagerFactory("my.entity.manager.name");
What I have tried so far:
1. automatic DI does not work with def entityManagerFactory.
2. I listed all the beans from ctx in grails shell but nothing like entity manager was found.
OR is their a way I can create persistence.xml or somehow override default persistence configuration.
Please help, thanks in advance.
Do you really need an EntityManager?
Grails/Gorm accesses hibernate through Session and SessionFactory instead of using the JPA specification.
You can inject the sessionFactory bean in your grails code using
def sessionFactory

Categories