-
Jun 25th, 2009, 03:00 AM
#1
dpHibernate problem
I'm hoping someone can point out what I'm doing wrong. I'm exposing a service via BlazeDS and am trying to get dpHibernate to lazy load the entities for me but can't figure out why dpHibernate is unable to access the hibernate session - I get a 'No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here' error every time I invoke the method to retreive the list of parents.
My web.xml is :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>dptest</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationConfig.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
</listener>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherSe rvlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Mapp all /messagebroker reequests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
my applicationContext.xml is:
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Database related stuff here -->
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="jdbc:mysql://localhost/dp"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQ LInnoDBDialect</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">create</prop> -->
<prop key="hibernate.cache.use_query_cache">false</prop>
<prop key="hibernate.cache.use_second_level_cache">false </prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>test.entity.Parent</value>
<value>test.entity.Child</value>
</list>
</property>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
my web-application-context.xml is:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:flex="http://www.springframework.org/schema/flex"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">
<bean id="hibernate-lazy-adapter" class="org.springframework.flex.core.ManageableCom ponentFactoryBean">
<constructor-arg value="net.digitalprimates.persistence.hibernate.H ibernateAdapter"/>
<property name="properties">
<value>
{"hibernate" :
{"sessionFactory" :
{ "class" : "net.digitalprimates.persistence.hibernate.utils.S pringSessionUtil",
"getCurrentSessionMethod" : "getCurrentSession"
}
}
}
</value>
</property>
</bean>
<!-- Flex stuff -->
<flex:message-broker>
</flex:message-broker>
<!-- Beans -->
<bean id="testService" class="test.service.TestServiceImpl">
<property name="sessionFactory" ref="sessionFactory" />
<flex:remoting-destination service-adapter="hibernate-lazy-adapter"/>
</bean>
</beans>
My parent class:
package test.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Parent {
@Id @GeneratedValue
private int id;
private String name;
@OneToMany(mappedBy="parent")
private Set<Child> kids = new HashSet<Child>();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Child> getKids() {
return kids;
}
public void setKids(Set<Child> kids) {
this.kids = kids;
}
}
My child class:
package test.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Child {
@Id @GeneratedValue
private int id;
private String name;
@ManyToOne
@JoinColumn(name="parent_id")
private Parent parent;
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
TestService interface:
package test.service;
import java.util.List;
import test.entity.Parent;
public interface TestService {
List<Parent> getParents();
}
and finally the implementation:
package test.service;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transac tional;
import test.entity.Parent;
@Transactional
public class TestServiceImpl implements TestService {
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@SuppressWarnings("unchecked")
@Override
public List<Parent> getParents() {
List l = sessionFactory.getCurrentSession().createQuery("fr om Parent p").list();
return l;
}
}
I expect it'll be something obvious but I just can't see the mistake!
-
Jun 27th, 2009, 02:46 PM
#2
Mattman -
You need to create an openSessionInViewFilter in your web.xml. I was unsuccessful getting dpHibernate to work any other way
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.O penSessionInViewFilter</filter-class>
<init-param>
<param-name>flushMode</param-name>
<param-value>ALWAYS</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/messagebroker/*</url-pattern>
</filter-mapping>
Also, in your app-context.xml, make sure you define a "load" method in the HibernateAdapter:
<bean id="hibernate-lazy-adapter" class="org.springframework.flex.core.ManageableCom ponentFactoryBean">
<constructor-arg value="net.digitalprimates.persistence.hibernate.H ibernateAdapter"/>
<property name="properties">
<value>
{
"hibernate":
{
"sessionFactory":
{
"class":"net.digitalprimates.persistence.hibernate .utils.SpringSessionUtil",
"getCurrentSessionMethod":"getCurrentSession"
},
"loadMethod":"load"
}
}
</value>
</property>
</bean>
In this case, "load" will be called on your DAOs -- this method is used just for the purpose of loading lazy objects when they are accessed -- not other service calls.
Hope this helps.
-Ryan
-
Jun 29th, 2009, 02:24 AM
#3
Ryan - great advice. That works just fine.
Many thanks,
Matt
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules