I am new to Spring and hibernate. I am struggling to do my job with fast self learning but without luck. I would be very grateful for your patience and help.
My project involves data collecting(collect data and put it into database) and presenting(json REST-ful api).
My plan is to persist collected data with hibernate persistence API:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("hello");
EntityManager entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(this);
entityManager.getTransaction().commit();
For this to work, I have to write a persistence.xml to specify the data source.
For presentation part, I have to specify data source in spring's application.properties file.
Can I specify data source in just one place for this use case? For example, maybe I can use persistence.xml for all at once.
And preferably, I'd like to put data source outside of the jar file.
EDIT:
I end up with a single application.properties file, which defines both hibernate data settings and spring hibernate database settings:
spring.datasource.name=gateway
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/gateway
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
hibernate.connection.username=root
hibernate.connection.password=123456
hibernate.connection.url=jdbc:mysql://127.0.0.1:3306/gateway
Related
How i could define some schema and data to be inserted into db for
sql database in spring boot
Also could i do this for embedded databases
For example i am using two databases and i want to populate some data or define some schema and apply to different databases before application starts.
A file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop). This can be useful for demos and for testing if you are careful but is probably not something you want to be on the classpath in production. It is a Hibernate feature (and has nothing to do with Spring).
You can take a look in spring docs
I am trying to create Repository with L2 cache with minimal setting. My database is postgresql.
I start with spring-boot-sample-data-jpa-archetype project using maven.
I have removed the HSQL and create a DataSource bean to connect to postgresql.
Also use ddl to create schema and imported the initial script data.
I have also added the #Cacheable to my entities.
Then I use unit test to query an entity 10 times using repository. It took 1~49ms. So that leaves me two questions.
Does repository benefits from L1 cache? how do I know if I am hitting the cache or data source?
How to enable L2 cache? does Spring data has its own implementation?
After some testing and the help from #dunni, I will answer my own question.
Spring uses Hibernate as an implementation of JPA. Spring data provides some wrapper over Hibernate. Also need to choose a caching implementation.
To enable L2 cache, add these properties to your project.
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
also add these dependencies:
hibernate-ehcache
Then, add #Cacheable(true) to your entity model class.
Extends a repository interface, Spring will generate the implementation using naming convention. For example
#QueryHints({#QueryHint(name="org.hibernate.cacheable", value="true")})
Entity findByName(String name)
You can also implement the interface. But that will require adding a hint to your query object to activate L2 cache.
query.setHint("org.hibernate.cacheable", true);
To verify the cache is working, you can use the following properties to see if the SQL has been executed.
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
I am familiar with using Hibernate and the hibernate.cfg.xml file to create a Session.
However, I have a persistence.xml file within my project, not the hibernate.cfg.xml file.
How do I use it to create a Session that I can then use to query my database?
If you want to use persistance.xml you should use EntityManager. How to use EntityManager you can find in Hibernate, JPA – Part 1 tutorial.
The key config file for JPA is persistence.xml. This lives in the
META-INF directory. It details what the persistence driver to use
and what JNDI data source to connect to. Additional properties can
also be specified, in this case we’ll include some Hibernate
properties.
Next pat is about Hibernate, JPA & Spring MVC – Part 2.
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
In Hibernate, I can use hbm2dll tool to import data files after hibernate generates the schema from my model. This is fine, but I want to load the file with my database schema and make hibernate use that instead of generating one.
Let's say I have some database configured in my hibernate config
<property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:."/>
Now before I run my test I want to load the db with some schema defined in my schema.sql file. And I don't want to use Spring, I know there is support for it there.
I know I could use hibernate.hbm2ddl to generate schema when SessionFactory is created but I'd rather want to load my manually defined schema. Something like <jdbc:embedded-database> from spring-jdbc.
Is there any support for that in Hibernate?
link1 and link2 might help you out.
Also you can set hibernate.hbm2ddl to none, and add your import.sql file in the classpath which will be executed at the server startup.