spring-boot – Spring boot – 配置EntityManager

I use Google guice in my project, and now I try to completely convert the framework to SpringBoot.

I configured Bean for persistence.xml, As shown below

@Autowired
@Bean(name = "transactionManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
LocalContainerEntityManagerFactoryBean lEMF = new LocalContainerEntityManagerFactoryBean();
lEMF.setPersistenceUnitName("leaseManagementPU");
lEMF.setPersistenceXmlLocation("persistence.xml");
return lEMF;
}

Now I need to configure (inject) EntityManager em to perform JPA operations like em.persist(), em.find, etc... How do I configure it, and some people try to explain this with sample code

With Spring Boot, there is no need to use a configuration file like persistence.xml. You can use annotations to configure just in JPA database configuration can be

application.properties

spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@DB...
spring.datasource.username=username
spring.datasource.password=pass< br />
spring.jpa.database-platform=org.hibernate.dialect....
spring.jpa.show-sql=true

Then you can use Spring to provide CrudRepository, you have standard CRUD transaction methods. There, you can also implement your own SQL, such as JPQL.

@Transactional
public interface ObjectRepository extends CrudRepository {
...
}

If you still need to use the entity manager, you can create another class.

public class ObjectRepositoryImpl implements ObjectCustomMethods{

@PersistenceContext
private EntityManager em;

}

This should be in your pom.xml


org.springframework.boot
spring-boot-starter- parent
1.2.5.RELEASE





org.springframework.boot
spring-boot-starter-data-jpa

< br />

org.springframework
spring-orm


< groupId>org.hibernate
hibernate-core
4.3.11.Final

I use Google guice in my project, and now I try to fully convert the framework to SpringBoot.

I configured Bean for persistence.xml , As shown below

@Autowired
@Bean(name = "transactionManager")
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
LocalContainerEntityManagerFactoryBean lEMF = new LocalContainerEntityManagerFactoryBean();
lEMF.setPersistenceUnitName("leaseManagementPU");
lEMF.setPersistenceXmlLocation("persistence.xml");
return lEMF;
}< /pre>

Now I need to configure (inject) EntityManager em to perform JPA operations like em.persist(), em.find etc... How do I configure it, and some people try to explain this with sample code

p>

With Spring Boot, there is no need to use something like persistence.xml Such a configuration file. You can use annotations for configuration. Just configure the JPA database configuration in

application.properties

spring .datasource.driverClassName=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@DB...
spring.datasource.username=username
spring. datasource.password=pass

spring.jpa.database-platform=org.hibernate.dialect....
spring.jpa.show-sql=true

Then you can use the CrudRepository provided by Spring, you have a standard CRUD transaction method. There, you can also implement your own SQL, such as JPQL.

@Transactional
public interface ObjectRepository extends CrudRepository {
...
}

If you still need to use the entity manager, you can create another class.

public class ObjectRepositoryImpl implements ObjectCustomMethods{

@PersistenceContext
private EntityManager em;

}

This should be in your pom.xml


org.springframework.boot
spring-boot-starter-parent
1.2.5.RELEASE





org.springframework.boot
spring-boot-starter-data-jpa




org.springframework
spring-orm


org.hibernate
hibernate-core
4.3.11.Final

Leave a Comment

Your email address will not be published.