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
*
*
I'm  on 
BlackBeltFactory.com<http://www.blackbeltfactory.com/ui#!User/jmotta/ref=jmotta>
!



On Thu, May 12, 2011 at 1:29 AM, Bin Wu <[email protected]> wrote:

> 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 <[email protected]> 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 <[email protected]> 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 [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> 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
> * <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.*
>
>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>

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

Reply via email to