How to inject Hibernate Session

47 views
Skip to first unread message

Ken

unread,
Mar 17, 2008, 11:19:32 PM3/17/08
to warp-core
I want to use hibernate in warp
this is my pojo
package com.ergal.test.po;

/**
*
* @author ohergal
* @hibernate.class table="T_BLOG" dynamic-update="true" dynamic-
insert="true" lazy="true"
*/
public class Blog {
private String id;
private String subject;
private String text;

/**
* @hibernate.id generator-class="uuid.hex"
* @hibernate.column name="SYS_ID" sql-type="VARCHAR2" length="50"
* @return the id
*/
public String getId() {
return id;
}
/**
* @hibernate.property type="string" length="50"
* @hibernate.column name="SUBJECT" sql-type="VARCHAR2" length="50"
* @return the subject
*/
public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

/**
* @hibernate.property type="string" length="50"
* @hibernate.column name="TEXT" sql-type="VARCHAR2" length="50"
* @return the text
*/
public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public void setId(String id) {
this.id = id;
}

// rest of the getters/setters + equals() + hashCode()

}

my module
package com.test;




import org.hibernate.cfg.Configuration;

import com.google.inject.AbstractModule;
import com.wideplay.warp.Warp;
import com.wideplay.warp.persist.PersistenceService;
import com.wideplay.warp.persist.UnitOfWork;

public class MyGuiceModule extends AbstractModule {

@Override
protected void configure() {
// TODO Auto-generated method stub

bind(Configuration.class).toInstance(new
Configuration().configure("hibernate.cfg.xml"));

}

public void configure(Warp warp) {
// TODO Auto-generated method stub

warp.install(PersistenceService.usingHibernate().across(UnitOfWork.TRANSACTION).buildModule());


warp.addStartupListener(MyWarpStartup.class);

}

}

MyWarpStartup

package com.test;

import com.google.inject.Inject;
import com.wideplay.warp.StartupListener;
import com.wideplay.warp.persist.PersistenceService;

public class MyWarpStartup implements StartupListener {
@Inject private PersistenceService service;

public void onStartup() {
//start persistence service
service.start();
}
}

my service
package com.test;

import org.hibernate.Session;

import com.ergal.test.po.Blog;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.wideplay.warp.persist.Transactional;

public class MyService {
@Inject
Provider<Session> session;

@Transactional
public void createNewPerson() {
session.get().saveOrUpdate(new Blog());
}
}

and my unit test

package com.ergal.test;

import org.junit.Before;
import org.junit.Test;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.test.MyGuiceModule;
import com.test.MyService;

public class ConnectionTest{

private MyGuiceModule m;
//private Client client;
private MyService s;

@Before
public void setup(){
//client = new Client();
m = new MyGuiceModule();
s = new MyService();

}

@Test
public void testMyService(){

Injector in = Guice.createInjector(m);
in.injectMembers(s);
s.createNewBlog();
}


}

but when I run the test


com.google.inject.ConfigurationException: Error at
com.test.MyService.session(MyService.java:10) Binding to
com.google.inject.Provider<org.hibernate.Session> not found. No
bindings to that type were found.
at com.google.inject.BinderImpl
$RuntimeErrorHandler.handle(BinderImpl.java:426)
at
com.google.inject.AbstractErrorHandler.handle(AbstractErrorHandler.java:
30)
at
com.google.inject.ErrorMessages.handleMissingBinding(ErrorMessages.java:
46)
at
com.google.inject.InjectorImpl.getInternalFactory(InjectorImpl.java:
192)
at com.google.inject.InjectorImpl
$SingleFieldInjector.<init>(InjectorImpl.java:461)
at com.google.inject.InjectorImpl$6.create(InjectorImpl.java:371)
at com.google.inject.InjectorImpl$6.create(InjectorImpl.java:369)
at
com.google.inject.InjectorImpl.addInjectorsForMembers(InjectorImpl.java:
384)
at
com.google.inject.InjectorImpl.addSingleInjectorsForFields(InjectorImpl.java:
367)
at com.google.inject.InjectorImpl.addInjectors(InjectorImpl.java:350)
at com.google.inject.InjectorImpl$4.create(InjectorImpl.java:332)
at com.google.inject.InjectorImpl$4.create(InjectorImpl.java:329)
at com.google.inject.util.ReferenceCache.create(ReferenceCache.java:
53)
at
com.google.inject.util.AbstractReferenceCache.internalCreate(AbstractReferenceCache.java:
59)
at
com.google.inject.util.AbstractReferenceCache.get(AbstractReferenceCache.java:
116)
at com.google.inject.InjectorImpl.injectMembers(InjectorImpl.java:
672)
at com.google.inject.InjectorImpl$8.call(InjectorImpl.java:682)
at com.google.inject.InjectorImpl$8.call(InjectorImpl.java:681)
at com.google.inject.InjectorImpl.callInContext(InjectorImpl.java:
747)
at com.google.inject.InjectorImpl.injectMembers(InjectorImpl.java:
680)
at com.ergal.test.ConnectionTest.testMyService(ConnectionTest.java:
29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
98)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
79)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
87)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
88)
at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:
51)
at org.junit.internal.runners.JUnit4ClassRunner
$1.run(JUnit4ClassRunner.java:44)
at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
27)
at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
37)
at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
42)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
38)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:
38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:
673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
196)



