rzulf

JPA

Feb 3rd, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import javax.persistence.EntityManagerFactory;
  2. import javax.persistence.Persistence;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. /**
  7.  * @author zolw
  8.  */
  9. public class CustomEntityManagerFactory {
  10.  
  11.     private static EntityManagerFactory emf;
  12.     private static final Object mutex = new Object();
  13.  
  14.     public static EntityManagerFactory createFactory(CustomConfig config) {
  15.         Map<String, String> properties = new HashMap<>();
  16.         properties.put("javax.persistence.jdbc.url", config.get(String.class, "h2", "url"));
  17.         properties.put("javax.persistence.jdbc.password", config.get(String.class, "h2", "password"));
  18.         properties.put("javax.persistence.jdbc.user", config.get(String.class, "h2", "user"));
  19.         return Persistence.createEntityManagerFactory(config.getPersistanceUnitName(), properties);
  20.     }
  21.  
  22.     public static EntityManagerFactory getInstance() {
  23.         if (emf == null) {
  24.             synchronized (mutex) {
  25.                 if (emf == null) {
  26.                     CustomConfig config = CustomConfig.getInstance();
  27.                     emf = createFactory(config);
  28.                 }
  29.             }
  30.         }
  31.         return emf;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment