NoSuchBeanDefinitionException when using Spring and Hibernate - spring

I get a NoSuchBeanDefinitionException when trying to run my program (: No bean named 'org.springframework.orm.hibernate4.LocalSessionFactoryBean' is defined) Yet the bean has been defined in the Beans2.xml file. The classes are listed below. Thanks.
PieceHibernateDAO.java
package sample.spring.chapter01;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.hibernate.SessionFactory;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.TransactionStatus;
//import org.springframework.transaction.TransactionCallback;
//import org.springframework.transaction.TransactionTemplate;
#Repository(value="PieceHibernateDAO")
public class PieceHibernateDAO {
#Autowired
private SessionFactory sessionFactory;
public int createPieceHibernate(final Piece piece) {
sessionFactory.getCurrentSession().save(piece);
return piece.getpieceid();
}
//public Piece getPiece
public void fill(Resource resource) throws FileNotFoundException, IOException{
InputStream is=resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
CSVParser parser = CSVFormat.DEFAULT.withHeader().parse(br);
for (CSVRecord record:parser) {
String ref=record.get("référence ascensoriste").trim();
String asc=record.get("ascensoriste").trim();
//String desc=record.get("Description article").trim();
String prix=record.get("Pv");
String category=record.get("Familie").trim();
//System.out.println(category);
Piece lift_comp=new Piece();
lift_comp.setasc(asc);
//lift_comp.setdesc(desc);
lift_comp.setref(ref);
lift_comp.setprice(prix);
lift_comp.settype(category);
lift_comp.setinfo();
//listp.add(lift_comp);
int cpid=lift_comp.getcompanyid(asc);
lift_comp.setcompanyid(cpid);
//sessionFactory.getCurrentSession().save(lift_comp);
createPieceHibernate(lift_comp);
}
}
}
Beans2.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"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="sample.spring" />
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/liftcomp_db" />
<property name="username" value="root" />
<property name="password" value="8K887h))00" />
</bean>
<bean id="PieceHibernateDAO"
class="sample.spring.chapter01.PieceHibernateDAO">
<property name="sessionFactory" ref="org.springframework.orm.hibernate4.LocalSessionFactoryBean"/>
</bean>
<bean id="PieceDAO" class="sample.spring.chapter01.PieceDAO">
<property name="listp" ref="Piece"></property>
</bean>
<bean id = "Company" class = "sample.spring.chapter01.Company"/>
<bean id = "Family" class = "sample.spring.chapter01.Family"/>
<bean id = "Piece" class = "sample.spring.chapter01.Piece"/>
</beans>

You create a bean with id sessionFeactory. this must be the value in the ref Attribute:
<property name="sessionFactory" ref="sessionFactory"/>
because this is the id of the bean.

Related

Unable to insert record into DB using hibernate integrated with Spring using XML

