Spring-boot + JPA EntityManager Error

In my J2EE application, I am trying to use spring-boot and JPA technologies with the introduction of EntityManager in the DAO layer. However, I have some problems ... My repository for CRUD user:

@Repository public class UserRepositoryImpl implements UserRepository { @PersistenceContext(unitName = "data") private EntityManager entityManager; // and crud methods } 

My spring-boot class:

 @SpringBootApplication public class App { public static void main(String [] args) { SpringApplication.run(App.class, args); } 

}

And finally, my persistence.xml located in the src / main / resources / META-INF folder:

 <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="data" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <class>com.example.domain.User</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.connection.autocommit" value="false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" /> <property name="hibernate.c3p0.min_size" value="4" /> <property name="hibernate.c3p0.max_size" value="128" /> <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=qwerty;sendStringParametersAsUnicode=false" /> <property name="javax.persistence.jdbc.user" value="sa" /> <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="javax.persistence.jdbc.password" value="" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="false" /> </properties> </persistence-unit> 

So, when I try to use this embedded entityManager, I get a NullPointerException. Other @Autowired fields are entered without any problems. What is wrong with this code? Do I need additional configuration? I am a beginner (not even a junior developer), and I have some misunderstanding of what spring-boot is and how to configure it, for example Spring in an xml file. If such an xml configuration is necessary due to EM injection, please show how to do this.

UPD2. Dependencies

 <dependencies> <!-- logger --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <!-- db --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-c3p0</artifactId> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> </dependency> <dependency> <groupId>com.microsoft</groupId> <artifactId>sqljdbc4</artifactId> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.3</version> </dependency> <!-- csv --> <dependency> <groupId>com.opencsv</groupId> <artifactId>opencsv</artifactId> <version>3.3</version> </dependency> <!-- spring-boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.2.4.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.2.4.RELEASE</version> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> 
+5
source share
3 answers

You should use the dependency for spring-boot-starter-data-jpa

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> 

And to use xistence persistence, you must define a bean, as the documentation says.

Spring does not require the use of XML to configure the JPA provider, and Spring Boot suggests that you want to use this feature. If you prefer to use persistence.xml, you need to define your own @Bean of type LocalEntityManagerFactoryBean (with the identifier 'entityManagerFactory and set the name of the save unit there.

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-traditional-persistence-xml

Or you can completely skip persistence.xml and define the connection properties in the application.properties file.

Quote from the documentation

DataSource configuration is controlled by external configuration parameters in spring.datasource. *. For example, you can declare the following section in application.properties:

 spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

(change the driver and other data to suit your environment)

Good luck

+4
source

you can use java configuration to configure jpa persistence. The code below shows an example configuration:

 @Component public class JpaConfiguration { @Bean @Primary public DataSource dataSource() { final SimpleDriverDataSource dataSource = new SimpleDriverDataSource(); dataSource.setDriver(new org.postgresql.Driver()); dataSource.setUrl("jdbc:postgresql://localhost:5432/users"); dataSource.setUsername("postgres"); dataSource.setPassword("admin"); return dataSource; } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setGenerateDdl(true); jpaVendorAdapter.setShowSql(true); jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect"); return jpaVendorAdapter; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean(); lef.setPackagesToScan("tn.bergit.crud.entity"); lef.setDataSource(dataSource()); lef.setJpaVendorAdapter(jpaVendorAdapter()); Properties properties = new Properties(); properties.setProperty("hibernate.show_sql", "true"); properties.setProperty("hibernate.jdbc.fetch_size", "100"); properties.setProperty("hibernate.hbm2ddl.auto", "update"); lef.setJpaProperties(properties); return lef; } } 

You can see this example on github ( click here )

+3
source

if you want to continue using the persistence.xml file just add the code below to your configuration class

  @Bean public LocalEntityManagerFactoryBean entityManagerFactory(){ LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean(); factoryBean.setPersistenceUnitName("data"); return factoryBean; } 
0
source

All Articles