@Transactional having no effect

94 views
Skip to first unread message

handyande

unread,
Sep 23, 2010, 6:59:44 PM9/23/10
to warp-core
Hi all,

I am new to Warp and loving the ideas but I have a problem with the
configuration of it.
No matter where I put the @Transactional annotations I get the error:

"saveOrUpdate is not valid without active transaction"

and similar errors when performing createQuery.

I'm not sure what I have missed as I have worked through all the
documentation I can find.

using warp 2.0 with the PersistenceFilter and the following
configuration:

Injector injector =
Guice.createInjector(PersistenceService.usingHibernate()
.across(UnitOfWork.REQUEST).buildModule(), getModule());

Many thanks for any pointers,
Andrew

Dhanji R. Prasanna

unread,
Sep 24, 2010, 2:55:22 AM9/24/10
to warp...@googlegroups.com
Can you post your transactional classes? I think the problem might be that the method is private or PersistenceFilter is not active.

BTW, guice-persist now officially supercedes warp.

Dhanji.


--
You received this message because you are subscribed to the Google Groups "warp-core" group.
To post to this group, send email to warp...@googlegroups.com.
To unsubscribe from this group, send email to warp-core+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/warp-core?hl=en.


handyande

unread,
Sep 24, 2010, 6:22:52 AM9/24/10
to warp-core
Thanks very much for the fast reply.
I saw the guice-persist but was not sure if it was stable enough to
replace the warp-persist at this stage - the hosting pages look a
little bare...

Back to the problem,
My files are just proof of concept, but here is the class making
hibernate calls:

package com.mycompany.web;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.wideplay.warp.persist.Transactional;
import org.apache.wicket.markup.html.basic.Label;
import org.hibernate.Session;
import org.wicketstuff.annotation.mount.MountPath;

import java.util.List;

@MountPath(path="second")
public class SecondPage extends BasePage {
@Inject
Provider<Session> session;

@Transactional
public void save(User user)
{
session.get().saveOrUpdate( user );
}

@Transactional
public List<User> getUsers()
{
return session.get().createQuery( "from User u" ).list();
}

public SecondPage() {
User user = new User( "myuser" );
save( user );

for ( User u : getUsers() )
{
System.out.println("user = " + u);
}
}
}


Here is the actual persisted class:

package com.mycompany.web;

import org.hibernate.annotations.Entity;

import javax.persistence.Id;

@Entity
public class User
{
@Id
String username;

public User( String username )
{
this.username = username;
}

/* snip */
}

and the web.xml is configured thusly:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>web</display-name>

<filter>
<filter-name>warpSessionFilter</filter-name>
<filter-class>com.wideplay.warp.persist.PersistenceFilter</
filter-class>
</filter>

<filter>
<filter-name>wicket.web</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-
class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>com.mycompany.web.WicketApplication</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>warpSessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>wicket.web</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


</web-app>
> > warp-core+...@googlegroups.com<warp-core%2Bunsubscribe@googlegroups .com>
> > .

Dhanji R. Prasanna

unread,
Sep 24, 2010, 6:39:30 AM9/24/10
to warp...@googlegroups.com
Hmm, I wonder if wicket might be overriding Guice's intercepted proxy...

Can you try it in a non wicket class? (Try @Injecting this class into a wicket class instead).

Dhanji.

To unsubscribe from this group, send email to warp-core+...@googlegroups.com.

yaniv kessler

unread,
Sep 24, 2010, 7:05:18 AM9/24/10
to warp...@googlegroups.com
Hi,

Can you also show the code that creates the injector ?

Yaniv

yaniv kessler

unread,
Sep 24, 2010, 7:06:37 AM9/24/10
to warp...@googlegroups.com
Pardon :)

it was in your first email, the reason I asked is, do you not need the Guice Filter in the mix too ?

Andrew Williams

unread,
Sep 24, 2010, 7:20:47 AM9/24/10
to warp...@googlegroups.com
That is handled by the wicket-guice code, I believe - line 2 in the following:

Injector injector = Guice.createInjector(PersistenceService.usingHibernate()
.across(UnitOfWork.REQUEST).buildModule(), getModule());

        addComponentInstantiationListener(new GuiceComponentInjector(this, injector));

Andrew Williams

unread,
Sep 24, 2010, 7:34:01 AM9/24/10
to warp...@googlegroups.com
Strangely when I do that hibernate complains that it can no longer find the mapping.

