How to configure Hibernate inside Spring configuration file - java

I'm studying Spring MVC and Hibernate. I had no problems handling database connections and queries with Spring mvc (MySql DB).
Now, I'm trying to use Hibernate and I found it intricate:
create a hibernate configuration file, create a class for retrieving SessionFactory, create a xml file for any persistent object etc.
I'm sure there's a simplest way that allow me to do an easy configuration using just:
the Spring xml configuration file
annotation (in persistent object classes)
What iI want to reach is something like the following. I saw a similar code in an example, but now I'm no more able to find it on the internet
xxxx-servlet.xml
<bean id="SessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.springgestioneerrori.model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.connection.autocommit">true</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/dbName" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
In the code above, I suppose, that what it's not correct is my dataSource bean. Does anybody know the way to reach my goal?
Thank everybody!

i think your properties names inside the 'data source' bean should be like the following :
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/dbName" />
<property name="username" value="root" />
<property name="password" value="root" />
so instead of driverClass , it should be driverClassName and so on ..
and please refer to this answer here , it talks about using spring DriverManagerDataSource vs apache BasicDataSource .
Hope that Helps

Everything must be correct, but I'd want to offer you to use *.properties to keep connection configuration to your DB like that
in appContext.xml:
<!-- JDBC DataSource bean -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:properties/database.properties</value>
</list>
</property>
</bean>
in database.properties:
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.schema = schema
jdbc.url = jdbc:mysql://localhost:3306/schema
jdbc.username = root
jdbc.password = password
And what interferes you to use annotations in your classes?

Related

How to configure Hibernate 5 SessionFactoryBean for using HikariCP?

Where should I configure connection pool for spring + hibernate application?
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="edu.khai.education.entity"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:tcp://localhost/~/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
I want to use org.hibernate.hikaricp.internal.HikariCPConnectionProvider and I don't have a problem to configure it in Hibernate application, but I don't know how to integrate it with Spring.
Spring uses DataSource bean to create a lot of infrastructure code e.g. JdbcTemplate or Spring Data JPA #Repository.
Replace the current data source implementation org.apache.commons.dbcp2.BasicDataSource with the com.zaxxer.hikari.HikariDataSource class. Change the current properties to match the HikariCP configuration.

How to configure spring to use two different datasources. Getting a NoUniqueBeanDefinitionException for PlatformTransactionManger error

I'm working on spring boot application, which already has a database connection established in its applicationContext.xml file and the necessary transaction manager and vendors etc.
I now need to connect the app to a second database. But I'm having issues with this. In my unit tests the connection is fine and can make simple queries to retrieve data, which is all I need it to do. However when I compile the app into a jar and run it, I get the following error
NoUniqueBeanDefinitionException: No qualifying bean of type "org.springframework.transaction.PlatformTransactionManager" available: expected single matching bean but found 2: transactionManager, transactionManager2
I have spent ages looking up how to solve this, and the suggested fixes I have found here , here and here have not worked.
I have one persistence.xml with two persistence units defined. And in my applicaitonContext.xml I defined two datasources, two transaction managers and two entity Manager Factories. I then use the #persitsencecontext and #Transactional("") annotations to say which persistence unit and managers to use, but I still get an error. I also added in the <qualifier> tag to the app context file, as I saw this as a suggested fix with the #transactional annotation, still no luck.
My code is below, can anyone spot an errors I have made, and why it may not be working as expected
applicationContext.xml
<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="..."/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
<bean id="entityManagerFactory" name="proxy">
<property name="persistenceUnitName" value="proxy" />
<property name="persistenceUnitXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource1" />
<property name="jpaVendorAdapter" ref="hiberanteVendorAdapter" />
<property name="jpaProperties">
<props>
<prop key="hiberante.hbm2ddl.auto">valudate</prop>
</props>
</property>
</bean>
<bean id="hibernateVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
<property name="database" value="HSQL" />
<property name="showSql" value="true" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<qualifier value="transactionManager1" />
</bean>
<!-- Second datasource -->
<bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="..."/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
<bean id="entityManagerFactory2" name="proxy">
<property name="persistenceUnitName" value="proxy2" />
<property name="persistenceUnitXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource2" />
<property name="jpaVendorAdapter" ref="hiberanteVendorAdapter2" />
<property name="jpaProperties">
<props>
<prop key="hiberante.hbm2ddl.auto">valudate</prop>
</props>
</property>
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory2" />
<qualifier value="transactionManager2" />
</bean>
<bean id="hibernateVendorAdapter2" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<tx:annotation-driven/>
Implementation
#Repository
#Transactional("transactionManager2")
public class myDaoImpl extends GenericJPADao<Integer, Integer> implements ImyDao {
#PersistenceContext(unitName="proxy2")
protected EntityManager em;
}
SOLUTION
The accepted answer was the correct solution for me, but a few things to note. The beans have to point to their respective entityManagerFactory's and you need to be careful on which bean you set the autowire-candidate="false" on, as I set it on the incorrect one at first, and had transactions rolled back as a result. I think there could be cleaner solution to this, but as a quick fix it works fine
try this :
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" autowire-candidate="false">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

