Some time ago I was studying Java EE with handouts. It used JSF and Hibernate . I am now working with Spring and Hibernate . But different from Java EE , I do not create any EntityManager .
Is it not required?
Some time ago I was studying Java EE with handouts. It used JSF and Hibernate . I am now working with Spring and Hibernate . But different from Java EE , I do not create any EntityManager .
Is it not required?
Spring Boot has many features already internalized and ready for use in your libs, making it easy to set up when building everything from scratch.
As described in the documentation:
The Spring Framework provides extensive support for working with SQL databases, from direct JDBC access to JdbcTemplate to complete "object relational mapping" technologies such as Hibernate. Spring Data provides an additional level of functionality: creating Repository implementations directly from interfaces and using conventions to generate queries from your method names.
That is, with Spring Boot and Spring Data you can use various interfaces to use things in common, such as queries based on the properties of your classes, among other things.
Many things set up in persistence.xml
can easily be implemented in application.properties
, according to the configuration example below:
app.datasource.url=jdbc:mysql://localhost/test
app.datasource.username=dbuser
app.datasource.password=dbpass
app.datasource.configuration.maximum-pool-size=30
Settings related to datasources can be checked through of this link
If your application requires setting Entity Manager
(using Spring Boot with Spring Batch for example), you can easily expose / configure your Entity Manager
as in the following example:
@Autowired
private EntityManagerFactory entityManagerFactory;
@Bean
public PlatformTransactionManager jpaTransactionManager() {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(entityManagerFactory);
return jpaTransactionManager;
}
In my example I used JpaTransactionManager
in my application to make my batch work more flexible.
When using EntityManagerFactory
you will have full control over what the manager will perform, as described in this documentation :
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 in the presence of a bean of that type.
An additional explanation can also be found in this StackOverflow thread .
I hope I have helped!
Is it not required?
EntityManager
is still required and is also available in Spring, but the direct use of it by the developer will depend on how it needs to query the information it wants.
Since Spring brings a lot of facilities for data search in databases, through CrudRepository
, JpaRepository
, etc, EntityManager
is still used indirectly by the application , because its use is hidden under these interfaces.
If you see, for example, which class implements the save
method of CrudRepository
, you will see that it is SimpleJpaRepository
. Looking at the source code of this class , we will have EntityManager
being used:
@Transactional
public <S extends T> S save(S entity) {
if (this.entityInformation.isNew(entity)) {
this.em.persist(entity); // em é o EntityManager
return entity;
} else {
return this.em.merge(entity);
}
}
In cases where you want to make a more complex query involving Criteria or JPQL, you will need to use EntityManager
directly, even with Spring. To do this, simply inject EntityManager
into your class:
@Autowired
private EntityManager em;
And make the query you want:
Query query = em.createQuery("SELECT pessoa FROM Pessoa pessoa");
return query.getResultList();
As you can see, Entity Manager still exists, but Spring has created some classes to abstract its use.