Spring 4 + Hibernate 4 configuration - java

I am trying to do Spring 4 + Hibernate 4 configuration. But I am facing sessionFactory = null in the Controller. Below are the configurations and code.
What is the mistake in this configuration?
spring-database.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- MySQL data source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/personal" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>/orm/Song.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
<bean id="songDao" class="com.letsdo.impl.SongImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- MUST have transaction manager, using aop and aspects -->
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
orm/Song/hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.letsdo.model.Song" table="song" catalog="personal">
<id name="id" type="int">
<column name="id" length="45" />
<generator class="increment"/>
</id>
<property name="filePath" column="filepath"/>
<property name="fileName" column="filename"/>
<property name="album" column="album"/>
<property name="title" column="title"/>
<property name="size" column="size"/>
</class>
</hibernate-mapping>
SongDao.java
public interface SongDao {
public List<String> getAllAlbums();
}
SongImpl.java
#Service
#Transactional
public class SongImpl implements SongDao{
private SessionFactory sessionFactory;
#SuppressWarnings("unchecked")
public List<String> getAllAlbums(){
List<String> allAlbums = new ArrayList<String>();
Query query = getSessionFactory().getCurrentSession().createQuery("Select DISTINCT Album from song");
allAlbums = query.list();
return allAlbums;
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
Controller: HomeController.java
public class HomeController {
#Autowired private SongImpl songDao;
#RequestMapping(value="/", method=RequestMethod.GET)
public ModelAndView welcomePageBeforeLogin(HttpServletRequest request, HttpServletResponse response,HttpSession session){
ModelAndView model = new ModelAndView();
List<String> album = songDao.getAllAlbums();
model.addObject("albumsize",album.size());
model.setViewName("hello");
return model;
}
}

I think you are ending up with two beans of class SongImpl - one that is defined in xml (named songDao), and another one annotated with #Service (named by default songImpl). The latter one has no SessionFactory autowired, that's why it is null.
You can find more info about it in one of the answers here.
If component scanning is enabled, spring will try to create a bean
even though a bean of that class has already been defined in the
spring config xml. However if the bean defined in the spring config
file and the auto-discovered bean have the same name, spring will not
create a new bean while it does component scanning.
The solution is to remove the xml version of the bean (if you don't need it), and to autowire SessionFactory in SongImpl.

Related

How to solve org.springframework.beans.factory.BeanNotOfRequiredTypeException error in Spring MVC

I am new in Spring. I have tried a demo project with Spring MVC, Hibernate, Spring-tx and Spring ORM. There I am facing the error. My scenario is:
public interface DaoInterface{
public SessionFactory getSessionFactory();
public Session getSession();
}
Another class BaseDao:
public abstract class BaseDao implements DaoInterface{
#Autowired
private SessionFactory sessionFactory;
private Session session;
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
public Session getSession() {
this.session = sessionFactory.getCurrentSession();
return this.session;
}
public abstract User retriveUser(String email);
}
Another class UserDao:
#Component
public class UserDao extends BaseDao{
#Override
#Transactional
public User retriveUser(String email) {
System.out.println("In userDao : retriveUser");
//other code
}
}
Now from my service class, when I am going to get UserDao object using BaseDao, I got the error:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'userDao' is expected to be of type 'dao.BaseDao' but was actually of type 'com.sun.proxy.$Proxy94'
My service class code:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BaseDao userDao = context.getBean("userDao", BaseDao.class);
If I do the following, no error comes.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DaoInterface userDao = context.getBean("userDao", DaoInterface.class);
My applicationContext.xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="model" />
<context:component-scan base-package="dao" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="abcDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo"></property>
<property name="user" value="xxxx"></property>
<property name="password" value="xxxxx"></property>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="maxIdleTime" value="30000" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="abcDataSource" />
<property name="packagesToScan" value="model" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="abcTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="abcTransactionManager" />
</beans>
So, my question is, what need to be done in my application, so that I can get UserDao object using BaseDao class reference, not DaoInterface reference?
Some answers are there in this platform, but I am not getting the exact answer. Few solutions I have tried but can't solve.
Spring uses JDK dynamic proxies by default ($Proxy94), that can only proxy interfaces. That's basically why you can cast the userDao bean to the Dao interface, but not to the BaseDao class.
By the way you should prefer coding by interfaces.
However You can use <tx:annotation-driven proxy-target-class="true"/> to instruct Spring to use CGLIB proxies.

#Transactional annotation not working in Spring MVC

#Transactional is not working in spring mvc. suppose i removed
#Transactional annotation data is reached to RepositoryClass.
Throwable targetException - org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(Object...)
i need to reach data to repository class.
please help me.,
Thank you.
ServiceImplClass
#Service("userService")
public class UserServiceImpl implements UserService{
#Autowired
UserRepository userRepository;
public String saveUserData(User user,HttpSession session) {
return userRepository.saveUserData(user);
}
}
RepositoryClass:
#Component
#Transactional
public class UserRepository {
#Autowired
protected SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public String saveUserData(User user) {
final Session session = (Session) getSessionFactory();
try {
session.beginTransaction();
Query query=session.createQuery("UPDATE User set user_Name =:userName,"
+ "reg_Date =:regDate,"
+ "img_Id=:imgId, emailId =:emailId");
query.setParameter("userName", user.getUserName());
query.setParameter("regDate", user.getRegDate());
query.setParameter("imgId", user.getImgId());
query.setParameter("emailId", user.getEmailId());
session.save(user);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
">
<context:annotation-config />
<context:component-scan base-package="com.demo.app" />
<mvc:default-servlet-handler />
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/plain;charset=UTF-8" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/html/" />
<property name="suffix" value="html" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/UserDB" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.demo.app.model.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">Error</prop>
</props>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152" />
</bean>
</beans>
You're managing your transactions manually. That's the task of transaction manager. saveUserData should be like:
public User saveUserData(User user) {
return (User)sessionFactory.getCurrentSession().merge(user);
}
And that's it.
And you'll probably want to annotate your service with #Transactional and not repository.
Your Repository don`t use #component annotation. and you use
<jpa:repositories base-package="your.package.put.repository"></jpa:repositories>
so, you already used
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
First of all all transaction and session should managed by spring container is good practice so please don't manage session at your own, just use the existing session for data base querying. Now, for only your situation, try #Transactional annotation at Controller level and if it will work then you require some modification as per given below.
For web MVC Spring app should #Transactional go on controller or service?
Use #Transactional at Service level and when one of the operations doesnt work as it should(for example, an update operation returns 0 which means it failed) throw new RuntimeException(). If one of the operation fails, all other operations which are part of the transaction will be rolled back.
You are trying to use both container managed transaction and User managed transaction at the same time. Try to use only one at a time.
Either remove #Transactional annotation or remove the transaction statements from your method.

Spring autowire null value

I have a problem with autowiring component with spring. Im trying inject bean (UserRepository) into java class Reader. When im using autowire annotation the object is allways null. I try add into spring-config.xml bean 'reader' whith property 'userRepository' but this not working too. What is missing?
spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="reader" />
<context:annotation-config/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.sqlite.JDBC"/>
<property name="url" value="jdbc:sqlite:C:/Users/Piotr/Downloads/projekt na projekt/IntelliDOM/IntelliDOM.sqlite"/>
<property name="username" value=""/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userRepository" class="repository.UserRepositoryHibernate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
Reader.java:
#Service
#Path("/")
public class Reader {
#Autowired
private UserRepository userRepository;
#GET
#Path("/test")
public void test(){
System.out.println("test");
userRepository.findAll(); //this is allways null
}
#POST
#Path("/dom2")
public Response read(String nonParsedWords){
Words parsedWords = WordParser.parseWords(nonParsedWords);
for(String word : parsedWords.getWords()){
System.out.println(word);
}
return Response.status(200).entity(nonParsedWords).build();
}
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
Is there something wrong with context of application? Why this bean cant not be injected?
Spring version: 2.5.6
EDIT:
hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:IntelliDOM.sqlite</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="entity.User"/>
</session-factory>
</hibernate-configuration>

Spring AnnotationSessionFactoryBean NullPointerException

I am a newbie to Spring and trying to integrate Spring and Hibernate. But I am facing nullpointer in SessionFactory.
Error Description:
java.lang.NullPointerException
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:29)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:34)
com.ume.LoginController.getUserCredentials(LoginController.java:39)
Another Error Description
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:544)
com.ume.dao.UserDaoImpl.openSession(UserDaoImpl.java:31)
com.ume.dao.UserDaoImpl.getUser(UserDaoImpl.java:37)
com.ume.service.UserServiceImpl.getUser(UserServiceImpl.java:23)
My dispatchar-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="com.nptest" />
<mvc:annotation-driven />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource">
<property name="basename" value="messages" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.nptest.pojo.UserPojo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>
<bean id="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
My DAOImplementation class
#Repository
public class UserDaoImpl implements UserDao{
#Autowired
private SessionFactory sessionFactory;
private Session openSession()
{
return sessionFactory.getCurrentSession();
}
public UserPojo getUser(String username, String password)
{
List<UserPojo> userlist=new ArrayList<UserPojo>();
Query query=openSession().createQuery("from UserPojo u WHERE u.login=:username AND u.userpwd=:password");
query.setParameter("login",username);
query.setParameter("password",password);
System.out.println("MADAN Query "+ query);
userlist=query.list();
if(userlist.size()>0)
return userlist.get(0);
else return null;
}
}
My Service Interface
public interface UserService {
public UserPojo getUser(String username,String password);
}
My Service Implementation class
#Service
public class UserServiceImpl implements UserService {
#Autowired
private UserDao userDao;
#Transactional
public UserPojo getUser(String username, String password) {
return userDao.getUser(username, password);
}
My controller class from where I make these call, I have instantiated the Service class by this
#Autowired
private UserService userservice;
And in the method I called
UserPojo user=userservice.getUser(username, password);
looks like you have instantiated a UserDaoImpl manually, the sessionFactory didn't get wired, please post the code how you use UserDaoImpl
add annotation to UserDaoImpl and put it under package com.nptest
package com.nptest.dao;
#Repository public class UserDaoImpl implements UserDao
then autowired UserDaoImpl in your app
--edit--
to enable annotation based transcation, you have to add
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
in the application context xml

Creating persistance layer with spring/hibernate

I'm trying to create a persistance project so it can be re-used by some other projects I'm building on top. I.e it will be used by a web service/spring mvc project and by standalone jar which does some file processing.
I've used hibernate with spring mvc before but never as a standalone executable java jar so I have everything setup and working(application context) :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- Enable annotation style of managing transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Root Context: defines shared resources visible to all other web components -->
<!-- HIBERNATE -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:spring.properties" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.databaseurl}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="acquireIncrement" value="5" />
<property name="idleConnectionTestPeriod" value="60"/>
<property name="maxPoolSize" value="100"/>
<property name="maxStatements" value="50"/>
<property name="minPoolSize" value="10"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list> <value>com/project/utility/persistence/mapping/TestProp.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- END HIBERNATE -->
</beans>
When I test it from main class everything looks ok with mapping/dependencies :
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appCtx.xml");
}
What I want to do next is to build few dao classes which will get some data and I'd build some interface above that so it can be re-used by both webservice and processing tool as a jar(maven dependency).
In order to build dao classes I need sessionFactory to be != null always. How do I do this?
There are many approaches to this. One solution I use is to use the javax.persistence.PersistenceContext annotation. Spring will respect this annotation and inject a proxy to a thread local EntityManager. Provided your DAO is created by Spring this allows access to the entity manager from within your DAO.
public class DAO {
private EntityManager entityManager;
#PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
#Repository
public class MyDAO {
#Autowired
private SessionFactory sessionFactory;
// ...
}
and add the MyDAO bean to the context XML file, or simply add the following lines to this file:
<context:annotation-config />
<context:component-scan base-package="one.of.the.parent.packages.of.your.dao" />

Categories