Annotation #Transactional does not working - spring

I am trying to use the annotation #Transactional to access my MySQL using Hibernate, Spring and JSF. My problem is:
When I use the annotation #Transactional at my managedBean to make a query I got this error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:65)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:687)
at br.com.rpg.DAO.AbstractDAO.getCurrentSession(AbstractDAO.java:14)
at br.com.rpg.DAO.CountryDAO.findAll(CountryDAO.java:14)
at br.com.rpg.managedBeans.SignupBean.init(SignupBean.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
I don't know what I'm doing wrong. My code and xml config is:
application-config.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.rpg.DO" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
<context:component-scan base-package="br.com.rpg" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
My MB
#Named
#Scope("request")
public class SignupBean implements Serializable {
private static final long serialVersionUID = 1787096549063029840L;
#Inject
private CountryDAO country;
#Inject
private UserDAO user;
private Map<String, Integer> countries;
private Integer selected;
private String username;
private String password;
#PostConstruct
#Transactional
public void init() {
List<CountryDO> findAll = country.findAll();
countries = new HashMap<String, Integer>();
for (CountryDO countryDO : findAll) {
countries.put(countryDO.getName(), countryDO.getId());
}
}
public Map<String, Integer> getCountries() {
return countries;
}
public void setCountries(Map<String, Integer> countries) {
this.countries = countries;
}
public Integer getSelected() {
return selected;
}
public void setSelected(Integer selected) {
this.selected = selected;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
My Country DAO has the #Named annotation. Someone could help me?
Thx.

you have applied #PostConstruct and #Transactional on init method. init method would be called before applying any AOP Proxy interceptor is applied because of #PostConstruct. Hence at the time of invoking init there is no transaction proxy applied. if you need to call init method on Application startup use ApplicationEvent

Related

Why is my #Autowired field null here?

This is not a duplicate of this question. So please don't close it for "is duplicate of" reasons..
I am trying to autowire a private field in my service class using this tutorial. My problem is that restaurantOwnerRepository remains null and does not get initialized.
servlet-context.xml
<context:component-scan base-package="com.mahlzeit.web.server" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="restaurantOwnerRepository" class="com.mahlzeit.web.server.dao.RestaurantOwnerRepository">
<constructor-arg>
<ref bean="sessionFactory" />
</constructor-arg>
</bean>
Service code:
#Component
public class RestaurantInformationServiceImpl extends XsrfProtectedServiceServlet implements RestaurantInformationService {
private static final long serialVersionUID = -4088840947018614411L;
#Autowired
private RestaurantOwnerRepository restaurantOwnerRepository;
private final static Logger logger = Logger.getLogger(RestaurantInformationServiceImpl.class);
#Override
public List<RestaurantDTO> getAvailableRestaurants() {
// restaurantOwnerRepository is 'null'
List<Restaurant> availableRestaurants = restaurantOwnerRepository.getAvailableRestaurants(getSessionId());
return null;
}
private String getSessionId() {
HttpServletRequest httpRequest = getThreadLocalRequest();
return httpRequest.getSession().getId();
}
}
RestaurantOwnerRepository.java
public class RestaurantOwnerRepository implements RestauranOwnerDAO {
private SessionFactory sessionFactory;
public RestaurantOwnerRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
// ..
}
What could be the reason for this?
here is a sample controller for your example , you should define the bean in your context.xml or if you place it in this package : com.mahlzeit.web.server it will be managed by spring automatically , cause as i see you have placed the context:component-scan
#Controller
public class RestaurantInformationServiceImpl {
#Autowired
private RestaurantOwnerRepository restaurantOwnerRepository;
#RequestMapping(value="/")
public #ResponseBody ModelAndView getRestaurants(
HttpServletRequest request,
HttpServletResponse response) {
ModelAndView model = new ModelAndView("yourPage");
List<?> rests = restaurantOwnerRepository.getAvailableRestaurants(httpRequest.getSession().getId());
model.addObject("restList", rests );
return model;
}
}

HibernateException: Could not obtain transaction-synchronized Session for current thread 3

I am getting error:
org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
If I add the below snippet in DataSourceContext.xml, I don't get any error and the application works as expected.
<bean id="userDAO" class="com.my.portal.user.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
I would like my scan packages and register beans without above snippet in DataSourceContext.xml.
How can I achieve this?
Here are my complete code:
Web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/*.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
DataSourceContext.xml
<!-- Data Source Setup -->
<bean id="myDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="maxIdle" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<!-- Hibernate Sessionfactory Bean Definition -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDS"/>
<property name="annotatedClasses">
<list>
<value>com.my.portal.user.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Hibernate Transaction Setup -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
User.java
#Entity
#Table(name="USER")
public class User {
#Id
#Column(name="UserId")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private int UserId;
private String FirstName;
private String LastName;
public int getUserId() {
return UserId;
}
public void setUserId(int userId) {
UserId = userId;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String firstName) {
FirstName = firstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String lastName) {
LastName = lastName;
}
#Override
public String toString(){
return "UserId="+UserId+", FirstName="+FirstName+", LastName="+LastName;
}
}
UserController.java
#Controller
public class UserController {
private static final Logger logger = LoggerFactory.getLogger(UserController.class);
private UserService userService;
#Autowired
public UserController(UserService userService){
this.userService = userService;
}
#RequestMapping(value = "/user", method = RequestMethod.GET)
public String user(Locale locale, Model model){
model.addAttribute("getUser",userService.getUser());
return "user";
}
UserDAO.java
#Repository
public interface UserDAO {
public List<User> getUser();
}
UserDAOImpl.java
#Repository
#Transactional
public class UserDAOImpl implements UserDAO {
private static final Logger logger = LoggerFactory.getLogger(UserDAOImpl.class);
private SessionFactory sessionFactory;
#Autowired
public void setSessionFactory(SessionFactory sessionFactory){
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
#SuppressWarnings("unchecked")
#Override
public List<User> getUser() {
List<User> userList = currentSession().createQuery("from User").list();
return userList;
}
}
userService.java
#Service
public interface UserService {
public String getMessage();
}
UserServiceImpl.java
#Service
public class UserServiceImpl implements UserService {
private UserDAO userDAO;
#Autowired
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public String getMessage(){
return "User Registration Page";
}
#SuppressWarnings("unchecked")
#Override
public List<User> getUser(){
logger.info("getUser()");
return this.userDAO.getUser();
}
}

JPA - Entities are not stored in database

I am facing a problem, when I tried to insert a data into Database through JPA (#persistanceContex)
Observations
Not getting any errors;
Record is not storing into database (save)
When I tried with listAll() ; it retrieving the data from database
Domain
#Entity
public class Test {
#Id
private int id;
#Column(name="full_name")
private String fullName;
#Column(name="mobile_number")
private int mobileNumber;
.....
}
DAO Class
#Repository("testDAO")
#Transactional
public class TestDAO {
private EntityManager entityManager;
#PersistenceContext(unitName="CRUD_Test_Annotation")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void save(Test test){
entityManager.persist(test);
}
}
Service
#Service("testService")
#Transactional
public class TestService {
private static final Logger logger = LoggerFactory.getLogger(TestService.class);
#Autowired(required=true)
private TestDAO testDAO;
public void save(Test test){
logger.info("TestService::save()");
testDAO.save(test);
}
public void list(){
testDAO.getAll();
}
}
Controller
#RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Locale locale, Model model) {
Test test = new Test();
test.setId(xx);
test.setFullName("xxxxx");
test.setMobileNumber(yyyyyy);
testService.save(test);
return "home";
}
application-context.xml
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Declare a JPA entityManagerFactory-->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
<property name="persistenceUnitName" value="CRUD_Test_Annotation" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
</property>
</bean>
<!-- Declare a transaction manager-->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
First of all, you don't need two transaction boundaries, I suggest you remove #Transactional from your DAO and keep the one in your service.
Start by verifying that spring-transaction has initiated a transaction: Use the debugger and stop the application after the transaction boundary, for instance in your TestService.save-method. If transactions are running, you will see org.springframework.transaction.interceptor.TransactionInterceptor#invoke in the call stack. If you don't see the TransactionInterceptor, then that's your problem. Post your persistence.xml file if transactions are running.

Combining xml and java configurations in spring 3.2 + hibernate 4

I get the following error when i try to manipulate the person 'entity' from within the spring container:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.s
pring.entity.Person
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionF
actoryImpl.java:1141)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.jav
a:1433)
Where am I going wrong - perhaps in trying to combine the xml and annotated metadata?
Appreciate any help on this.
Entity:
#Entity
#Table(name = "PERSON")
public class Person implements Serializable {
private static final long serialVersionUID = -5527566248002296042L;
#Id
#Column(name = "ID")
#GeneratedValue
private Integer id;
#Column(name = "FIRST_NAME")
private String firstName;
#Column(name = "LAST_NAME")
private String lastName;
.....
.....
}
Service bean:
#Service("personService")
#Transactional
public class PersonService {
#Resource(name="sessionFactory")
private SessionFactory sessionFactory;
public List<Person> getAll() {
// Retrieve session from Hibernate
Session session = sessionFactory.openSession();
try{
// Create a Hibernate query (HQL)
Query query = session.createQuery("FROM Person");
// Retrieve all
return query.list();
}
finally{
session.close();
}
}
....
....
}
Main:
public static void main(String[] args){
ApplicationContext appContext =
new ClassPathXmlApplicationContext("META-INF/beans-txn.xml");
PersonService personService = (PersonService)appContext.getBean("personService");
personService.add("Rob","Cahill", new Double(20000));
List<Person> persons = personService.getAll();
.....
spring configuration:
<context:annotation-config />
<bean id="personService" class="org.spring.service.PersonService"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
.....
</bean>
<!-- Hibernate session factory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
.....
</property>
</bean>
Add to your sessionFactory bean definition:
<property name="packagesToScan" value="common.**.entities" />
Where common.**.entities - package with your entities.

Can't get view scope working for my ManagedBeans

I'm using JSF 2 with Spring 3 in my project and until now, I'm just using request and session scopes. I want to use view scope for some of my JSF beans (for multiple reasons).
The problem is that the JSF ManagedBean have to implement Serializable interface, and all of its properties.
I have some difficulty to get the Spring beans Serializables (ex. : VilleService...) because it continue to ask for more classes to be serializable even if it's not possible (hibernate classes or mysql ...).
Here is an example :
public class VilleBean extends BaseBean implements Serializable {
private Ville ville;
private List<Ville> villes;
private VilleService villeService;
private PaysService paysService;
private Pays pays;
private List<Pays> payss;
private PojoConverter<Pays> paysConverter = null;
public VilleBean() {
ville = new Ville();
pays = new Pays();
}
public void setVilleService(VilleService villeService) {
this.villeService = villeService;
}
public void setPaysService(PaysService paysService) {
this.paysService = paysService;
}
// getters and setters for attributes : ville, pays, villes, payss
public void addVilleAction() {
ville.setPays(pays);
villeService.create(ville);
}
public void deleteVilleAction(Ville ville) {
villeService.delete(ville);
villes = villeService.readAll();
}
public void updateVilleAction() {
ville.setPays(pays);
villeService.update(ville);
}
public PojoConverter<Pays> getPaysConverter() {
this.paysConverter = new PojoConverter<Pays>(getPayss());
return this.paysConverter;
}
}
public abstract class BaseBean {
protected FacesContext context = FacesContext.getCurrentInstance();
protected MessageFactory msg;
protected static final Logger log = Logger.getLogger(BaseBean.class);
}
POJO :
public class Ville implements Serializable {
private int id;
private String intitule;
private Pays pays;
// setters and getters
}
public class Pays implements Serializable {
private int id;
private String intitule;
// setters and getters
}
Service interface :
public interface VilleService extends ICrud<Ville> {
}
Service Impl :
public class VilleServiceBase implements VilleService {
private IDao<Ville> dao;
// getter and setter for dao
#Override
public List<Ville> readAll() throws Exception {
return dao.readAll();
}
#Override
public void create(Ville entity) throws Exception {
dao.create(entity);
}
//****
Util :
public interface ICrud<T> {
public java.util.List readAll() throws Exception;
public void create(T entity) throws Exception;
public void update(T entity) throws Exception;
//****
public interface IDao<T> {
public java.util.List<T> readAll();
public T create(T entity);
//****
public class DaoBase<T> extends org.springframework.orm.hibernate3.support.HibernateDaoSupport implements IDao<T> {
private final Class<T> type;
public DaoBase(Class<T> type) {
this.type = type;
}
#Override
public T load(int id) {
return (T) this.getHibernateTemplate().get(this.type, new Integer(id));
}
#Override
public T create(T entity) {
//****
this.getHibernateTemplate().save(entity);
return entity;
}
Spring beans : spring.xml :
<!-- Ville Service Implementation -->
<bean id="villeServiceBase" class="commun.ref.crud.VilleServiceBase">
<property name="dao">
<ref bean="villeDao"/>
</property>
</bean>
<!-- Ville Service Proxy -->
<bean id="villeService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="villeServiceBase"/>
</property>
<property name="proxyInterfaces">
<value>commun.ref.crud.VilleService</value>
</property>
<property name="interceptorNames">
<list>
<value>manageableServiceTransactionInterceptor</value>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
<bean id="villeDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="smile.util.DaoBase">
<constructor-arg>
<value>commun.ref.Ville</value>
</constructor-arg>
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
</property>
<property name="proxyInterfaces">
<value>smile.util.IDao</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
JSF configuration : faces-config.xml :
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
<application>
<variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
</variable-resolver>
</application>
******
<managed-bean>
<managed-bean-name>villeBean</managed-bean-name>
<managed-bean-class>commun.ref.ui.VilleBean</managed-bean-class>
<managed-bean-scope>view</managed-bean-scope>
<managed-property>
<property-name>villeService</property-name>
<value>#{villeService}</value>
</managed-property>
<managed-property>
<property-name>paysService</property-name>
<value>#{paysService}</value>
</managed-property>
</managed-bean>
Can you tell me please, what to do to get this to work.
On fields which you don't want to be serialized or they can't be serialized use transient modifier. For example:
private transient VilleService villeService;
As you are using MyFaces add this parameter to web.xml:
<context-param>
<param-name>org.apache.myfaces.SERIALIZE_STATE_IN_SESSION</param-name>
<param-value>false</param-value>
</context-param>
As I know this parameter is default false in Mojarra. This will disable serialization of state in session.
If you don't use session passivation you may use transient attribute for non-serializable fields.
For example:
private transient Connection connection;

Resources