如何使用java配置在Intellij中配置Hibernate Console?

时间:2016-09-12 04:18:40

标签: java spring hibernate jpa intellij-idea

我在Java中配置了Persistence。没有persistence.xml。 Intellij向我展示了entityManagerFactory和Persistence窗口中的实体。查询在应用程序中运行正常。

如何让Intellij中的hibernate控制台运行查询?

我得到了

[2016-09-12 14:09:07] java.lang.NoSuchMethodError: org.hibernate.Session.createQuery(Ljava/lang/String;)Lorg/hibernate/Query

方言是hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect

配置为:

@Configuration
@EnableTransactionManagement
@PropertySource({ "file:${view.config}" })
@ComponentScan({ "my.me.model" })
public class PersistenceConfig {

private static Logger logger = LoggerFactory.getLogger(PersistenceConfig.class);

@Autowired
private Environment env;

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan("my.me.model");

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

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

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
    dataSource.setUrl(env.getProperty("jdbc.url"));
    dataSource.setUsername(env.getProperty("jdbc.user"));
    dataSource.setPassword(env.getProperty("jdbc.password"));

    return dataSource;
}

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


@SuppressWarnings("serial")
Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
            setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
            setProperty("hibernate.globally_quoted_identifiers", "true");
            setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        }
    };
}

1 个答案:

答案 0 :(得分:0)

现在可以在2106.3 EAP中使用。答案是升级。