- How to configure Hibernate Db connection settings in JAVA based spring config
- 3 Answers 3
- Bootstrapping Hibernate 5 with Spring
- Get started with Spring Data JPA through the reference Learn Spring Data JPA course:
- 1. Overview
- 2. Spring Integration
- 3. Maven Dependencies
- 4. Configuration
- 4.1. Using Java Configuration
- 4.2. Using XML Configuration
- 5. Usage
- 6. Supported Databases
- 7. Conclusion
How to configure Hibernate Db connection settings in JAVA based spring config
Currently I am moving my beans creation and config settings from XML based to JAVA based spring config. I am stuck with moving db settings? How can I write these hibernate db settings in JAVA based config file?
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url=jdbc:mysql://url hibernate.connection.username=username hibernate.connection.password=password hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.show_sql=false
3 Answers 3
Here is the config. I have also added transaction management features which you will definately need in the final solution.
import java.util.Properties; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.DriverManagerDataSource import org.springframework.orm.hibernate3.HibernateTransactionManager; import org.springframework.orm.hibernate3.LocalSessionFactoryBean import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @EnableTransactionManagement public class DbConfig < @Bean public DataSource getDatasource() < DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://url"); dataSource.setUsername("username"); dataSource.setPassword("password"); return dataSource; >@Bean public SessionFactory getSessionFactory() throws IOException < LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setPackagesToScan("com.test"); //getHibernateProperties method is a private method sessionFactoryBean.setHibernateProperties(getHibernateProperties()); sessionFactoryBean.setDataSource(getDatasource()); sessionFactoryBean.afterPropertiesSet(); return sessionFactoryBean.getObject(); >@Bean public HibernateTransactionManager getTransactionManager() throws IOException < HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(getSessionFactory()); return transactionManager; >private static Properties getHibernateProperties() < Properties hibernateProperties = new Properties(); hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect"); hibernateProperties.put("hibernate.show_sql", false); // other properties return hibernateProperties; >
I am getting NoClassDefFound exception. I have updated the question with few more details and exception. Can you please help?
Bootstrapping Hibernate 5 with Spring
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
We rely on other people’s code in our own work. Every day.
It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.
The problem is, of course, when things fall apart in production — debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky.
Lightrun is a new kind of debugger.
It’s one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics.
Learn more in this quick, 5-minute Lightrun tutorial:
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes:
DbSchema is a super-flexible database designer, which can take you from designing the DB with your team all the way to safely deploying the schema.
The way it does all of that is by using a design model, a database-independent image of the schema, which can be shared in a team using GIT and compared or deployed on to any database.
And, of course, it can be heavily visual, allowing you to interact with the database using diagrams, visually compose queries, explore the data, generate random data, import data or build HTML5 database reports.
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
Get started with Spring Data JPA through the reference Learn Spring Data JPA course:
We’re looking for a new Java technical editor to help review new articles for the site.
1. Overview
In this article, we’ll discuss how to bootstrap Hibernate 5 with Spring, using both Java and XML configuration.
This article focuses on Spring MVC. Our article Spring Boot with Hibernate describes how to use Hibernate in Spring Boot.
2. Spring Integration
Bootstrapping a SessionFactory with the native Hibernate API is a bit complicated and would take us quite a few lines of code (have a look at the official documentation in case you really need to do that).
Fortunately, Spring supports bootstrapping the SessionFactory – so that we only need a few lines of Java code or XML configuration.
3. Maven Dependencies
Let’s get started by first adding the necessary dependencies to our pom.xml:
org.hibernate hibernate-core 5.4.2.Final
The spring-orm module provides the Spring integration with Hibernate:
org.springframework spring-orm 5.1.6.RELEASE
For the sake of simplicity, we’ll use H2 as our database:
Finally, we are going to use Tomcat JDBC Connection Pooling, which fits better for production purposes than the DriverManagerDataSource provided by Spring:
org.apache.tomcat tomcat-dbcp 9.0.1
4. Configuration
As mentioned before, Spring supports us with bootstrapping the Hibernate SessionFactory.
All we have to do is to define some beans as well as a few parameters.
With Spring, we have two options for these configurations, a Java-based and an XML-based way.
4.1. Using Java Configuration
For using Hibernate 5 with Spring, little has changed since Hibernate 4: we have to use LocalSessionFactoryBean from the package org.springframework.orm.hibernate5 instead of org.springframework.orm.hibernate4.
Like with Hibernate 4 before, we have to define beans for LocalSessionFactoryBean, DataSource, and PlatformTransactionManager, as well as some Hibernate-specific properties.
Let’s create our HibernateConfig class to configure Hibernate 5 with Spring:
@Configuration @EnableTransactionManagement public class HibernateConf < @Bean public LocalSessionFactoryBean sessionFactory() < LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan( ); sessionFactory.setHibernateProperties(hibernateProperties()); return sessionFactory; > @Bean public DataSource dataSource() < BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.h2.Driver"); dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"); dataSource.setUsername("sa"); dataSource.setPassword("sa"); return dataSource; >@Bean public PlatformTransactionManager hibernateTransactionManager() < HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory().getObject()); return transactionManager; >private final Properties hibernateProperties() < Properties hibernateProperties = new Properties(); hibernateProperties.setProperty( "hibernate.hbm2ddl.auto", "create-drop"); hibernateProperties.setProperty( "hibernate.dialect", "org.hibernate.dialect.H2Dialect"); return hibernateProperties; >>
4.2. Using XML Configuration
As a secondary option, we can also configure Hibernate 5 with an XML-based configuration:
create-drop org.hibernate.dialect.H2Dialect
As we can easily see, we’re defining exactly the same beans and parameters as in the Java-based configuration earlier.
To bootstrap the XML into the Spring context, we can use a simple Java configuration file if the application is configured with Java configuration:
@Configuration @EnableTransactionManagement @ImportResource() public class HibernateXMLConf < // >
Alternatively, we can simply provide the XML file to the Spring Context, if the overall configuration is purely XML.
5. Usage
At this point, Hibernate 5 is fully configured with Spring, and we can inject the raw Hibernate SessionFactory directly whenever we need to:
public abstract class BarHibernateDAO < @Autowired private SessionFactory sessionFactory; // . >
6. Supported Databases
Unfortunately, the Hibernate project doesn’t exactly provide an official list of supported databases.
That being said, it’s easy to see if a particular database type might be supported, we can have a look at the list of supported dialects.
7. Conclusion
In this quick tutorial, we configured Spring with Hibernate 5 – with both Java and XML configuration.
As always, the full source code of the examples is available over on GitHub.
Slow MySQL query performance is all too common. Of course it is. A good way to go is, naturally, a dedicated profiler that actually understands the ins and outs of MySQL.
The Jet Profiler was built for MySQL only, so it can do things like real-time query performance, focus on most used tables or most frequent queries, quickly identify performance issues and basically help you optimize your queries.
Critically, it has very minimal impact on your server’s performance, with most of the profiling work done separately — so it needs no server changes, agents or separate services.
Basically, you install the desktop application, connect to your MySQL server, hit the record button, and you’ll have results within minutes: