Guice with Hibernate and Dao

1,635 views
Skip to first unread message

Bin Wu

unread,
May 10, 2011, 3:02:10 AM5/10/11
to google...@googlegroups.com
Hello everyone,

I am pretty new to Guice and even J2EE, and I am currently trying to build a webapp which ues (GWT, Google-gin, gwt-platform, Guice, Hibernate etc.). My problem here is, at the server side, I am bit confused about how those components can collaborate with each other in a  good manner. Anyone here has a good example which uses Guice, Hibernate and a DAO layer?

Thanks a lot.

jMotta

unread,
May 10, 2011, 1:41:59 PM5/10/11
to google...@googlegroups.com
Bin,

I always like to think on IoC as a mechanism to abstract any code that isn't related to my business, I also think that most part of the application servers do something in this way, moving the responsibility of coding some infrastructure coding to the server instead making the developer worry about this kind of boring boilerplate code. But the price for this is the dependency of the container, some of them make your application be stucked in a single version of this application server given its particularities and it's somehow against the WORA, so you must let just the heavy infrastructure in the application server (JEE), like load balancing, clustering and so on.

You must also keep in mind that JPA itself isn't part of JEE, it was projected to either outside or inside an application server. I'm saying this because it's the closer that your explanation arrives of an JEE application compliance while using Hibernate, that's a framework with the same implementation line of the most modern implementations of JPA.

Anyway, I just said all this to make sure that you don't get confused about stuffs either inside (JEE) or outside the box. I also have an application that uses the same architecture that you've described besides the Hibernate part, I use low-level API of Datastore (that uses datanucleus to translate to GQL) in the Google AppEngine platform.

Any doubt, count on us! :)

Atenciosamente,

Jayr Motta
Software Developer




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

Bin Wu

unread,
May 10, 2011, 7:07:08 PM5/10/11
to google...@googlegroups.com
Hey Jayr,

Thanks for your detailed explanation about Java EE. I think my specific question is more about how to use Guice within the DAO. In a webapp, we should better control the DB session outside the DAO? So I am currently have no idea how to do this, had googled around but haven't found any good examples. So if anyone can show me some very simple pieces of code that would be great. Anyway, thanks again.
--
Regards,

Bin Wu 
BluePoint 03 9296 5100 0423 710 288 www.bluepoint.net.au

BluePoint has Gone Google

Each day, thousands of companies are going Google by switching to Google Apps – a web-based suite of messaging and collaboration applications. It's all hosted by Google, and designed with security and reliability in mind, saving your company the frustrations and hassles of managing traditional IT solutions yourself.
 Call BluePoint for more information or view the cloud calculator to calculate your potential business savings.

jMotta

unread,
May 10, 2011, 10:10:00 PM5/10/11
to google...@googlegroups.com
Bin,

I've forgot to paste this link in the last email, it was my failure. The link follows http://code.google.com/p/google-guice/wiki/GuicePersist.

What I meant explaining that is that you can use JPA as your persistence layer and use the Hibernate under JPA as a persistence provider. You'll need to create a persistence.xml saying what will be your datasource (it abstracts the creation of a connections pool), and doing this you'll make your application more portable. You'll also have transactional implementation and persistence context totally out of the box.

I hope this help. :)


Atenciosamente,

Jayr Motta
Software Developer




Alen Vrečko

unread,
May 11, 2011, 5:49:06 AM5/11/11
to google-guice
If you want to use Hibernate Native go with http://code.google.com/p/warp-persist/
(http://code.google.com/p/warp-persist/wiki/Tutorials). At work we
still use Hibernate with Warp persist as nobody sees the point in
spending time in learning the JPA API with it's slightly changed
semantics and less features while at the same time Hibernate does
exactly what we need.

Cheers
Alen

On 11 maj, 04:10, jMotta <jayrmo...@gmail.com> wrote:
> Bin,
>
> I've forgot to paste this link in the last email, it was my failure. The
> link followshttp://code.google.com/p/google-guice/wiki/GuicePersist.
>
> What I meant explaining that is that you can use JPA as your persistence
> layer and use the Hibernate under JPA as a persistence provider. You'll need
> to create a persistence.xml saying what will be your datasource (it
> abstracts the creation of a connections pool), and doing this you'll make
> your application more portable. You'll also have transactional
> implementation and persistence context totally out of the box.
>
> I hope this help. :)
>
> Atenciosamente,
>
> *Jayr Motta*
> Software Developer
> *
> *
> I'm  on BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta>
> !
>
>
>
>
>
>
>
> On Tue, May 10, 2011 at 8:07 PM, Bin Wu <b...@bluepoint.net.au> wrote:
> > Hey Jayr,
>
> > Thanks for your detailed explanation about Java EE. I think my specific
> > question is more about how to use Guice within the DAO. In a webapp, we
> > should better control the DB session outside the DAO? So I am currently have
> > no idea how to do this, had googled around but haven't found any good
> > examples. So if anyone can show me some very simple pieces of code that
> > would be great. Anyway, thanks again.
>
> > On 11 May 2011 03:41, jMotta <jayrmo...@gmail.com> wrote:
>
> >> Bin,
>
> >> I always like to think on IoC as a mechanism to abstract any code that
> >> isn't related to my business, I also think that most part of the application
> >> servers do something in this way, moving the responsibility of coding some
> >> infrastructure coding to the server instead making the developer worry about
> >> this kind of boring boilerplate code. But the price for this is the
> >> dependency of the container, some of them make your application be stucked
> >> in a single version of this application server given its particularities and
> >> it's somehow against the WORA, so you must let just the heavy infrastructure
> >> in the application server (JEE), like load balancing, clustering and so on.
>
> >> You must also keep in mind that JPA itself isn't part of JEE, it was
> >> projected to either outside or inside an application server. I'm saying this
> >> because it's the closer that your explanation arrives of an JEE application
> >> compliance while using Hibernate, that's a framework with the same
> >> implementation line of the most modern implementations of JPA.
>
> >> Anyway, I just said all this to make sure that you don't get confused
> >> about stuffs either inside (JEE) or outside the box. I also have an
> >> application that uses the same architecture that you've described besides
> >> the Hibernate part, I use low-level API of Datastore (that uses datanucleus
> >> to translate to GQL) in the Google AppEngine platform.
>
> >> Any doubt, count on us! :)
>
> >> Atenciosamente,
>
> >> *Jayr Motta*
> >> Software Developer
> >> *
> >> *
> >> I'm  on BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta>
> >> !
> > Bin Wu | *BluePoint *| 03 9296 5100 | 0423 710 288 | *www.bluepoint.net.au
> > * <http://www.bluepoint.net.au/>
>
> > BluePoint has Gone *Google*!
>
> > *Each day, thousands of companies are going Google by switching to Google
> > Apps – a web-based suite of messaging and collaboration applications. It's
> > all hosted by Google, and designed with security and reliability in mind,
> > saving your company the frustrations and hassles of managing traditional IT
> > solutions yourself.** Call BluePoint for more information or view the cloud
> > calculator <http://www.gonegoogle.com> to calculate your potential
> > business savings.*

ale

unread,
May 11, 2011, 11:41:59 PM5/11/11
to google-guice
------------ CLIENT SIDE ------------

class View {
uiBinder()....
}

class Presenter {
onbind() {
newUserButton() {
dispatch(new NewUserAction("name", "lastname"), ...);
}
}
}

------------ SERVER SIDE ------------

class NewUserActionHandler {
private final UserService userService;

@Inject
public NewUserActionHandler(UserService userService) {
this.userService = userService;
}

public NewUserResult execute(NewUserAction action, ...) {
this.userService.createNewUser(action.getName(),
action.getLastName());
}
...
}

class UserService {
private final UserDao userDao;

@Inject
UserService(UserDao userDao) {
this.userDao = userDao;
}

Long createNewUser(String name, String lastName) {
return userDao.storeUser( new User(name, lastName));
}

}

