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
Related
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
How can I configure persistence.xml for production? My question specifically concerns the definition of parameters such as user, password and bank url, for example.
I would like to not include this data directly in the file, but in an environment variable, for example.
However, it can be any other approach, except overwriting the file settings via the entity manager factory as was said in these questions 1 e 2
This restriction exists because my EntityManager is injected via #PersistenceContext. This even generated a comment
I'm using a JavaEE application with JAX-RS, Hibernate JPA and Spring MVC
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?
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
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.