当前位置: 首页>>代码示例>>Java>>正文


Java EntityManagerFactory类代码示例

本文整理汇总了Java中javax.persistence.EntityManagerFactory的典型用法代码示例。如果您正苦于以下问题:Java EntityManagerFactory类的具体用法?Java EntityManagerFactory怎么用?Java EntityManagerFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


EntityManagerFactory类属于javax.persistence包,在下文中一共展示了EntityManagerFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
public static void main(String[] args) {
	EntityManagerFactory entityManagerFactory = Persistence
			.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
	EntityManager em = entityManagerFactory.createEntityManager();

	em.getTransaction().begin();
	
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<User> cq = cb.createQuery(User.class);
	Root<User> from = cq.from(User.class);
	Join<User, Role> join = from.join("role", JoinType.LEFT);
	cq.where(cb.equal(join.get("name"), "ordinary"));
	TypedQuery<User> tq = em.createQuery(cq);
	List<User> users = tq.getResultList();
	for (User u : users) {
		System.out.println(u.getLastName());
	}
	
	em.getTransaction().commit();
	em.close();
	entityManagerFactory.close();
}
 
开发者ID:PawelBogdan,项目名称:BecomeJavaHero,代码行数:23,代码来源:App8.java

示例2: jpa

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Test
public void jpa() {
  EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa");

  Employee employee = new Employee();
  EntityManager entityManager = entityManagerFactory.createEntityManager();
  entityManager.getTransaction().begin();
  entityManager.persist(employee);
  entityManager.getTransaction().commit();
  entityManager.close();
  entityManagerFactory.close();

  assertNotNull(employee.id);

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(8, finishedSpans.size());

  checkSpans(finishedSpans);
  assertNull(mockTracer.activeSpan());
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:21,代码来源:HibernateTest.java

示例3: jpaWithActiveSpanOnlyNoParent

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Test
public void jpaWithActiveSpanOnlyNoParent() {
  EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa_active_span_only");

  Employee employee = new Employee();
  EntityManager entityManager = entityManagerFactory.createEntityManager();
  entityManager.getTransaction().begin();
  entityManager.persist(employee);
  entityManager.getTransaction().commit();
  entityManager.close();
  entityManagerFactory.close();

  assertNotNull(employee.id);

  List<MockSpan> finishedSpans = mockTracer.finishedSpans();
  assertEquals(0, finishedSpans.size());
  assertNull(mockTracer.scopeManager().active());
}
 
开发者ID:opentracing-contrib,项目名称:java-p6spy,代码行数:19,代码来源:HibernateTest.java

示例4: getEntityManager

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
private synchronized EntityManager getEntityManager(final PersistenceUnitInfo unit) {
    if (entityManagers.containsKey(unit.getPersistenceUnitName())) {
        return entityManagers.get(unit.getPersistenceUnitName());
    }
    final String providerClassName = unit.getPersistenceProviderClassName();
    try {
        final PersistenceProvider provider = (PersistenceProvider) Class.forName(providerClassName).newInstance();
        final EntityManagerFactory emf = provider.createContainerEntityManagerFactory(unit, new HashMap());
        final EntityManager entityManager = emf.createEntityManager();
        entityManagers.put(unit.getPersistenceUnitName(), entityManager);
        entityManager.getTransaction().begin();
        return entityManager;
    } catch (final IllegalAccessException | InstantiationException | ClassNotFoundException e) {
        throw new TestEEfiException("Failed to load persistence provider class " + providerClassName, e);
    }
}
 
开发者ID:dajudge,项目名称:testee.fi,代码行数:17,代码来源:JpaInjectionServicesImpl.java

示例5: getEntityManagerFactory

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
/**
 * Opens an {@link EntityManagerFactory} for the provided {@code persistenceUnit} if not already opened/cached.
 *
 * @param persistenceUnit The name of the persistence unit
 * @return The {@link EntityManagerFactory} for {@code persistenceUnit}
 */
private static synchronized EntityManagerFactory getEntityManagerFactory(String persistenceUnit) {
    if (persistenceUnit.isEmpty() && DEFAULT_PERSISTENCE_UNIT != null) {
        persistenceUnit = DEFAULT_PERSISTENCE_UNIT;
    }
    if (!SESSION_FACTORY_STORE.containsKey(persistenceUnit)) {
        LOG.debug("Create new EntityManagerFactory for persistence unit {}", persistenceUnit);
        SESSION_FACTORY_STORE.put(
              persistenceUnit,
              Persistence.createEntityManagerFactory(
                    persistenceUnit.isEmpty() ? null : persistenceUnit,
                    PERSISTENCE_PROPERTIES.get(persistenceUnit)
              )
        );
    }
    return SESSION_FACTORY_STORE.get(persistenceUnit);
}
 
开发者ID:mcarleio,项目名称:strix,代码行数:23,代码来源:StrixManager.java

示例6: transactionManager

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Bean
@Autowired
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
	JpaTransactionManager txManager = new JpaTransactionManager();
	txManager.setEntityManagerFactory(emf);
	return txManager;
}
 
开发者ID:dockersamples,项目名称:atsea-sample-shop-app,代码行数:8,代码来源:JpaConfiguration.java

示例7: setupFactory

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
/**
 * @param unitName   persistence unit name
 * @param properties nullable properties for overriding
 * @return created TransactionProvider
 */
public static TransactionProvider setupFactory(String unitName, Map<String, String> properties) {
    if (!providers.containsKey(unitName)) {
        synchronized (HibernateUtils.class) {
            if (!providers.containsKey(unitName)) {
                // Setup Factory & Provider
                EntityManagerFactory factory = Persistence.createEntityManagerFactory(unitName, properties);
                TransactionProvider provider = new TransactionProvider(factory);
                // Put to Map
                providers.put(unitName, provider);
                return provider;
            }
        }
    }
    throw new RuntimeException(new IllegalStateException("Factory already initialized."));
}
 
开发者ID:Fuxingloh,项目名称:hibernate-utils,代码行数:21,代码来源:HibernateUtils.java

示例8: entityManagerFactory

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Bean
public EntityManagerFactory entityManagerFactory() throws SQLException {
	HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
	// vendorAdapter.setGenerateDdl(true); // Crea la tabella e le colonne quando non esistono
	vendorAdapter.setShowSql(config.getShowSql());
	vendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5InnoDBDialect");
	LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
	factory.setJpaVendorAdapter(vendorAdapter);
	List<String> packages = config.getDbEntityPackages();
	packages.add("net.yadaframework.persistence.entity");
	packages.add("net.yadaframework.security.persistence.entity");
	packages.add("net.yadaframework.cms.persistence.entity");
	packages.add("net.yadaframework.commerce.persistence.entity");
	log.info("Scanning packages for entities: {}", StringUtils.join(packages, ","));
	factory.setPackagesToScan(packages.toArray(new String[]{}));
	factory.setDataSource(dataSource());
	factory.afterPropertiesSet();
	return factory.getObject();
}
 
开发者ID:xtianus,项目名称:yadaframework,代码行数:20,代码来源:YadaJpaConfig.java

示例9: btn_kabuletActionPerformed

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
private void btn_kabuletActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_kabuletActionPerformed

// TODO add your handling code here:
        if (tbl_arkadaslistesi.getSelectedRow() < 0) {
            JOptionPane.showMessageDialog(rootPane, "Bir kullanıcı Seçmelisiniz");
            return;
        }

        int id = (int) dtm2.getValueAt(tbl_arkadaslistesi.getSelectedRow(), 0);

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("SosyalMedyaAppWithDatabasePU");
        EntityManager em = emf.createEntityManager();

        Query q = em.createQuery("UPDATE ARKADASLIK u SET u.kabulDurumu=true  WHERE u.idUserKabul=:kabulid AND U.idUserIstek=:istekid");
        q.setParameter("istekid", id);
        q.setParameter("kabulid", Frm_Login.loginuser.getId());

        em.getTransaction().begin();
        q.executeUpdate();
        em.getTransaction().commit();


    }
 
开发者ID:sametkaya,项目名称:Java_Swing_Programming,代码行数:24,代码来源:Frm_KullaniciSayfasi.java

示例10: btn_silActionPerformed

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
private void btn_silActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_silActionPerformed
    // TODO add your handling code here:
    if (tbl_arkadaslistesi.getSelectedRow() < 0) {
        JOptionPane.showMessageDialog(rootPane, "Bir kullanıcı Seçmelisiniz");
        return;
    }

    int id = (int) dtm2.getValueAt(tbl_arkadaslistesi.getSelectedRow(), 0);

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("SosyalMedyaAppWithDatabasePU");
    EntityManager em = emf.createEntityManager();

    Query q = em.createQuery("DELETE FROM ARKADASLIK u WHERE u.idUserKabul=:kabulid AND U.idUserIstek=:istekid");
    q.setParameter("istekid", id);
    q.setParameter("kabulid", Frm_Login.loginuser.getId());

    em.getTransaction().begin();
    q.executeUpdate();
    em.getTransaction().commit();
    
    
}
 
开发者ID:sametkaya,项目名称:Java_Swing_Programming,代码行数:23,代码来源:Frm_KullaniciSayfasi.java

示例11: testPersistence

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Test
public void testPersistence() {
    // Make sure derby.log is in target
    System.setProperty("derby.stream.error.file", "target/derby.log");
    TaskServiceImpl taskServiceImpl = new TaskServiceImpl();
    EntityManagerFactory emf = createTestEMF();
    final EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    taskServiceImpl.em = em;

    TaskService taskService = taskServiceImpl;

    Task task = new Task();
    task.setId(1);
    task.setTitle("test");
    taskService.addTask(task);

    Task task2 = taskService.getTask(1);
    Assert.assertEquals(task.getTitle(), task2.getTitle());
    em.getTransaction().commit();
    em.close();
}
 
开发者ID:apache,项目名称:aries-jpa,代码行数:23,代码来源:TaskServiceImplTest.java

示例12: jpa_with_parent

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Test
public void jpa_with_parent() {

  try (Scope ignored = mockTracer.buildSpan("parent").startActive(true)) {
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("jpa");

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    entityManager.getTransaction().begin();
    entityManager.persist(new Employee());
    entityManager.persist(new Employee());
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManagerFactory.close();
  }

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(11, spans.size());
  checkSameTrace(spans);
  assertNull(mockTracer.activeSpan());
}
 
开发者ID:opentracing-contrib,项目名称:java-jdbc,代码行数:22,代码来源:HibernateTest.java

示例13: registerBeanDefinitions

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry registry) {
	// Register JPA Datastore (transactional)
	if (beanFactory instanceof ListableBeanFactory) {
		String[] emfBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
				(ListableBeanFactory) beanFactory, EntityManagerFactory.class, true, false);
		if (emfBeanNames == null || emfBeanNames.length == 0) {
			emfBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors((ListableBeanFactory) beanFactory,
					AbstractEntityManagerFactoryBean.class, true, false);
		}
		if (emfBeanNames != null && emfBeanNames.length == 1) {
			String emfBeanName = (emfBeanNames[0].startsWith("&")) ? emfBeanNames[0].substring(1) : emfBeanNames[0];
			JpaDatastoreRegistrar.registerDatastore(registry, null, PrimaryMode.AUTO, emfBeanName, true, false,
					beanClassLoader);
		}
	}
}
 
开发者ID:holon-platform,项目名称:holon-datastore-jpa,代码行数:18,代码来源:JpaDatastoreAutoConfigurationRegistrar.java

示例14: btn_degistirActionPerformed

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
private void btn_degistirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_degistirActionPerformed
    // TODO add your handling code here:
       // UPDATE
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
    EntityManager em = emf.createEntityManager();

    Query q = em.createQuery("UPDATE Hasta h SET h.adi=:ad,h.soyadi=:soyad, h.idDoktor=:did  WHERE h.id=:id");
    q.setParameter("ad", txt_hastaadi.getText());
    q.setParameter("soyad", txt_hastasoyadi.getText());
    q.setParameter("did", doktorlar.get(cmb_doktorlar.getSelectedIndex()).getId());
    q.setParameter("id", tbl_hastalar.getValueAt(tbl_hastalar.getSelectedRow(), 0));
    em.getTransaction().begin();
    q.executeUpdate();
    em.getTransaction().commit();
    ///
    dtm.setRowCount(0);
    q = em.createQuery("SELECT h FROM Hasta h");

    List<Hasta> hastalar = q.getResultList();
    for (Hasta hs : hastalar) {
        dtm.addRow(new Object[]{hs.getId(), hs.getAdi(), hs.getSoyadi(), hs.getIdDoktor()});
    }
    tbl_hastalar.setModel(dtm);


}
 
开发者ID:sametkaya,项目名称:Java_Swing_Programming,代码行数:27,代码来源:Soru1.java

示例15: lifeCycleTest

import javax.persistence.EntityManagerFactory; //导入依赖的package包/类
@Test
public void lifeCycleTest() {
    EntityManagerFactory emf = mockEmf();
    Coordinator coordinator = new DummyCoordinator();

    EMSupplierImpl emSupplier = new EMSupplierImpl("myunit", emf, coordinator);
    assertIllegalState(emSupplier);
    coordinator.begin("test", 0);
    EntityManager em = emSupplier.get();
    Assert.assertNotNull("EM should be present after preCall", em);
    coordinator.begin("testinner", 0);
    Assert.assertSame("Same EM for inner preCall", em, emSupplier.get());
    coordinator.pop().end();
    Assert.assertSame("EM must still be the same after inner postCall", em, emSupplier.get());
    coordinator.pop().end();
    assertIllegalState(emSupplier);
    
    boolean clean = emSupplier.close();
    Assert.assertTrue("Shutdown should be clean", clean);
}
 
开发者ID:apache,项目名称:aries-jpa,代码行数:21,代码来源:EmSupplierTest.java


注:本文中的javax.persistence.EntityManagerFactory类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。