使用@PersistenceContext的EntityManager为空 [英] Null EntityManager using @PersistenceContext

查看:941
本文介绍了使用@PersistenceContext的EntityManager为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Spring Boot中使用简单的编码,在Entitymanager中使用@PersistenceContext来在MySQL中创建对象,但是我发现我的objectmanager对象为null,不知道为什么,因为该方法正在使用entitymanager的@transaction注释.

I'm trying to use a simple coding with Spring Boot, using the @PersistenceContext in the entitymanager, to create a object in MySQL, but I'm getting that my entitymanager object is null and not sure why, because the method that is using the entitymanager hast the @transaction annotation.

这是我的代码,我在其中调用插入数据的方法:

This is my code where I call the method to insert the data:

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:

Here is where I call the entitymanager and always is 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.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>

推荐答案

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

I think in your case you should work with hibernate session factory and hibernate session instead of entity manager ,if you want to work with entity manager just go to your application.properties in your resource file and add this :

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依赖项

don't forget to add spring jpa dependency in your pom.xml

这篇关于使用@PersistenceContext的EntityManager为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