gpt4 book ai didi

java - 我需要如何配置 PersistenceJPAConfig?

转载 作者:行者123 更新时间:2023-11-30 06:23:50 24 4
gpt4 key购买 nike

我有一堂带有配置的类(class)。我有一个方法entityManagerFactory(),标记为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;
}

}

我有一个使用 EntityManager 的服务。 EntityManager 标记为 @Autowired,并且 spring create EntityManager 为 null。我哪里配置不正确。我需要使用来自方法entityManagerFactory()(在类PersistenceJPAConfig中)的字段的entityManager。

@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

提前致谢

最佳答案

您应该使用“@PersistenceContext”注释来注入(inject)实体管理器:

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

....

关于java - 我需要如何配置 PersistenceJPAConfig?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47629348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com