1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************
 * Copyright (c) 2008 Jason Kusnier & Tim Hollosy. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Jason Kusnier, Tim Hollosy, and Others
 ******************************************************************************/
package br.com.germantech.emf;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.eclipse.persistence.config.PersistenceUnitProperties;
import org.eclipse.persistence.config.TargetDatabase;
import org.eclipse.persistence.jpa.osgi.PersistenceProvider;

public abstract class FiscalEntityManagerFactory {

  private static EntityManagerFactory emf = null;
  private static Map<String, Object> properties = new HashMap<String, Object>();

  private static void init() {
    properties.put(PersistenceUnitProperties.TARGET_DATABASE, TargetDatabase.PostgreSQL);
    properties.put(PersistenceUnitProperties.JDBC_DRIVER, "org.postgresql.Driver");
    properties.put(PersistenceUnitProperties.JDBC_URL, "jdbc:postgresql://192.168.0.10:5432/ti_fiscal");
    properties.put(PersistenceUnitProperties.JDBC_USER, "postgres");
    properties.put(PersistenceUnitProperties.JDBC_PASSWORD, "admin");
    properties.put(PersistenceUnitProperties.JDBC_READ_CONNECTIONS_MIN, "1");
    properties.put(PersistenceUnitProperties.JDBC_WRITE_CONNECTIONS_MIN,"1");
    //properties.put(PersistenceUnitProperties.CACHE_SHARED_DEFAULT,"true");
    properties.put(PersistenceUnitProperties.BATCH_WRITING, "JDBC");
    properties.put(PersistenceUnitProperties.CLASSLOADER, FiscalEntityManagerFactory.class.getClassLoader());
    properties.put("eclipselink.logging.level", "FINE");
    properties.put("eclipselink.logging.timestamp", "false");
    properties.put("eclipselink.logging.session", "false");
    properties.put("eclipselink.logging.thread", "false");
    //properties.put("eclipselink.ddl-generation", "create-tables");
    //properties.put("eclipselink.ddl-generation.output-mode", "database");

    emf = new PersistenceProvider().createEntityManagerFactory("Fiscal", properties);
  }

  /**
   * Cria um novo objeto do tipo {@link FiscalEntityManagerFactory};
   *
   * @return the entity manager
   */
  public static EntityManager createEntityManager() {
    if (emf == null)
      init();

    return emf.createEntityManager();
  }
}