/** * Get the EntityManager for the specified persistence unit name. * * @param name The persistence unit name */ public EntityManager em(String name) { EntityManagerFactory emf = emfs.get(name); if (emf == null) { return null; } return emf.createEntityManager(); }
public static void initialiseSubscriptions( EntityManagerFactory entityManagerFactory, Long immediateFrequency) { EntityManager badSubsEm = entityManagerFactory.createEntityManager(); TypedQuery<Subscription> badSubsQuery = badSubsEm.createNamedQuery("subscription.unInitialised", Subscription.class); List<Subscription> uninitialisedSubscriptions = badSubsQuery.getResultList(); badSubsEm.close(); if (uninitialisedSubscriptions.size() > 0) { logger.debug("Initialising {} subscriptions", uninitialisedSubscriptions.size()); for (Subscription s : uninitialisedSubscriptions) { logger.debug( "Scheduling new run for {} subscription {}", s.getSubscriber().emailAddress, s.getId()); EntityManager schedEm = entityManagerFactory.createEntityManager(); schedEm.getTransaction().begin(); s.calculateNextScheduledRun(immediateFrequency); schedEm.merge(s); schedEm.getTransaction().commit(); schedEm.close(); } } }
/** @param args */ public static void main(String[] args) { EntityManagerFactory emf = null; EntityManager em = null; Map<String, String> properties = new HashMap<String, String>(); properties.put(DDL_GENERATION, DROP_AND_CREATE); properties.put(DDL_GENERATION_MODE, DDL_BOTH_GENERATION); properties.put(TARGET_DATABASE, TargetDatabase.Oracle); emf = PersistenceService.createEMF(properties, true, new EclipseLinkServiceFactory(), true); em = emf.createEntityManager(); em.close(); emf.close(); }
public static void main(String[] args) throws Exception { HashMap map = new HashMap(); // map.put("hibernate.show_sql", "true"); EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan", map); EntityManager entityManager = factory.createEntityManager(); entityManager.getTransaction().begin(); try { System.out.println("Initialize DB"); InitializeDB.initialize(entityManager); System.out.println(); System.out.println(); System.out.println("Find Bill Burke by named parameter"); Customer cust = findCustomerByNamedParameter(entityManager, "Bill", "Burke"); System.out.println("Bill Burke's cust id: " + cust.getId()); System.out.println(); System.out.println(); System.out.println("Find Gavin King by indexed parameter"); cust = findCustomerByIndexedParameter(entityManager, "Gavin", "King"); System.out.println("Gavin King's cust id: " + cust.getId()); System.out.println(); System.out.println(); System.out.println("Output all customers via paging"); List results; int first = 0; int max = 2; do { results = getCustomers(entityManager, max, first); Iterator it = results.iterator(); while (it.hasNext()) { Customer c = (Customer) it.next(); System.out.println(c.getFirstName() + " " + c.getLastName()); } entityManager.clear(); first = first + results.size(); } while (results.size() > 0); } finally { entityManager.getTransaction().commit(); entityManager.close(); factory.close(); } }
public void importColumns() throws Exception { LOGGER.info("List of objects for source: " + stageSourceCode); EntityManagerFactory emf = Persistence.createEntityManagerFactory("OpenBIStage"); EntityManager em = emf.createEntityManager(); Query query; // get source query = em.createQuery("SELECT x FROM StageSource x WHERE x.etlStageSourceCode = ?1"); query.setParameter(1, stageSourceCode); List<StageSource> sources = query.getResultList(); // get source objects and dbs List<StageObject> objects = sources.get(0).getStageObject(); List<StageSourceDb> sourcedbs = sources.get(0).getStageSourceDb(); // load properties from property file String dbpropertyfile = "datasources/" + sourcedbs.get(0).getEtlStageSourceDbJdbcname() + ".properties"; Properties dbproperties = new Properties(); dbproperties.load(new FileInputStream(dbpropertyfile)); // Configure db connection org.opendatakraken.core.db.DBConnection sourceConnectionBean = new org.opendatakraken.core.db.DBConnection(); sourceConnectionBean.setPropertyFile(dbproperties.getProperty("srcconnaddpropertyfile")); sourceConnectionBean.setDatabaseDriver(dbproperties.getProperty("srcdbdriverclass")); sourceConnectionBean.setConnectionURL(dbproperties.getProperty("srcdbconnectionurl")); sourceConnectionBean.setUserName(dbproperties.getProperty("srcdbusername")); sourceConnectionBean.setPassWord(dbproperties.getProperty("srcdbpassword")); sourceConnectionBean.openConnection(); DictionaryExtractor dataDict = new DictionaryExtractor(); dataDict.setSourceConnection(sourceConnectionBean); // For each object String sourceIdentifier; for (StageObject object : objects) { // Dermine source identifier sourceIdentifier = object.getEtlStageObjectName(); if (!(sourcedbs.get(0).getEtlStageSourceOwner().equals("")) && sourcedbs.get(0).getEtlStageSourceOwner() != null) { sourceIdentifier = sourcedbs.get(0).getEtlStageSourceOwner() + "." + sourceIdentifier; } dataDict.setSourceTable(sourceIdentifier); dataDict.retrieveColumns(); String[] colNames = dataDict.getColumnNames(); String[] colDefs = dataDict.getColumnDefinition(); String[] colOriginalDefs = dataDict.getColumnDefinition(); int[] colPkPos = dataDict.getColumnPkPositions(); em.getTransaction().begin(); for (int i = 0; i < colNames.length; i++) { query = em.createQuery( "SELECT x FROM StageColumn x WHERE x.etlStageObjectId = ?1 AND x.etlStageColumnName = ?2"); query.setParameter(1, object.getEtlStageObjectId()); query.setParameter(2, colNames[i]); List<StageColumn> columns = query.getResultList(); StageColumn column; if (columns.size() == 0) { column = new StageColumn(); column.setEtlStageObjectId(BigDecimal.valueOf(object.getEtlStageObjectId())); column.setEtlStageColumnPos(BigDecimal.valueOf(i + 1)); column.setEtlStageColumnName(colNames[i]); column.setEtlStageColumnDef(colDefs[i]); column.setEtlStageColumnDefSrc(colOriginalDefs[i]); column.setEtlStageColumnEdwhFlag(BigDecimal.valueOf(1)); if (colPkPos[i] > 0) { column.setEtlStageColumnNkPos(BigDecimal.valueOf(colPkPos[i])); object.setEtlStageSourceNkFlag(BigDecimal.valueOf(1)); } em.persist(column); } else { column = columns.get(0); column.setEtlStageColumnPos(BigDecimal.valueOf(i + 1)); column.setEtlStageColumnDef(colDefs[i]); column.setEtlStageColumnDefSrc(colOriginalDefs[i]); if (colPkPos[i] > 0) { column.setEtlStageColumnNkPos(BigDecimal.valueOf(colPkPos[i])); object.setEtlStageSourceNkFlag(BigDecimal.valueOf(1)); } } } em.getTransaction().commit(); } }
private DB() { emfConfig = Persistence.createEntityManagerFactory("$objectdb/db/config.odb"); emConfig = emfConfig.createEntityManager(); }