Perhaps this is the next step and there is a problem with the hibernate configuration - though it seems fine to me.

Andrew

yaniv kessler

unread,
Sep 24, 2010, 7:34:49 AM9/24/10
to warp...@googlegroups.com
its not visible in the attached code. so just a direction to check :)

also, you might wanna make sure you have "hibernate.current_session_context_class" set to "managed"

and... which jar version of warp persist are you using (the exact version) ?

Andrew Williams

unread,
Sep 24, 2010, 7:39:01 AM9/24/10
to warp...@googlegroups.com
Thanks,

the configuration does include that propert,

AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration.setProperty("hibernate.current_session_context_class", "managed");

configuration.addAnnotatedClass(User.class);

configuration.configure();
binder.bind(Configuration.class).toInstance(configuration);

The version is

2.0-20090214

Andrew

yaniv kessler

unread,
Sep 24, 2010, 8:12:10 AM9/24/10
to warp...@googlegroups.com
Try what dhanji suggested, make a test class with a @Transactional method in it (like DataService or something) and inject IT into your page, maybe there is a problem for Guice to intercept Wicket page methods, besides its much better practice to separate that code from your page :)

Andrew Williams

unread,
Sep 24, 2010, 9:36:35 AM9/24/10
to warp...@googlegroups.com
Yes, I did that - the comment I made about the entity not being found is what I see when I try that, 
but from the example I posted last you can see that the User is included in the configuration...

this is taxing me - I seem to have (had?) 2 problems at once

Andrew

yaniv kessler

unread,
Sep 24, 2010, 9:46:04 AM9/24/10
to warp...@googlegroups.com
Maybe you should try to create an injector independent of Wicket (using a GuiceFilter and SevletListener) and initialized warp persist and your independent data class there, make sure it appears before wicket filter in your web.xml, and just for the sake of testing simply set the previously created injector in your WebApplication class statically and then create the GuiceComponentInjector. (i.e. you'll have a static injector field in your web app class), this is just for testing! :)

Hopefully this will either implicate or eliminate wicket Component Injector...

Dan Retzlaff

unread,
Sep 24, 2010, 9:57:44 AM9/24/10
to warp...@googlegroups.com
Andrew,

Wicket's component injector only handles @Inject annotated methods. It isn't sophisticated enough to create the proxy required for @Transactional. I'm guessing that you're creating the page with the "new" operator instead of with Injector.getInstance(). I suggest moving your @Transactional methods onto a DAO object, and @Inject'ing that into your page. Wicket's component injector will then create the DAO through the injector, allowing the transaction-creating proxy to be created.

Dan

Andrew Williams

unread,
Sep 24, 2010, 11:02:41 AM9/24/10
to warp...@googlegroups.com
Success!

Thanks for all the input guys it was very helpful in understanding the platform and finding the fix.
In the end the combination of two problems turned out to be:

1) Wicket's guice component injector indeed cannot handle @Transactional
2) My @Entity s were not annotated correctly - this became evident when I stripped it to the base and got to the core of the errors.

Things are now working smoothly and the resulting code is as clean and slick as I had imagined it would be.

Keep up the great work!

Andrew

P.S. how many people are migrating to guice-persist or have a plan of doing so in the short term?

nino martinez wael

unread,
Sep 24, 2010, 12:28:51 PM9/24/10
to warp...@googlegroups.com
I have a plan of migration as soon as possible , when Guice Persist comes out..

2010/9/24 Andrew Williams <and...@loc8solutions.com>:

Dhanji R. Prasanna

unread,
Sep 24, 2010, 4:12:09 PM9/24/10
to warp...@googlegroups.com
On Fri, Sep 24, 2010 at 11:57 PM, Dan Retzlaff <dret...@gmail.com> wrote:
Andrew,

Wicket's component injector only handles @Inject annotated methods. It isn't sophisticated enough to create the proxy required for @Transactional. I'm guessing that you're creating the page with the "new" operator instead of with Injector.getInstance(). I suggest moving your @Transactional methods onto a DAO object, and @Inject'ing that into your page. Wicket's component injector will then create the DAO through the injector, allowing the transaction-creating proxy to be created.

Yes, this is exactly what I meant in my first post. Thanks for adding the details =)

Dhanji.
Reply all
Reply to author
Forward
0 new messages