Get Dialect within some class inside Spring MVC + Hibernate application - java

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.

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.

Spring hibernate transactional removing entity

I have Spring MVC application divided on layers. One of them is Service and here's one of the methods
#Transactional
public boolean deleteProject(long id){
Project project = projectDAO.read(id);
taskDAO.deleteAllByProject(project);
projectDAO.delete(project);
return true;
}
It deletes tasks, but project remains in the db (with no error or exception). I didnt use #Transactional in repository classes.
Here's my Spring config
<?xml version="1.0" encoding="UTF-8"?>
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/automate" />
<property name="username" value="postgres" />
<property name="password" value="" />
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="ru.jeak.keep" />
<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>
<prop key="connection.pool_size">3</prop>
<prop key="hibernate.temp.use_jdbc_metadata_defaults">false</prop>
<prop key="hibernate.default_schema">test</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="userHibernateDAO" class="ru.jeak.keep.repository.hibernate.UserHibernateDAO" />
<bean id="projectHibernateDAO" class="ru.jeak.keep.repository.hibernate.ProjectHibernateDAO" />
<bean id="taskHibernateDAO" class="ru.jeak.keep.repository.hibernate.TaskHibernateDAO" />
UPDATE
I added persistence #Transactional to DAO class and it works now. But Im still confused why Spring did not do the rollback of deleting tasks after failure of deleting project

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 :)

How to configure Hibernate inside Spring configuration file

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?

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();

Categories

Resources