I am trying to implement a signup page using Spring MVC integrated with hibernate using XML.
Application Context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
xmlns:tx="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="mysessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<property name="template" ref="template"></property>
</bean>
</beans>
Controller Class
package com.uday;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class ControllerSignUp_Login {
public ControllerSignUp_Login() {
// TODO Auto-generated constructor stub
}
#RequestMapping("/hello")
public String diaplay(#RequestParam("name") String name, #RequestParam("pass") String pass,Model m) {
Login_DetailsDao dao = (Login_DetailsDao) getBean();
if(dao.isLogoinSuccessfull(name , pass)) {
m.addAttribute("message", "Hello"+name);
return "Success";
}
else {
m.addAttribute("message", "You have Entered Wrong pin");
return "Failure";
}
}
#RequestMapping("/SignUp")
public String redirect() {
System.out.println("ControllerSignUp_Login.display()");
return "signup";
}
#RequestMapping("/login")
public String display() {
System.out.println("ControllerSignUp_Login.display()");
return "login";
}
#RequestMapping("/updateDetails")
#Transactional
public String display(HttpServletRequest req , Model M) {
String firstName=req.getParameter("firstName");
String lastName=req.getParameter("lastName");
String mobileNo=req.getParameter("mobileNo");
String address=req.getParameter("address");
String city=req.getParameter("city");
String password=req.getParameter("password");
Login_DetailsDao dao = (Login_DetailsDao) getBean();
if(checkLength(firstName) && checkLength(lastName) && checkLength(mobileNo) && checkLength(address) && checkLength(city) && checkLength(password)) {
Login_Details ld = new Login_Details();
ld.setFirstName(firstName);
ld.setLastName(lastName);
ld.setCity(city);
ld.setAddress(address);
ld.setMobileNo(mobileNo);
ld.setPassword(password);
dao.saveEmployee(ld);
M.addAttribute("message", "SignUp Successfull !! Thank You");
M.addAttribute("displayLogin", true);
return "Success";
}
else {
M.addAttribute("message","SignUp Failed !! All details are mandatory.");
return "signup";
}
}
public boolean checkLength(String s) {
if(s != null && s.length() > 0) {
return true;
}
return false;
}
public Object getBean() {
ApplicationContext appcontext = new ClassPathXmlApplicationContext("Applicationcontext.xml");
Login_DetailsDao lDDao =(Login_DetailsDao)appcontext.getBean("d");
return lDDao;
}
}
DAO class
package com.uday;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.transaction.annotation.Transactional;
public class Login_DetailsDao {
HibernateTemplate template;
HibernateTransactionManager hbt;
public HibernateTransactionManager getHbt() {
return hbt;
}
public void setHbt(HibernateTransactionManager hbt) {
this.hbt = hbt;
}
public Login_DetailsDao() {
// TODO Auto-generated constructor stub
}
#Transactional
public void saveEmployee(Login_Details e){
System.out.println("Login_DetailsDao.saveEmployee()"+e.getMobileNo());
SessionFactory sf = hbt.getSessionFactory();
Session session =sf.getCurrentSession();
Transaction t =session.beginTransaction();
session.persist(e);
t.commit();
session.close();
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public List<Login_Details> getEmployees(){
List<Login_Details> list=new ArrayList<Login_Details>();
list=template.loadAll(Login_Details.class);
return list;
}
}
I am able to get the login_details data but unable to insert the record.
getting below exception
org.xml.sax.SAXParseException; lineNumber: 51; columnNumber: 29; cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'.
Could any one please suggest to solve this?
Your code is flawed on multiple levels.
Never create a BeanFactory or ApplicationContext just because you need an instance of a bean. Instead use dependency injecting for that bean.
The fact that you use a BeanFactory over an ApplicationContext is one of the reasons your code doesn't work. See this for why you shouldn't use a BeanFactory.
You aren't using transactions and without transactions nothing will be persisted in the database.
Now first in your controller you need to dependency inject the Login_DetailsDao instead of creating a BeanFactory and obtain an instance.
#Controller
public class ControllerSignUp_Login {
private final Login_DetailsDao dao;
#Autowired // If using Spring 4.3 or later this isn't needed
public ControllerSignUp_Login(Login_DetailsDao dao) {
this.dao=dao;
}
}
Now in your method use this instance, instead of doing the lookup.
Your Login_DetailsDao should operate on the SessionFactory and nothing else.
public class Login_DetailsDao {
private final SessionFactory sf;
public Login_DetailsDao(SessionFactory sf) {\
this.sf=sf;
}
#Transactional
public void saveEmployee(Login_Details e){
sf.getCurrentSession().save(e);
}
#Transactional(readOnly=true)
public List<Login_Details> getEmployees(){
return sf.getCurrentSession()
.createQuery("SELECT ld FROM Login_Details", Login_Details.class)
.getResult();
}
}
That is all you need. Now finally in your XML tie all this together.
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
xmlns:tx="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<constructor-arg ref="sessionFactory" />
</bean>
</beans>
One simple change made this application worked.
1 > Deleted applicationcontext.xml and added the contents in disapatcher-servlet.xml
disaptcher-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: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-3.0.xsd">
<context:annotation-config/>
<tx:annotation-driven />
<!-- Provide support for component scanning -->
<context:component-scan base-package="com.uday" />
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:#localhost:1521:xe"></property>
<property name="username" value="hr"></property>
<property name="password" value="umashetti123"></property>
</bean>
<bean id="mysessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>Login_Detailshbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="checkWriteOperation" >false</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="mysessionFactory" />
</bean>
<bean id="d" class="com.uday.Login_DetailsDao">
<constructor-arg ref="mysessionFactory" />
</bean>
</beans>
DAO Class:
package com.uday;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
public class Login_DetailsDao {
private final SessionFactory sessionFactory;
public Login_DetailsDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
#Transactional
public void saveEmployee(Login_Details e){
sessionFactory.getCurrentSession().save(e);
}
public boolean isLogoinSuccessfull(String uName , String password) {
List<Login_Details> a= getEmployees();
Iterator<Login_Details> i = a.iterator();
while(i.hasNext()) {
Login_Details ld = i.next();
String mobileNo = ld.getMobileNo();
String pas = ld.getPassword();
if(mobileNo != null && mobileNo.equals(uName) && pas != null && pas.equals(password)) {
return true;
}
}
return false;
}
#Transactional
public List<Login_Details> getEmployees(){
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Login_Details.class);
List<Login_Details> l = criteria.list();
return l;
}
}
Controller Class:
package com.uday;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
#Controller
public class ControllerSignUp_Login {
#Autowired
private final Login_DetailsDao dao;
#Autowired
public ControllerSignUp_Login(Login_DetailsDao login_DetailsDao) {
this.dao = login_DetailsDao;
}
#RequestMapping("/hello")
#Transactional
public String diaplay(#RequestParam("name") String name, #RequestParam("pass") String pass,Model m) {
if(dao.isLogoinSuccessfull(name , pass)) {
m.addAttribute("message", "Hello"+name);
return "Success";
}
else {
m.addAttribute("message", "You have Entered Wrong pin");
return "Failure";
}
}
#RequestMapping("/SignUp")
public String redirect() {
System.out.println("ControllerSignUp_Login.display()");
return "signup";
}
#RequestMapping("/login")
public String display() {
System.out.println("ControllerSignUp_Login.display()");
return "login";
}
#RequestMapping("/updateDetails")
#Transactional
public String display(HttpServletRequest req , Model M) {
String firstName=req.getParameter("firstName");
String lastName=req.getParameter("lastName");
String mobileNo=req.getParameter("mobileNo");
String address=req.getParameter("address");
String city=req.getParameter("city");
String password=req.getParameter("password");
if(checkLength(firstName) && checkLength(lastName) && checkLength(mobileNo) && checkLength(address) && checkLength(city) && checkLength(password)) {
Login_Details ld = new Login_Details();
ld.setFirstName(firstName);
ld.setLastName(lastName);
ld.setCity(city);
ld.setAddress(address);
ld.setMobileNo(mobileNo);
ld.setPassword(password);
dao.saveEmployee(ld);
M.addAttribute("message", "SignUp Successfull !! Thank You");
M.addAttribute("displayLogin", true);
return "Success";
}
else {
M.addAttribute("message","SignUp Failed !! All details are mandatory.");
return "signup";
}
}
public boolean checkLength(String s) {
if(s != null && s.length() > 0) {
return true;
}
return false;
}
}
Project Structure looks like below
Instead of getting current session and making the transaction opened new session that's it worked like charm!!!
I don't know why it is not working if I am getting current session!!
#Transactional
public void saveEmployee(Login_Details e){
System.out.println("Login_DetailsDao.saveEmployee()"+e.getMobileNo());
SessionFactory sf = hbt.getSessionFactory();
//Changed here
Session session =sf.openSession(); //Session session =sf.getCurrentSession();
Transaction t =session.beginTransaction();
session.persist(e);
t.commit();
session.close();
}