class UserDao {
private final Provider<EntityManager> em;

@Inject
public UserDao(Provider<EntityManager> em) {
this.em = em;
}

public Long storeUser(User user) {
this.em.get().persist(user);
return user.getId();

Bin Wu

unread,
May 12, 2011, 12:29:19 AM5/12/11
to google...@googlegroups.com
I think this answer is pretty much what I want. But I still got a question about the EntityManager. Can we inject a Session (Hibernate session) and how to do that? How the session can be managed? Or I am absolutely wrong?

On 12 May 2011 13:41, ale <ale.s...@gmail.com> wrote:
------------ CLIENT SIDE ------------

class View {
 uiBinder()....
}

class Presenter {
 onbind() {
   newUserButton() {
     dispatch(new NewUserAction("name", "lastname"), ...);
   }
 }
}

Clear
 

------------ SERVER SIDE ------------

class NewUserActionHandler {
 private final UserService userService;

 @Inject
 public NewUserActionHandler(UserService userService) {
   this.userService = userService;
 }

 public NewUserResult execute(NewUserAction action, ...) {
   this.userService.createNewUser(action.getName(),
action.getLastName());
 }
 ...
}

Clear
 

class UserService {
 private final UserDao userDao;

 @Inject
 UserService(UserDao userDao) {
   this.userDao = userDao;
 }

 Long createNewUser(String name, String lastName) {
    return userDao.storeUser( new User(name, lastName));
 }

}

Clear
 

class UserDao {
 private final Provider<EntityManager> em;

 @Inject
 public UserDao(Provider<EntityManager> em) {
   this.em = em;
 }

 public Long storeUser(User user) {
   this.em.get().persist(user);
   return user.getId();
 }
}

I do not understand this one here. Suppose my UserDao inherited from a GenericDao. So the inherited store method looks like

...
public T store (T entity) {
  getSession().saveOrUpdate(entity);
  return entity.getId();
}
...
protected Session getSession() {
  return this.session
}

protected void setSession(Session s) {
  this.session = s;
}
...

also the UserDao's constructor will be looks like

@Inject
public UserDao(Session s) {
  setSession(s);
}

Sorry I am really confused, and I may absolutely wrong. I am wondering here how the session could be correctly injected/managed if this is feasible.

Thanks.
 

On May 10, 4:02 am, Bin Wu <b...@bluepoint.net.au> wrote:
> Hello everyone,
>
> I am pretty new to Guice and even J2EE, and I am currently trying to build a
> webapp which ues (GWT, Google-gin, gwt-platform, Guice, Hibernate etc.). My
> problem here is, at the server side, I am bit confused about how those
> components can collaborate with each other in a  good manner. Anyone here
> has a good example which uses Guice, Hibernate and a DAO layer?
>
> Thanks a lot.

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




--
Regards,

Bin Wu 
BluePoint 03 9296 5100 0423 710 288 www.bluepoint.net.au

BluePoint has Gone Google

Each day, thousands of companies are going Google by switching to Google Apps – a web-based suite of messaging and collaboration applications. It's all hosted by Google, and designed with security and reliability in mind, saving your company the frustrations and hassles of managing traditional IT solutions yourself.
 Call BluePoint for more information or view the cloud calculator to calculate your potential business savings.

jMotta

unread,
May 12, 2011, 9:58:28 AM5/12/11
to google...@googlegroups.com
Bin,

What if you create a provider to the session and then bind the real implementation that will be accessed through the Session interface using some Guice module. You can use either a provider:


public class SessionProvider implements Provider<Session> {
 
private final Connection connection;

 
@Inject
 
public SessionProvider(Connection connection) {
   
this.connection = connection;
 
}

 
public Session get() {
   
Session session = new HibernateSession(); // You can either keep some sessions cached, use criativity! :P
    session
.setConnection(connection);
   
return session;
 
}
}
Then you do in your Guice module:


public class SomeModule extends AbstractModule {
 
@Override
 
protected void configure() {
    bind
(Session.class)
       
.toProvider(SessionProvider.class);
 
}
Then you inject like you've said: "@Inject Session session".

You can also use a @Provides annotation inside your Guice module:


public class SomeModule extends AbstractModule {
 
@Override
 
protected void configure() {
   
...
 
}

 
@Provides
 
Session provideSession() {
   
Session session = new HibernateSession();
    ... // You can inject in this method the connection or everything you need and use as the same way of the provider!

   
return session;
 
}
}

Hope it help! :)

Atenciosamente,

Jayr Motta
Software Developer




Reply all
Reply to author
Forward
0 new messages