Spring xml constructor injection of JPA entity manager

I am using Spring in a standalone desktop application along with JPA and Hibernate. So what i need is a global entity manager, provided by spring. The thing is, i use the ClassPathXmlApplicationContext and constructor injection, because i prefer my classes to be totally free of any framework.
So a bean in my ApplicationContext.xml basically looks like this:
<bean id="bookingDAO" class="dk.gokartland.booking.dao.BookingDAO">
<constructor-arg name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
So i do the actual wiring myself.
So what i've done until now is pass the EntityManagerFactory to my DAOs, but this doesn't really work when i close it manually. So i would like to pass the same EntityManager around.
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-jpa
The LocalEntityManagerFactoryBean sounds like what i need, but i do not know how to map it through the applicationContext.xml.
My current JPA configuration looks like:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="dk.gokartland.booking.domain"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="#{database['jdbc.url']}"/>
<property name="username" value="#{database['jdbc.username']}"/>
<property name="password" value="#{database['jdbc.password']}"/>
</bean>
Now somehow i need to be able to to something like this:
<bean id="bookingDAO" class="dk.gokartland.booking.dao.BookingDAO">
<constructor-arg name="entityManager" ref="entityManager"/>
</bean>
Hope someone has a simple solution :)

Passing hibernate.connection.datasource from Spring's applicationContext xml

Earlier I had datasource configured in the application server itself and I am passing it to application level hbr.xml file as
<property name="hibernate.connection.datasource">java:comp/env/jdbc/myDataSource</property>
Now I have configured datasource in applicationContext.xml as
<bean id="mydatasource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydatasource;maxPoolSize=100" />
<property name="username" value="sa" />
<property name="password" value="${database.password}"/>
</bean>
How to pass this to hbr.xml file?
Based on the given below my another question is that what should be the class for my session factory? Should it be org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
OR
org.springframework.orm.hibernate3.LocalSessionFactoryBean
Inject the datasource along with the hibernate mappings into your DAOs:
<bean id="mydatasource" ...>
...
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mydatasource" />
<property name="mappingResources">
<list>
<value>path/to/your/mappings.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props><!-- see http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/session-configuration.html -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
....
</props>
</property>
...
</bean>
Using annotations you could access your datasource through the injected bean
#Resource
private SessionFactory sessionFactory;
...
Session session = this.sessionFactory.getCurrentSession();

Get Dialect within some class inside Spring MVC + Hibernate application

I have Hibernate Transaction manager configured inside Spring MVC controller.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://127.0.0.1/doolloop2" />
<property name="username" value="doolloop2" />
<property name="password" value="doolloop" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>WEB-INF/mapping/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
In Addition, I have some class which needs to get Hibernate Dialect inside on of it method.
Is class is not Configured as bean inside Spring Framework.
How can I access Hibernate Dialect property from this class? I believe it should be some static class,but I don't know how can I do it. Please help.
You could separate the properties from the spring config. Put them in a properties file, then reference that in a PropertyPlaceholderConfigurer bean ( http://almaer.com/blog/spring-propertyplaceholderconfigurer-a-nice-clean-way-to-share ). Then you could inject that value into whatever bean it is that you need the value in the same way you are injecting it into the sessionFactory bean.

Categories

Resources