1

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I already have it working. I can use spring repositories with MySQL (see the link to my prevoius post) What I want for now is pure Hibernate and EntityManager
It worked in a way now I have: Shared EntityManager proxy for target factory[org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean@351e8433] instead of NullPointer and this is great! One thing doesn't work though: when I call persist() nothing happens, I tried to call flush() afterwards but then I have: javax.persistence.TransactionRequiredException: no transaction is in progress. I also have spring.jpa.hibernate.ddl-auto: create-drop
I think you might need @EnableTransactionManagement then.
It didnt work. However I also tried to use EntityTransaction and this is what I get: "Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead", How about persistance.xml, I have it ready, just where to place it?
I added @Transactional to the method where I have a call to persist() and I works:) For some reason every time i restart the application DB records from previous lunch are cleared out and I start with an empty DB. Any idea why? EDIT: spring.jpa.hibernate.ddl-auto: update does the trick:) also thanks for you help Laurentiu, I very much appreciate it!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.