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?

Community
  • 1
  • 1
jarosik
  • 4,136
  • 10
  • 36
  • 53

1 Answers1

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

Alex Mantaut
  • 3,657
  • 3
  • 35
  • 45
Laurentiu L.
  • 6,566
  • 1
  • 34
  • 60
  • 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 – jarosik Aug 26 '15 at 13:35
  • 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 – jarosik Aug 26 '15 at 14:46
  • I think you might need @EnableTransactionManagement then. – Laurentiu L. Aug 26 '15 at 14:47
  • 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? – jarosik Aug 26 '15 at 14:54
  • If you prefer to use persistence.xml then you need to define your own @Bean of type LocalEntityManagerFactoryBean (with id ‘entityManagerFactory’, and set the persistence unit name there. However i don't recommend that. Rather try and see why persist() does not work for you. I don't think changing to persistence.xml will change that. – Laurentiu L. Aug 26 '15 at 15:03
  • And more than, why are you calling persist? Use CrudRepositories or JpaRepositories for what you need. You will need to configure them with @EnableJpaRepositories("com.acme.repositories") (your repository packages) – Laurentiu L. Aug 26 '15 at 15:06
  • 1
    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! – jarosik Aug 26 '15 at 16:05