2016-11-07 101 views
1

我想用springboot,在entitymanager中使用@PersistenceContext,在mysql中創建一個對象,但我得到的是,我的entitymanager對象爲null,不知道爲什麼,因爲使用entitymanager的方法具有@transaction註釋。空EntityManager使用@PersistenceContext

在此先感謝!

這是我的代碼,我調用該方法來插入數據:

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的,始終爲空:

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> 
+0

你可以顯示你的application.propreties文件嗎? –

+0

更新了我的問題! –

回答

5

我認爲在你的情況下你應該與休眠會話工廠的Hibernate Session,而不是實體管理,如果你想與實體管理器的工作就到你的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 dependency

相關問題