RequestFactory persist() error with hibernate

121 views
Skip to first unread message

Emanuele Righetto

unread,
Jan 6, 2011, 8:59:34 PM1/6/11
to Google Web Toolkit
Hi, i'm trying to build a custom webapp using RequestFactory + Spring
+ Hibernate.
My goal is to build a table (using CellTable) where users can edit
some properties of the objects being displayed and then save them, but
anytime i call the persist() method i have an hibernate exception:

02:56:41,500 ERROR LazyInitializationException:42 - could not
initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy
- no Session
at
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:
86)
at
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:
140)
at
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:
190)
at it.aimvicenza.intercompany.server.entity.Lavorato_$
$_javassist_4.getData(Lavorato_$$_javassist_4.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.serializeEntity(JsonRequestProcessor.java:
1536)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.getSerializedEntity(JsonRequestProcessor.java:
1372)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.constructBeforeDataMap(JsonRequestProcessor.java:
1167)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.processJsonRequest(JsonRequestProcessor.java:
889)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.decodeAndInvokeRequest(JsonRequestProcessor.java:
243)
at
com.google.gwt.requestfactory.server.JsonRequestProcessor.decodeAndInvokeRequest(JsonRequestProcessor.java:
63)
at
com.google.gwt.requestfactory.server.RequestFactoryServlet.doPost(RequestFactoryServlet.java:
116)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
233)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:
191)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
127)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
102)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
109)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
298)
at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:
852)
at org.apache.coyote.http11.Http11Protocol
$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint
$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

what i've done so far is:
- created EntityProxy (@ProxyFor) for my @Entity class (used by
hibernate)
- created @Service(ObjectExstendingMyEntity.class). i didn't used the
@Entity class directly to keep the code clean, i'm not sure if this
can be be a problem, but all static methods are working correctly.
I've also tryed to move anything inside the @Entity class but the
error still remain.
- created a CellTable with some column and some editable fields: here
an example of one column updater:

comColumn.setFieldUpdater(new FieldUpdater<LavoratoProxy, String>() {
public void update(int index, LavoratoProxy object, String value) {
LavoratoRequest lavReq = requestFactory.lavoratoRequest();
LavoratoProxy newObject = lavReq.edit(object);
int selectedIndex = comOption.getSelectedIndex();
String commId;
if (selectedIndex >= 0 ){
//Another column
commId = comOption.getValue(selectedIndex);
}
for (CommessaProxy comm : currentList){
//searching into an existing list
if (comm.getId().equals(commId)) {
newObject.setCodiceCommessa(comm);
lavReq.persist().using(newObject).fire(new Receiver<Object>(){
@Override
public void onFailure(ServerFailure error){
Window.alert("ERROR: "+error.getMessage());
}
@Override
public void onSuccess(Object response) {
Window.alert("OK: "+response);
}
});
}
});

(i know the for loop is a bit strange, but usually the field update
operation is managed by a dialogbox, and here i've merged the code to
simpify comprehension. The problem is the same with or without the
dialog).

my @Service class looks like:

@Service(LavoratoDTO.class)
public interface LavoratoRequest extends RequestContext {
public Request<List<LavoratoProxy>> findAll();
public Request<LavoratoProxy> findLavorato(Long id);
.........
public InstanceRequest<LavoratoProxy, Void> persist();
public InstanceRequest<LavoratoProxy, Void> remove();
}

and the server classes are:

@Entity
public class Lavorato {
protected Commessa codiceCommessa;
//many other fields
protected Long id;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name = "codice_commessa")
public Commessa getCodiceCommessa() {
return codiceCommessa;
}
public void setCodiceCommessa(Commessa codiceCommessa) {
this.codiceCommessa = codiceCommessa;
}
//many other getters and setters for fields....
@Transient
public Integer getVersion(){
return 1;
}
@Transient
public static Lavorato findLavorato(Long id){
return ServiceLocator.getStaticLavoratoDAO().findById(id, false);
}
}




public class LavoratoDTO extends Lavorato {
public static List<Lavorato> findAll(){
List<Lavorato> lav =
ServiceLocator.getStaticLavoratoDAO().findAllDaApprovareAmm();
return lav;
}
public static Lavorato findLavorato(Long id){
return ServiceLocator.getStaticLavoratoDAO().findById(id, false);
}
public void persist(){
System.out.println("hello!!!!");
ServiceLocator.getStaticLavoratoDAO().makePersistent(this);
}
public void remove(){
ServiceLocator.getStaticLavoratoDAO().makeTransient(this);
}
}

I'm not sure if the error is from hibernate (as it seems) or from gwt,
because i never got the "hello" string printed out (first statement of
persist() method).

Note: the ServiceLocator you see is NOT related to new features of
2.1.1, but it is a simply class that returs static references to
hibernate DAOs

I'm also using a filter to manage hibernate session:
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-
class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</
filter-class>
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

any idea?
Reply all
Reply to author
Forward
0 new messages