Interlated Interlated - 1 month ago 62
Java Question

How do I configure Hibernate Console in Intellij with java config?

I have a Persistence configured in Java. No persistence.xml. Intellij shows me the entityManagerFactory and the entities in the Persistence window. The query runs OK in the application.

How do I get the hibernate console in Intellij to run queries?

I get

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


The dialect is hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect

The configuration is

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

Answer

This now works in 2106.3 EAP. The answer is to upgrade.