我正在使用Spring Data,但是对于自定义查询,我想开始使用EntityManager。
问题是,除了使用EntityManager进行查询外,所有使用Spring Data进行的查询都可以正常工作。对于EntityManager查询,我一直都有超时错误。

@Bean
public BasicDataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl(MessageFormat.format(Constants.DATABASE_CONNECTION_URL, host, port, schema));
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setTestWhileIdle(true);
    ds.setValidationQuery(validationQuery);
    ds.setTimeBetweenEvictionRunsMillis(validationTestInterval);
    return ds;
}

@Bean
public EntityManager entityManager() {
    return entityManagerFactory(dataSource()).createEntityManager();
}

@Bean
public EntityManagerFactory entityManagerFactory(BasicDataSource dataSource) {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setShowSql(false);
    vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
    vendorAdapter.setDatabase(Database.MYSQL);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan(
            "com.xxx"
    );

    Properties properties = new Properties();
    properties.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
    properties.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    properties.setProperty("hibernate.connection.url", MessageFormat.format(Constants.DATABASE_CONNECTION_URL, host, port, schema));
    properties.setProperty("hibernate.connection.username", username);
    properties.setProperty("hibernate.connection.password", password);
    properties.setProperty("hibernate.c3p0.acquire_increment", "5");
    properties.setProperty("hibernate.c3p0.idle_test_period", "300");
    properties.setProperty("hibernate.c3p0.min_size", "5");
    properties.setProperty("hibernate.c3p0.timeout", "1000");

    factory.setJpaProperties(properties);
    factory.afterPropertiesSet();

    return factory.getObject();
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    JpaDialect jpaDialect = new HibernateJpaDialect();
    txManager.setEntityManagerFactory(entityManagerFactory);
    txManager.setJpaDialect(jpaDialect);
    txManager.setDefaultTimeout(60000);
    txManager.setRollbackOnCommitFailure(true);
    return txManager;
}



@Component
public class ReportGenerator {

    @Autowired
    private EntityManager entityManager;

    private List<MyObject> doSomething() {
        StringBuilder sql = new StringBuilder();
        // build query
        List<MyObject> list = entityManager.createNativeQuery(sql.toString(), MyObject.class).getResultList();
        entityManager.clear();
        return list;
    }


有什么建议吗?

更新1:

validationQuery = SELECT 1;
validationTestInterval = 30000

最佳答案

@PersistenceContext

@PersistenceContext
private EntityManager entityManager;


更新:

我认为:这个简短的答案总比没有好,可以为我节省很多时间

link 1


  很好,但是如何获得EnityManager?
  这实际上可能是最酷的部分。基本上,您可以通过添加@PersistenceContext JPA注释来完全按照不使用Spring时的方式定义DAO。


link 2 (rus)


  
  Вбинах,гденеобходимменеджерсущностейпометитьсоответствующееполеаннотацией@PersistenceContext
  


link 3


  @PersistenceContext注入容器管理的实例。

08-04 13:16