JPA: Creating EntityManager without persistence.xml

For JPA to work, we need to define a persistence.xml inside META-INF. I always found it pretty cumbersome and difficult to maintain. Especially so for my integration tests. Wouldn’t it be cool if we could do everything from Java code?

Here you go:

Using only Hibernate

Using Spring-JPA

Note that you can add your entities through code. In order for this to compile, you would need to include the below dependencies in maven:

The complete sources can be found here:

https://github.com/paawak/blog/tree/master/code/one-to-many-example-2

This particular Java file is:

https://github.com/paawak/blog/blob/master/code/one-to-many-example-2/src/test/java/com/swayam/demo/spring/springbootdemo/JpaIntegrationTest.java

Leave a Reply

Your email address will not be published. Required fields are marked *