Why doesn’t Spring boot use Entitymanager?

Asked

Viewed 2,034 times

3

Some time ago I was studying Java EE with handouts. Wore JSF and Hibernate. Now I’m working with Spring and Hibernate. But different from Java EE, I don’t create any Entitymanager.

It is not mandatory?

2 answers

4


Spring Boot has a number of built-in, ready-to-use features in its libs, making it easy to set-up by creating everything from scratch.

As described in the documentation:

The Spring Framework provides extensive support for Working with SQL Databases, from direct JDBC access using 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.

Various things configured on persistence.xml can be easily 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

How to use Entity Manager with Spring Boot?

If your application requires configuration of the Entity Manager (using Spring Boot with Spring Batch for example), you can easily expose/configure your Entity Manager as the following example:

@Autowired
private EntityManagerFactory entityManagerFactory;

@Bean
public PlatformTransactionManager jpaTransactionManager() {
    JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(entityManagerFactory);
    return jpaTransactionManager;
}

In this example I used the JpaTransactionManager in my application to make my batch work more flexible.

When using the EntityManagerFactory you will have full control over what the manager will perform, as described in this section of 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 on this Stackoverflow thread.

I hope I’ve helped!

1

It is not mandatory?

The EntityManager remains mandatory and is also available in Spring, but use straightforward of it by the developer will depend on how he needs to query the information he wants.

As Spring brings several facilities for data searches in databases, through the CrudRepository, JpaRepository, etc., the EntityManager is still used shape indirect by the application, as its use is hidden under these interfaces.

If you check, for example, which class implements the method save of CrudRepository, you will see that it is the SimpleJpaRepository. Looking at the source code of this class, we will have the 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 the EntityManager directly, even with Spring. For this, simply inject the EntityManager in your class:

@Autowired
private EntityManager em;

And make any query you wish:

Query query = em.createQuery("SELECT pessoa FROM Pessoa pessoa");
return query.getResultList();

As you can see, Entity Manager continues to exist, but Spring has created some classes to abstract its use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.