使用Spring Boot的EntityManagerFactory问题

时间:2018-01-27 10:56:39

标签: java spring hibernate spring-boot

这是我的配置类:

def profile_edit(request):
    user = request.student
    form = StudentForm(request.POST or None, initial={'name': user.name,
                                                      'surname': user.surname})
    if request.method == 'POST':
        if form.is_valid():
            user.student.name = request.POST['name']
            user.student.surname = request.POST['surname']

            user.save()
            return HttpResponseRedirect('index')

    context = {
        "form": form
    }

    return render(request, "registration/profile_edit.html", context)

我得到的例外是:

@Configuration
@EnableTransactionManagement
@PropertySource({"classpath:application.properties"})
@ComponentScan({"com"})
public class GrabOneConfiguration {

//  @Autowired
//  private static EntityManagerFactory entityManagerFactory;

     // Private fields

      @Autowired
      private Environment env;

      @Autowired
      private DataSource dataSource;

//    @Autowired
//    private LocalContainerEntityManagerFactoryBean _entityManagerFactory;

    @Value("${db.driver}")
    private String DRIVER;

    @Value("${db.password}")
    private String PASSWORD;

    @Value("${db.url}")
    private String URL;

    @Value("${db.username}")
    private String USERNAME;

    @Value("${hibernate.dialect}")
    private String DIALECT;

    @Value("${hibernate.show_sql}")
    private String SHOW_SQL;

    @Value("${hibernate.hbm2ddl.auto}")
    private String HBM2DDL_AUTO;

    @Value("${entitymanager.packagesToScan}")
    private String PACKAGES_TO_SCAN;

    public GrabOneConfiguration() {
        // TODO Auto-generated constructor stub
    }


    ////////////////////////////////////////////////////////////////
    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("db.driver"));
        dataSource.setUrl(env.getProperty("db.url"));
        dataSource.setUsername(env.getProperty("db.username"));
        dataSource.setPassword(env.getProperty("db.password"));
        return dataSource;
    }

    /**
       * Declare the JPA entity manager factory.
       */
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

        LocalContainerEntityManagerFactoryBean entityManagerFactory =
            new LocalContainerEntityManagerFactoryBean();

        entityManagerFactory.setDataSource(dataSource);

        // Classpath scanning of @Component, @Service, etc annotated class
        entityManagerFactory.setPackagesToScan(
            env.getProperty("entitymanager.packagesToScan"));

        // Vendor adapter
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        entityManagerFactory.setJpaVendorAdapter(vendorAdapter);

        // Hibernate properties
        Properties additionalProperties = new Properties();
        additionalProperties.put(
            "hibernate.dialect", 
            env.getProperty("hibernate.dialect")); 
        additionalProperties.put(
            "hibernate.show_sql", 
            env.getProperty("hibernate.show_sql"));
        additionalProperties.put(
            "hibernate.hbm2ddl.auto", 
            env.getProperty("hibernate.hbm2ddl.auto"));
        entityManagerFactory.setJpaProperties(additionalProperties);

        return entityManagerFactory;
      }

      /**
       * Declare the transaction manager.
       */
//    @Bean
//    public JpaTransactionManager transactionManager() {
//      JpaTransactionManager transactionManager = 
//          new JpaTransactionManager();
//      transactionManager.setEntityManagerFactory(
//          _entityManagerFactory.getObject());
//      return transactionManager;
//    }


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

1 个答案:

答案 0 :(得分:0)

我希望您只定义DatabasePlatform并创建JpaVendorAdapter作为Bean。 在我们的项目中,我们使用它:

@Bean
public JpaVendorAdapter vendorAdapter() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(Boolean.FALSE);
        vendorAdapter.setShowSql(Boolean.TRUE);
        vendorAdapter.setDatabasePlatform("org.hibernate.dialect.Oracle10gDialect");// you should use hibernate dialect base on database type
        return vendorAdapter;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setDataSource(dataSource());
    factory.setJpaVendorAdapter(vendorAdapter());
    factory.setPackagesToScan(
            "org.*"
    );

    factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver());

    factory.setPersistenceUnitName("entityManagerFactory");
    return factory;
}

@Bean
public PlatformTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
    return transactionManager;
}