Null EntityManager使用@PersistenceContext

时间:2016-11-07 09:32:06

标签: java mysql entitymanager spring-transactions

我正在尝试使用Spring Boot的简单编码,使用entitymanager中的@PersistenceContext,在MySQL中创建一个对象,但我得到的是我的entitymanager对象为null并且不确定原因,因为该方法正在使用entitymanager的是@transaction注释。

这是我调用插入数据的方法的代码:

import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class VehicleService implements IVehicleService {

    @Autowired
    IFileService fileService;

    @Transactional
    public void addVehicle(Vehicle vehicle) throws ServiceException{
        Vehicle vehicleNew = new Vehicle();
        vehicleNew.setName(vehicle.getName());
        vehicleNew.setType(vehicle.getType());
        vehicleNew.setEnrollment(vehicle.getEnrollment());
        try{
            fileService.createVehicle(vehicle);
        }catch(Exception e){

        }
    }   
}

import org.hibernate.service.spi.ServiceException;
import org.springframework.transaction.annotation.Transactional;

public interface IFileService {

    @Transactional
    void createVehicle(Vehicle vehicle) throws ServiceException;

}

这是我调用entitymanager的地方,并且始终为null:

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

@Component
public class FileService implements IFileService{

    @PersistenceContext
    protected EntityManager entityManager;

     public void createVehicle(Vehicle vehicle) {
          System.out.println("Inserting........................");
          entityManager.persist(vehicle);
          System.out.println("Inserted!");
          return;
     }
}

的hibernate.cfg.xml

<hibernate-configuration>
 <session-factory name="hibernateSessionFactory">
  <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="hibernate.connection.password">global</property>
  <property name="hibernate.connection.url">jdbc:mysql://localhost/testDB</property>
  <property name="hibernate.connection.username">root</property>
  <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  <property name="hibernate.show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
<!--   <property name="hibernate.hbm2ddl.auto">create</property> -->
  <mapping class="com.org.testing.Vehicle"/>
 </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:5)

我认为在您的情况下,您应该使用 hibernate会话工厂 hibernate会话而不是实体管理器,如果您想使用实体经理只需转到资源文件中的 application.properties 并添加:

spring.datasource.url = jdbc:mysql://localhost:3306/testDB
# Username and password
spring.datasource.username = root
spring.datasource.password = global
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
不要忘记在你的pom.xml中添加spring jpa依赖