pombreda / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 1 forks source link

With guice-persist, it is not possible to provide a DataSource to providers. #750

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
JpaPersistService takes a parameter thus: 
   Properties persistenceProperties

However, this is used later to create an entity manager:
Persistence.createEntityManagerFactory(persistenceUnitName, 
persistenceProperties); 

The signature for that method is String, Map, NOT String, Properties.

If one is using Hibernate, you can pass the desired DataSource object in using 
that Map with Environment.DATASOURCE storing the actual object. Clearly you 
cannot do that if it's Properties (string->string map)

Changing this so that the persistenceProperties were a Map would solve this.

Original issue reported on code.google.com by nigel.ma...@gmail.com on 25 May 2013 at 3:19

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago

Original comment by sberlin on 20 Dec 2013 at 2:16

GoogleCodeExporter commented 9 years ago
FWIW, this was my really simple patch to make this work, which means I can do 
this:

            javax.sql.DataSource ds;
            // ...
            Map p = new HashMap();
            p.put( Environment.DATASOURCE, ds );
            JpaPersistModule jpaPersistModule = new JpaPersistModule("myapp-db").properties(p);

Should be broadly compatible, since Properties are also Maps.

---
From 0fac6920787289de42a2dea2f198e775447afbb9 Mon Sep 17 00:00:00 2001
From: Nigel Magnay <nigel.magnay@gmail.com>
Date: Tue, 23 Jul 2013 20:48:53 +0100
Subject: [PATCH] Properties to be a map, so that objects can be used.

Signed-off-by: Nigel Magnay <nigel.magnay@gmail.com>
---
 .../src/com/google/inject/persist/jpa/JpaPersistModule.java   | 11 ++++++-----
 .../src/com/google/inject/persist/jpa/JpaPersistService.java  |  7 +++----
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git 
a/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistModule.java 
b/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistModule.java
index b318b27..9efbfed 100644
--- a/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistModule.java
+++ b/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistModule.java
@@ -35,6 +35,7 @@ import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;

 import javax.persistence.EntityManager;
@@ -54,17 +55,17 @@ public final class JpaPersistModule extends PersistModule {
     this.jpaUnit = jpaUnit;
   }

-  private Properties properties;
+  private Map properties;
   private MethodInterceptor transactionInterceptor;

   @Override protected void configurePersistence() {
     bindConstant().annotatedWith(Jpa.class).to(jpaUnit);

     if (null != properties) {
-      bind(Properties.class).annotatedWith(Jpa.class).toInstance(properties);
+      bind(Map.class).annotatedWith(Jpa.class).toInstance(properties);
     } else {
-      bind(Properties.class).annotatedWith(Jpa.class)
-          .toProvider(Providers.<Properties>of(null));
+      bind(Map.class).annotatedWith(Jpa.class)
+          .toProvider(Providers.<Map>of(null));
     }

     bind(JpaPersistService.class).in(Singleton.class);
@@ -94,7 +95,7 @@ public final class JpaPersistModule extends PersistModule {
    * @param properties A set of name value pairs that configure a JPA persistence
    * provider as per the specification.
    */
-  public JpaPersistModule properties(Properties properties) {
+  public JpaPersistModule properties(Map properties) {
     this.properties = properties;
     return this;
   }
diff --git 
a/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistService.java 
b/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistService.java
index b8fe35c..3bc5358 100644
--- 
a/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistService.java
+++ 
b/extensions/persist/src/com/google/inject/persist/jpa/JpaPersistService.java
@@ -28,8 +28,7 @@ import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
-import java.util.Properties;
-
+import java.util.Map;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
@@ -42,11 +41,11 @@ class JpaPersistService implements Provider<EntityManager>, 
UnitOfWork, PersistS
   private final ThreadLocal<EntityManager> entityManager = new ThreadLocal<EntityManager>();

   private final String persistenceUnitName;
-  private final Properties persistenceProperties;
+  private final Map persistenceProperties;

   @Inject
   public JpaPersistService(@Jpa String persistenceUnitName,
-      @Nullable @Jpa Properties persistenceProperties) {
+      @Nullable @Jpa Map persistenceProperties) {
     this.persistenceUnitName = persistenceUnitName;
     this.persistenceProperties = persistenceProperties;
   }
-- 
1.8.5.1

Original comment by nigel.ma...@gmail.com on 20 Dec 2013 at 3:55

GoogleCodeExporter commented 9 years ago
Patch provided is totally reasonable. There is no developer anymore on Guice? 
Why I have to build my own version. Should this be already merged, with a 3.0.1 
build?

Original comment by Sebastie...@gmail.com on 17 Mar 2014 at 1:18