1

I have a class with config. And I have a method entityManagerFactory() that tagged like Bean.

@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;
}

}

And I have a service that use EntityManager. EntityManager tagged like @Autowired, and spring create EntityManager is null. Where I'm configured incorrectly. I need to entityManager with fields from method entityManagerFactory() (in class 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();
   }
}

}

my packages are located like this

enter image description here

thanks in advance

1 Answers1

1

You should use the `@PersistenceContext' annotation to inject the entity manager:

@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();
  }
}

there are several reasons to do that, you can get an overview here

and update you configuration class with the @ComponentScan annotation

@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;
   }

....
gtosto
  • 1,381
  • 1
  • 14
  • 18
  • Okey. I add, and it doesn't work too. You can see this https://screenshare.ru/2jdHA3EC – Alexandr Romanovskij Dec 04 '17 at 08:55
  • It seems to me that your service class was not scanned by spring.. you should add a @ComponentScan annotation to your configuration – gtosto Dec 04 '17 at 09:24
  • if your are you using springboot for entity it's better to use the @EntityScan annotation, otherwise you should declare the entities in your jpa configuration – gtosto Dec 04 '17 at 10:00
  • I did like you, but it doesn't work too https://pastebin.com/RqiN5H9n . Exception is https://pastebin.com/husTWw39 . I don't use springboot. – Alexandr Romanovskij Dec 04 '17 at 10:04