Hibernate Envers

129 views
Skip to first unread message

Tushar Bhasme

unread,
Dec 2, 2014, 5:29:10 AM12/2/14
to google...@googlegroups.com
I am trying to use  hibernate's envers functionality in my app. I though need to inject one of my services to implementation of RevisionListner.
I can't figure out how and where to bind the RevisionListner to Guice.

Thanks,
Tushar

nage...@gmail.com

unread,
Dec 3, 2014, 5:19:28 AM12/3/14
to google...@googlegroups.com
The only working way we found was this:



package something;

import java.io.Serializable;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.EntityTrackingRevisionListener;
import org.hibernate.envers.RevisionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;
...

public class AuditListener implements EntityTrackingRevisionListener {

   
private static final Logger log = LoggerFactory.getLogger(AuditListener.class);

   
@Inject
   
private RevMasterChangeDAO revMasterChangeDAO;

   
public AuditListener() {
       
super();
       
// this is not created by guice but somehow instatiated by hibernate envers
       
// therefore injection does not work out of the box
       
// the following statement injects manually all members in this.
       
// TODO find a better way to integrate this in guice

       
try {
           
InjectorHolder.getInjector()
                         
.injectMembers(this);
       
} catch (Exception e) {
            log
.warn(e.getMessage(), e);
       
}

   
}

   
public AuditListener(boolean dontInject) {
       
super();
       
// use this constructor for tests, if mocks are injected with and via mockito
   
}
snip

InjectorHolder looks like this:
/**
 * Holds instance of the Guice injector. Should be filled after initialization
 * of Injector (after calling createInjector())
 */

public final class InjectorHolder {

   
private static Injector staticInjector;

   
private InjectorHolder() {
   
}

   
public static void setInjector(Injector injector) {
        staticInjector
= injector;
   
}

   
public static Injector getInjector() {
       
return staticInjector;
   
}



tushar bhasme

unread,
Dec 3, 2014, 5:36:46 AM12/3/14
to google...@googlegroups.com
Yes, this is the exact solution that I just found!

I also created a helper class and set its Injector from GuiceServletContextListener when it gets initialized.


Thanks,
Tushar Bhasme

--
You received this message because you are subscribed to a topic in the Google Groups "google-guice" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-guice/Yp1xbCM37jY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/3925d1c3-f452-4719-9803-aa402c84840d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages