我需要如何配置PersistenceJPAConfig?

时间:2017-12-04 08:36:59

标签: java spring spring-data-jpa config

我有一个配置的课程。我有一个标记为Bean的方法entityManagerFactory()。

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:hibernate.properties" })
@EnableJpaRepositories
public class PersistenceJPAConfig {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "java.entities" });

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(additionalProperties());

    return em;
}

@Bean
public DataSource dataSource(){
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/carpark?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTCx`");
    dataSource.setUsername( "root" );
    dataSource.setPassword( "1111" );
    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}

@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
    return new PersistenceExceptionTranslationPostProcessor();
}

public Properties additionalProperties() {
    Properties properties = new Properties();
    properties.setProperty("hibernate.hbm2ddl.auto", "update");
    properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
    return properties;
}

}

我有一个使用EntityManager的服务。 EntityManager标记为@Autowired,而spring create EntityManager为null。我配置错误的地方。我需要entityManager与方法entityManagerFactory()中的字段(在类PersistenceJPAConfig中)。

@Service
public class BusService {
@Autowired
private BusRepository busRepository;

@Autowired
private EntityManager entityManager;


public void getBus(){
    try{
       entityManager.getTransaction().begin();
       Query query = entityManager.createNativeQuery("SELECT ID, NUMBER , Rote_ID FROM bus", Bus.class);
       busRepository.save(query.getResultList());
       System.out.println(busRepository.toString());
   }finally {
       entityManager.getTransaction().commit();
   }
}

}

我的包裹就像这样

enter image description here

提前致谢

1 个答案:

答案 0 :(得分:1)

你应该使用`@ PersistenceContext'注入实体管理器的注释:

@Service
public class BusService {
@Autowired
private BusRepository busRepository;

@PersistenceContext
private EntityManager entityManager;

public void getBus(){
  try{
    entityManager.getTransaction().begin();
    Query query = entityManager.createNativeQuery("SELECT ID, NUMBER , Rote_ID FROM bus", Bus.class);
   busRepository.save(query.getResultList());
   System.out.println(busRepository.toString());
  } finally {
    entityManager.getTransaction().commit();
  }
}

有几个原因,您可以获得概述here

并使用@ComponentScan注释

更新配置类
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:hibernate.properties" })
@EnableJpaRepositories
@ComponentScan(basePackages = {
    "model.service"})
public class PersistenceJPAConfig {

   ....

   @Bean
   public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new 
     LocalContainerEntityManagerFactoryBean();
     em.setDataSource(dataSource());
     em.setPackagesToScan(new String[] { "model.entities" });

     JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
     em.setJpaVendorAdapter(vendorAdapter);
     em.setJpaProperties(additionalProperties());
     return em;
   }

....