Dhanji R. Prasanna

unread,
Mar 18, 2008, 12:59:24 AM3/18/08
to warp...@googlegroups.com
You have forgotten to add the warp-configuration in your test code =)

   @Test
       public void testMyService(){
               Module wp = PersistenceService.usingHibernate();

               Injector in = Guice.createInjector(m, wp);    //here!!
               in.injectMembers(s);
               s.createNewBlog();
       }


Instead of using injectMembers() I recommend using getInstance().

Dhanji.

Ken

unread,
Mar 18, 2008, 1:49:09 AM3/18/08
to warp-core
Thx very much
But ut still not work

I rewrite the test to this
public class ConnectionTest{

private MyGuiceModule m;
//private Client client;
private MyService s;

@Before
public void setup(){
//client = new Client();
m = new MyGuiceModule();
s = new MyService();

}

@Test
public void testMyService(){
Module wp = PersistenceService.usingHibernate().buildModule();
Injector in = Guice.createInjector(m,wp);
in.getInstance(MyService.class);
s.createNewBlog();
}
}


when I run this test the error is

java.lang.NullPointerException
at com.test.MyService.createNewBlog(MyService.java:16)
at com.ergal.test.ConnectionTest.testMyService(ConnectionTest.java:
32)

the session is still null in MyService
what can I do now




Rory Ye

unread,
Mar 18, 2008, 2:16:33 AM3/18/08
to warp...@googlegroups.com
the same question. I got this exception.

org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:540)
    at com.wideplay.warp.hibernate.HibernateLocalTxnInterceptor.invoke(Unknown Source)
    at com.google.inject.InterceptorStackCallback$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:66)
    at com.google.inject.InterceptorStackCallback.intercept(InterceptorStackCallback.java:45)
    at com.jdkcn.guice.service.impl.EntryServiceHibernateImpl$$EnhancerByGuice$$ec2fe298.saveEntry(<generated>)
    at com.jdkcn.guice.service.EntryServiceHibernateTest.testSave(EntryServiceHibernateTest.java:60)
    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 org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)

    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



2008/3/18, Ken <ohe...@gmail.com>:



--
My site:http://www.jdkcn.com

Ken

unread,
Mar 18, 2008, 2:19:42 AM3/18/08
to warp-core
I write an other test

like this
MyGuiceModule m;
m = new MyGuiceModule();
s = new MyService();
Module wp = PersistenceService.usingHibernate().buildModule();
Injector in = Guice.createInjector(m,wp);
MyService s2 = in.getInstance(MyService.class);
s2.createNewBlog();

the error changed

Exception in thread "main" java.lang.NullPointerException
at
com.wideplay.warp.hibernate.SessionFactoryHolder.getCurrentSessionFactory(SessionFactoryHolder.java:
62)
at
com.wideplay.warp.hibernate.HibernateLocalTxnInterceptor.invoke(HibernateLocalTxnInterceptor.java:
48)
at com.google.inject.InterceptorStackCallback
$InterceptedMethodInvocation.proceed(InterceptorStackCallback.java:66)
at
com.google.inject.InterceptorStackCallback.intercept(InterceptorStackCallback.java:
45)
at com.test.MyService$$EnhancerByGuice$
$2db8423a.createNewBlog(<generated>)
at com.ergal.test.TestMyConnection.main(TestMyConnection.java:24)

this is my module
ublic class MyGuiceModule extends AbstractModule {

@Override
protected void configure() {
// TODO Auto-generated method stub

bind(Configuration.class).toInstance(new
Configuration().configure("hibernate.cfg.xml"));

}

public void configure(Warp warp) {
// TODO Auto-generated method stub

warp.install(PersistenceService.usingHibernate().across(UnitOfWork.TRANSACTION).buildModule());

//install other modules (to guice)
// warp.install(...);

//register a startup listener to initialize the
PersistenceService
warp.addStartupListener(MyWarpStartup.class);

}

}

how to build a sessionFactory

Dhanji R. Prasanna

unread,
Mar 18, 2008, 2:28:10 AM3/18/08
to warp...@googlegroups.com
In tests you must manage the unit of work yourself. One thing you can do is:

injector.getInstance(WorkManager).beginWork();

//do create blog etc.

injector.getInstance(WorkManager).endWork();


But must manage the transactions yourself. I do not recommend writing unit tests with the injector and warp-persist. They are meant mainly for production code. For tests you should try using mock objects such as  EasyMock provides.

Dhanji.

Rory Ye

unread,
Mar 18, 2008, 2:32:56 AM3/18/08
to warp...@googlegroups.com
sorry for this question.
i missing this property in hibernate.cfg.xml

<property name="current_session_context_class">thread</property>

2008/3/18, Rory Ye <ror...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages