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
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.
p>
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=trueThen 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 CrudRepositoryIf 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