HibernateException: No CurrentSessionContext configured (Hibernate4)

I have been working on various issues to get this Spring-Hibernate app to work. I have probably made a basic error.
This seems to be a recurring question with many answers covering older versions of Hibernate. I am using Spring 4.3.9 and Hibernate 4.0.5
The UT gets a SessionFactory but getCurrentSession() is returning null and in debug I can see that currentSessionContext is null.
applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="data.xml"/>
</beans>
data.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
>
<!-- Enable autowiring -->
<tx:annotation-driven/>
<!-- context:annotation-config/-->
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan" value="com.my"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.connection.pool-size">1</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
</beans>
SpringConfig.java
package utils.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;
#Configuration
#Import
(
{SpringRepositoryConfig.class
}
)
#ImportResource("classpath:configuration/applicationContext.xml")
public class SpringConfig
{
}
SpringRepositoryConfig.java
package utils.config;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import utils.persistence.ItemRepository;
import utils.persistence.HibernateItemRepositoryImpl;
import org.springframework.beans.factory.annotation.Autowired;
// Declare as a configuration class
#Configuration
public class SpringRepositoryConfig
{
#Autowired
SessionFactory sessionFactory;
// Define repository bean
#Bean
public ItemRepository itemRepository()
{
ItemRepository rep = new HibernateItemRepositoryImpl();
rep.setSessionFactory(sessionFactory);
return rep;
}
}
SpringServicesConfig.java
package utils.config;
import javax.inject.Inject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import bussysutils.persistence.ItemRepository;
import bussysutils.repository.DecoderLoadRepository;
import bussysutils.repository.DecoderLoadRepositoryImpl;
#Configuration
public class SpringServicesConfig
{
#Inject
ItemRepository repository;
#Bean
public DecoderLoadRepository decoderLoadRepository()
{
DecoderLoadRepositoryImpl decoderLoad = new DecoderLoadRepositoryImpl(repository);
return decoderLoad;
}
}
SessionConfig.java
package utils;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class SessionConfig
{
public static SessionFactory buildFactory(String url
,String user
,String password)
{
ServiceRegistry sr;
SessionFactory sfactory;
Configuration config = new Configuration();
config.setProperty("hibernate.connection.url", url);
config.setProperty("hibernate.connection.username", user);
config.setProperty("hibernate.connection.password", password);
StandardServiceRegistryBuilder ssrb
= new StandardServiceRegistryBuilder().applySettings(config.getProperties());
sr = ssrb.build();
try
{
sfactory = config.buildSessionFactory(sr);
}
catch (Throwable ex)
{
throw new ExceptionInInitializerError(ex);
}
return sfactory;
}
}
UT_DecoderLoad.java
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import utils.DecoderLoad;
import utils.SessionConfig;
import utils.config.SpringConfig;
import utils.persistence.HibernateItemRepositoryImpl;
import utils.persistence.ItemRepository;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
#ContextConfiguration(classes = SpringConfig.class)
#RunWith(SpringJUnit4ClassRunner.class)
public class UT_DecoderLoad
{
#Autowired
SessionFactory sessionFactory;
#Test
public void decoderLoadTest()
{
try
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
sessionFactory = SessionConfig.buildFactory("jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-host-name.my.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=dev)))"
,"myuser"
,"mypassword");
}
catch (SQLException e)
{
System.err.println("uploadServlet ERROR - " + e.getMessage());
System.out.print("uploadServlet logon ERROR - SQLException: " + e.getMessage());
e.printStackTrace();
}
Session s = sessionFactory.getCurrentSession(); /// <<<<<<<<
s.beginTransaction();
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
System.out.println(decoderLd.getSiteRefNo());
// update the database
ItemRepository itemRepo = new HibernateItemRepositoryImpl();
// itemRepo.create(decoderLd);
s.save(decoderLd);
s.getTransaction().commit();
}
}
You are making things very complicated for yourself, first you are mixing XML and Java based configuration and you have a configuration class for single beans. Either use Java or XML but don't mix them, especially if you aren't sure what those things do.
Your DataSource setup is also flawed as it is a partially setup DataSource.
Next your SessionFactory configuration is basically useless due to the use of the SessionConfig (which basically renders your Spring configuration useless). You shouldn't be using the SessionConfig, so drop it. Your SessionFactory configuration in the XML is also flawed, the hibernate.connection properties don't do anything, due the the injected DataSource and you shouldn't be messing around with the hibernate.current_session_context property unless you are using JTA. Spring will manage it for you.
Your unit test is also flawed, you should be injecting the repository not creating a new instance yourself.
That being said and moving everything to xml your data.xml should look something like this.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context">
<tx:annotation-driven/>
<context:property-placeholder location="jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.my"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernatTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="itemRepository" class="utils.config.HibernateItemRepositoryImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="decoderLoadRepository" class="utils.config.DecoderLoadRepositoryImpl">
<constructor-arg ref="itemRepository" />
</bean>
</beans>
The jdbc.properties would contain the following
jdbc.url=jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my-host-name.my.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=dev)))
jdbc.username=myuser
jdbc.password=mypassword
You can now drop your SessionConfig, SpringServicesConfig and SpringRepositoryConfig classes as the first you shouldn't have in the first place the config class are obsolete due to adding the context to the xml file.
Now your test is also flawed as it should be #Transactional and should load the XML file instead of the java config.
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad {
#Autowired
SessionFactory sessionFactory;
#Test
public void decoderLoadTest() {
Session s = sessionFactory.getCurrentSession(); /// <<<<<<<<
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
System.out.println(decoderLd.getSiteRefNo());
s.save(decoderLd);
s.flush(); // "simulate a commit"
}
}
Although not sure what you are testing here, you should probably be testing the HibernateItemRepositoryImpl.
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad {
#Autowired
SessionFactory sessionFactory;
#Autowired
ItemRepository repository
#Test
public void decoderLoadTest() {
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
repository.create(decoderLd);
sessionFactory.getCurrentSession().flush(); // "simulate a commit"
// Validate the existence in the database
}
}
This is my current version, it runs but does not update the db table.
data.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
>
<!-- Enable autowiring -->
<tx:annotation-driven/>
<context:property-placeholder location="resources/jdbc.properties"/>
<bean id="DataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource"/>
<property name="packagesToScan" value="utils"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.cache.provider_cache">org.hibernate.cache.NoCacheProvider</prop>
<prop key="show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="itemRepository" class="utils.persistence.HibernateItemRepositoryImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="decoderLoadRepository" class="utils.repository.DecoderLoadRepositoryImpl">
<constructor-arg ref="itemRepository" />
</bean>
</beans>
UT_DecoderLoad.java
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import utils.DecoderLoad;
import utils.persistence.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
#ContextConfiguration("classpath:configuration/applicationContext.xml")
#RunWith(SpringJUnit4ClassRunner.class)
#Transactional
public class UT_DecoderLoad
{
#Autowired
SessionFactory sessionFactory;
#Autowired
ItemRepository itemRepo;
#Test
public void decoderLoadTest()
{
DecoderLoad decoderLd = new DecoderLoad();
decoderLd.setSiteRefNo("123456");
decoderLd.setDecoderNo("999");
System.out.println(decoderLd.getSiteRefNo());
itemRepo.create(decoderLd);
sessionFactory.getCurrentSession().flush();
}
}
I added a primary key column into the db table so that I could add the required #Id
DecoderLoad.java
package utils;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
#Entity
#Table(name = "Decoder_Load")
public class DecoderLoad
{
public DecoderLoad()
{
}
// pojo with annotations
#Id
#Column(name = "decoder_no")
private String decoderNo;
...
I also added javassist to the pom.
Maven compile complains:
package org.springframework.test.context does not exist
package org.springframework.test.context.junit4 does not exist
This simple test runs but it is not inserting a record without forcing it and causing other issues.
EDIT: just learned that a test does a rollback so it will not commit.

Null Pointer with Spring #Autowired

I am new to Spring & Hibernate and I have spent a couple of weeks trying to get to grips with it. Most of my experience is with Oracle development so this is a departure for me. Hibernate is working but Spring is giving me a headache.
I tried following this tutorial but using an Oracle db and my Address table.
http://www.codejava.net/frameworks/spring/spring-4-and-hibernate-4-integration-tutorial-part-1-xml-configuration
The web page wasn't working so I tried a test case on the HomeController.java That didn't work so but I believe I have narrowed it down to a null pointer in addressDao and so I wrote a test case for that to try to pin it down.
java.lang.NullPointerException: null
springexp.unittest.AddressDaoTest.testListAddr(AddressDaoTest.java:29)
package springexp.unittest;
import bussys.model.Address;
import springexp.dao.AddressDao;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
public class AddressDaoTest
{
#Autowired
private AddressDao addressDao;
#RequestMapping(value="/")
/**
* #see springexp.dao.AddressDao#listAddr()
*/
#Test
public void testListAddr()
{
List<Address> listAddresses = addressDao.listAddr(); << error thrown here
System.out.println(listAddresses.get(1).getFirstname());
fail("Unimplemented");
}
}
I have also spent a long time reading stackoverflow and others to try to fix the null pointer issue but there seems to be lots of ways of writing this and it has changed in various versions.
In servlet-context.xml I have this:
<!-- Hibernate Session Factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>springexp.dao.AddressDao</value>
</list>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="addressDao" class="springexp.dao.AddressDaoImpl">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
<!-- added this recently based on one suggestion -->
<bean id="addressDaoTest" class="springexp.unittest.AddressDaoTest">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
AddressDaoImpl.java
package springexp.dao;
import bussys.model.Address;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
public class AddressDaoImpl
implements AddressDao
{
private SessionFactory sessionFactory;
public AddressDaoImpl(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}
#Override
#Transactional
public List<Address> listAddr()
{
#SuppressWarnings("unchecked")
List<Address> listAddress = (List<Address>)
sessionFactory.getCurrentSession().createCriteria(Address.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
return listAddress;
}
}
I hope this is enough to give a clue to my mistake(s).

Entity attribute not passed to sql statement with hibernateTemplate

I am learning spring data access and I've tried to insert data through hibernateTempate. Here is my code:
UPDATED
applicationContext.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.eric.mvnlab"/>
<context:property-placeholder properties-ref="properties"/>
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages"/>
<property name="defaultEncoding" value="${source.encoding}"/>
</bean>
<!-- DAO layer -->
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
...
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="packagesToScan" value="com.eric.mvnlab.model.*" />-->
<property name="annotatedClasses">
<list>
<value>com.eric.mvnlab.model.Machine</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
MachineDAO.java
package com.eric.mvnlab.server.dao.impl;
import com.eric.mvnlab.model.Machine;
import org.springframework.stereotype.Repository;
#Repository("machineDao")
public class MachineDAO extends GenericHibernateDAOImpl<Machine, Integer> {
public void addMachine(Machine machine) {
getHibernateTemplate().save(machine);
}
public Machine findById(int id) {
return (Machine) getHibernateTemplate().get(Machine.class, id);
}
}
Main.java
package com.eric.mvnlab;
import com.eric.mvnlab.model.Machine;
import com.eric.mvnlab.server.dao.impl.MachineDAO;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created with IntelliJ IDEA.
* User: eric
* Date: 9/25/12
* Time: 3:15 PM
* To change this template use File | Settings | File Templates.
*/
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MachineDAO machineDAO = (MachineDAO)context.getBean("machineDao");
machine.setHostname("MyLaptop");
machine.setIpaddress("127.0.0.1");
machineDAO.addMachine(machine);
}
}
Machine.java
package com.eric.mvnlab.model;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
#Entity
#Table(name="MACHINE")
public class Machine {
#Column(name="MID")
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int mid;
#Column(name="HOSTNAME")
private String hostname;
#Column(name="IPADDRESS")
private String ipaddress;
public String getIpaddress() {
return ipaddress;
}
public void setIpaddress(String ipaddress) {
this.ipaddress = ipaddress;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getMid() {
return mid;
}
public void setMid(int machineId) {
this.mid = machineId;
}
}
Table Machine has three columns: mid, hostname and ipaddress. mid is the primary key and is auto increment.
When I run Main, I got below output:
Hibernate: insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?)
Exception in thread "main" org.springframework.dao.InvalidDataAccessResourceUsageException: could not insert: [com.eric.mvnlab.model.Machine]; SQL [insert into MACHINE (MID, HOSTNAME, IPADDRESS) values (null, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not insert: [com.eric.mvnlab.model.Machine]
WARN - JDBCExceptionReporter - SQL Error: -798, SQLState: 428C9
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:629)
ERROR - JDBCExceptionReporter - DB2 SQL Error: SQLCODE=-798, SQLSTATE=428C9, SQLERRMC=MID, DRIVER=4.7.85
Could anyone tell me why the entity attribute data is not passed to sql statement?
NOTE:
If you are using DB2 9.7, and you are likely to met this error:
org.hibernate.HibernateException: The database returned no natively generated identity value
It's a DB2 jdbc driver bug, and the solution is to use a later version driver, such as 10.1
Note that you configured Hibernate to use HSQLDB dialect, whereas your error message comes from DB2. You need to configre a dialect that matches your DBMS, see 3.4.1. SQL Dialects.

Spring 3.1 JPA not inserting data while running in tomcat

I have spent three days trying to find the solution to this problem to no avail. I am desperate to figure this out. I have a simple spring app, running in servlet 2.5 with jstl tags 1.2, running in tomcat with spring 3.1, using hibernate and the hibernate jpa implementation.
I can list data from a page, but I cannot complete an insert. The record goes back, it appears to fire through with no problems. Yet no insert takes place. I know there are other posts similar but I have looked through them all and have not been able to find a solution anywhere.
If I run the exact same code via a MAIN class, the insert works fine. It just does not work when running as a web-app in tomcat.
I have tried running this via the main which works, inside the controller I have skipped calling the service layer, trying to go directly to the interface, when that didnt work, I tried going directly to the implementing DAO class, and that didnt work. It appears via the spring logs, that the entity manager is getting created, and shut down before a transaction takes place.
Please help me.
Here is my App-context
<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: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-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.naturalbornliar.site"/>
<tx:annotation-driven />
<!-- Bean declarations go here-->
<bean id="duke" class="com.naturalbornliar.site.entity.Admin">
<constructor-arg name="admin_id" type="Long" value="15" />
<constructor-arg name="admin_login" type="String" value="testUser" />
<constructor-arg name="admin_pwd" type="String" value="testPwd" />
<constructor-arg name="email_id" type="int" value="15" />
<constructor-arg name="quote" type="String" value="Something to say here" />
</bean>
<!-- <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/nbl_db"/> -->
<!-- <property name="username" value="web_user"/> -->
<!-- <property name="password" value="web_pwd"/> -->
<!-- <property name="initialSize" value="5"/> -->
<!-- <property name="maxActive" value="10"/> -->
<!-- </bean> -->
<bean id="simpledataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/nbl_db"/>
<property name="username" value="web_user"/>
<property name="password" value="web_pwd"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="simpledataSource"/>
</bean>
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="nblPersistenceUnit"/>
<property name="dataSource" ref="simpledataSource"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
</bean>
<!-- <bean id="emf" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> -->
<!-- <property name="persistenceUnitName" value="nblPersistenceUnit"/> -->
<!-- <property name="dataSource" ref="simpledataSource"/>-->
<!-- <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> -->
<!-- </bean> -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL"/>
<property name="showSql" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* *..CategoryDaoImpl.*(..))" advice-ref="txAdvice"/>
</aop:config>
</beans>
Here is my controller:
package com.naturalbornliar.site.mvc;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.naturalbornliar.site.entity.Category;
import com.naturalbornliar.site.entity.Link;
import com.naturalbornliar.site.service.CategoryService;
#Controller
#RequestMapping("/categories")
public class CategoryController {
protected final Logger logger = Logger.getLogger(CategoryController.class);
private final CategoryService categoryService;
#Inject
public CategoryController(CategoryService categoryService){
this.categoryService = categoryService;
}
#RequestMapping(value="/listCategories")
public String listLinks(Model model){
model.addAttribute("categories", categoryService.getAllCategories());
return "categories";
}
#RequestMapping(method=RequestMethod.GET, params="new")
public String showCreateCategoryForm(Model model){
model.addAttribute(new Category());
return "addcategory";
}
#RequestMapping(method=RequestMethod.POST)
public String addCategoryFromForm(Category category, BindingResult bindingResult){
if(bindingResult.hasErrors()){
return"addcategory";
}
categoryService.addCategory(category);
return "redirect:/categories/listCategories";
}
}
Here is my service called from the controller:
package com.naturalbornliar.site.service;
import java.util.Collection;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import com.naturalbornliar.site.entity.Category;
import com.naturalbornliar.site.i.ICategoryDao;
#Service
public class CategoryService {
private ICategoryDao iCategoryDao;
#Inject
public CategoryService(ICategoryDao iCategoryDao){
this.iCategoryDao = iCategoryDao;
}
public Collection<Category> getAllCategories(){
return iCategoryDao.getAllCategories();
}
public Collection<Category> getCategoriesByType(String type) {
return iCategoryDao.getCategoriesByType(type);
}
public Category getCategoryById(Long id) {
throw new UnsupportedOperationException();
}
public void deleteCategory(Category category) {
throw new UnsupportedOperationException();
}
public void updateCategory(Category category) {
throw new UnsupportedOperationException();
}
public void inactivateCategory(Category category){
throw new UnsupportedOperationException();
}
public void addCategory(Category category){
iCategoryDao.addCategory(category);
}
}
Here is my implementing DAO:
package com.naturalbornliar.site.dao;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.naturalbornliar.site.entity.Content;
import com.naturalbornliar.site.i.IContentDao;
#Transactional
#Repository
public class ContentDaoImpl implements IContentDao {
protected final Logger logger = Logger.getLogger(ContentDaoImpl.class);
#PersistenceContext
private EntityManager em;
public EntityManager getEm() {
return em;
}
public void setEm(EntityManager em) {
this.em = em;
}
#Override
public void addContent(Content content) {
// TODO Auto-generated method stub
em.persist(content);
}
#Override
public void deleteContent(Content content) {
// TODO Auto-generated method stub
em.remove(content);
}
#Override
public void inactivateContent(Content content) {
// TODO Auto-generated method stub
}
#Override
public Content getContentById(Long id) {
// TODO Auto-generated method stub
return null;
}
#Override
public Content getContentByName(String name) {
// TODO Auto-generated method stub
return null;
}
#Override
public Collection<Content> getAllObjects() {
List<Content> resultList = em.createQuery("FROM Content", Content.class).getResultList();
return resultList;
}
}
Here is the interface (just in case)
package com.naturalbornliar.site.i;
import java.util.Collection;
import com.naturalbornliar.site.entity.Content;
public interface IContentDao {
public void addContent(Content content);
public void deleteContent(Content content);
public void inactivateContent(Content content);
public Content getContentById(Long id);
public Content getContentByName(String name);
public Collection<Content> getAllObjects();
}
Jeremy, thay may be many problems with that and I had similar problem.
In my case I used tomcat and I needed to add spring weaver to tomcat (this problem is described here: http://asrijaffar.blogspot.com/2007/02/spring-jpa-tomcat.html).
In my case I needed to have:
<tx:annotation-driven proxy-target-class="true" />
In DispatcherServlet config.
Additionally in db-context config:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="jpatest" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
Well I actually solved the problem. Here is the solution, there was another stackoverflow issue that solved it:
Spring #Transaction not starting transactions
Basically all that was needed for transactions to fire correctly was for me to add:
<tx:annotation-driven proxy-target-class="true"/>
to my servlet xml. I guess if you just have it in the application config.xml it only works when running under the main class (like in a standalone) if you need to run it in a container you have to declare transaction annotations in the servlet as well.
I also spent several hours trying to figure out where the problem is although my previous application with the same stack is working fine and I wasn't able to understand the difference.
And.... the error was in tag in spring-servlet.xml - it was defined to scan the root package with all the web controller, repository classes, etc.
After changing it to make scanning of the package with web controllers only, the problem was gone..
Just for you (and for me) in case you might meet the same problem, just an additional hint
I also had this same problem, spent couple nights searching - alex solutions saved me - in servlet xml I changed context:component-scan to scan only package with web controller.
Example from this page should look like
<context:component-scan base-package="com.naturalbornliar.site.mvc"/>

Resources