Here's how my module works currently:
1. I create an injector with my custom hibernate module:
GuiceHibernateModule guiceHibernateModule =
new GuiceHibernateModule( new Class[] { User.class,
Item.class } );
* this module takes the array of classes (User.class, Item.class),
creates an AnnotationConfiguration, and binds a SessionFactory
2. I add a second module with the following bindings:
binder.bind(new Key<HibernateDao<User, Long>>() {})
.toInstance(new AbstractHibernateDao<User, Long>() {});
binder.bind(new Key<HibernateDao<Item, Long>>() {})
.toInstance(new AbstractHibernateDao<Item, Long>() {});
* HibernateDao is my DAO interface
* AbstractHibernateDao is the HibernateDao implementation
* User/Item - the hibernate pojo type
* Long - the primary key type
3. then in my code, i access my DAO with the following:
HibernateDao<User, Long> userDao =
injector.getInstance(new Key<HibernateDao<User, Long>>() {});
User user = new User();
user.setUsername("test");
Long id = userDao.save(user); // save the user
userDao.delete(user); // delete the user
Here's what I'm trying to figure out: I'd like get rid of step #2 by
dynamically creating it as part of my custom hibernate module in step
#1. I'd like to dynamically create the bindings based on the array of
classes that are passed into the module.
ex:
public GuiceHibernateModule(Class[] pojos) {
this.pojos = pojos;
}
...
for (Class pojo : pojos) {
binder.bind(new Key<HibernateDao<pojo, Long>>() {})
.toInstance(new AbstractHibernateDao<pojo, Long>() {});
}
Does anyone know if this is possible? If not, is there something that
Guice can do to address this?
I hope I illustrated my point clearly. If anyone is interested in
obtaining my code, please contact me. I have the project set up with
Maven2 and a JUnit test.
Thanks.
for (Class pojo : pojos) {
binder.bind(new Key<HibernateDao<pojo, Long>>() {})
.toInstance(new AbstractHibernateDao<pojo, Long>() {});
}
Does anyone know if this is possible? If not, is there something that
Guice can do to address this?
private static Map<Class, Key> keyMap = new HashMap<Class, Key>();
private <T> void bindDao(Class<T> clazz, Binder binder) {
binder.bind(getKey(clazz))
.toInstance(new AbstractHibernateDao<T, Long>() {});
}
@SuppressWarnings("unchecked")
public static <T> Key<HibernateDao<T, Long>> getKey(Class<T>
clazz) {
if (!keyMap.containsKey(clazz)) {
Key<HibernateDao<T, Long>>key = new Key<HibernateDao<T,
Long>>() {};
keyMap.put(clazz, key);
}
return keyMap.get(clazz);
}
I used a map to cache my Keys because creating a new key for the
instance lookup doesn't work (probably because it returns a different
hashcode/TypeLiteral).
So now to get a DAO instance, all I do is:
HibernateDao<User, Long> userDao =
injector.getInstance(GuiceHibernateModule.getKey(User.class));
and all is good! Thanks again for your help Dhanji.
If there's any interest, I'd like to contribute this as a 3rd party
module. It would be nice to get some help from the community. In the
meantime, I'll continue to work on it for my personal projects.
On Mar 27, 2:48 am, "Dhanji R. Prasanna" <dha...@gmail.com> wrote: