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


Java Persistence.createEntityManagerFactory方法代码示例

本文整理汇总了Java中javax.persistence.Persistence.createEntityManagerFactory方法的典型用法代码示例。如果您正苦于以下问题:Java Persistence.createEntityManagerFactory方法的具体用法?Java Persistence.createEntityManagerFactory怎么用?Java Persistence.createEntityManagerFactory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.persistence.Persistence的用法示例。


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

示例1: jpa

import javax.persistence.Persistence; //导入方法依赖的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

示例2: testPersistTwiceInNewFactory

import javax.persistence.Persistence; //导入方法依赖的package包/类
@Test
public void testPersistTwiceInNewFactory() {
    User02 user02 = new User02();
    user02.setId(1L);

    boolean persisted = persistInATransaction(user02);
    assertTrue(persisted);

    em.close();
    factory.close();
    factory = Persistence.createEntityManagerFactory("DB");
    em = factory.createEntityManager();

    persisted = persistInATransaction(user02);
    assertTrue(persisted);
    /*
        Here we managed to commit, because createEntityManagerFactory does
        create a new database, as we used "create-drop" in the persisten.xml file
     */
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:21,代码来源:IdHandlingTest.java

示例3: getPermission

import javax.persistence.Persistence; //导入方法依赖的package包/类
/**
 * retrieves the <code>RolesPermission</code> by the role.
 * 
 * @param role
 *            the role
 * @return userPermissions the list of permissions of the user
 * 
 */
public static Set<String> getPermission(String role) {
	EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
	EntityManager em = emf.createEntityManager();
	EntityTransaction tx = em.getTransaction();
	tx.begin();
	CriteriaBuilder cb = em.getCriteriaBuilder();
	CriteriaQuery<RolePermission> q = cb.createQuery(RolePermission.class);
	Root<RolePermission> c = q.from(RolePermission.class);
	q.select(c).where(cb.equal(c.get("roleName"), role));
	TypedQuery<RolePermission> query = em.createQuery(q);
	List<RolePermission> permissions = query.getResultList();
	Set<String> userPermissions = new HashSet<String>();
	for (RolePermission permission : permissions)
		userPermissions.add(permission.getPermission());
	tx.commit();
	em.close();
	return userPermissions;
}
 
开发者ID:ETspielberg,项目名称:bibliometrics,代码行数:27,代码来源:RolePermissionDAO.java

示例4: main

import javax.persistence.Persistence; //导入方法依赖的package包/类
public static void main(String[] args) {
	EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
	EntityManager em = entityManagerFactory.createEntityManager();
	
	em.getTransaction().begin();
	
	Query query = em
			.createQuery("Select user from User user join user.role role where role.name = :name");
	query.setParameter("name", "ordinary");
	List<User> users = query.getResultList();
	
	for (User user : users) {
		System.out.println(user.getFirstName() + "   " + user.getRole().getName());
	}
	
	em.getTransaction().commit();
	em.close();
	entityManagerFactory.close();
}
 
开发者ID:PawelBogdan,项目名称:BecomeJavaHero,代码行数:20,代码来源:App7.java

示例5: btn_ekleActionPerformed

import javax.persistence.Persistence; //导入方法依赖的package包/类
private void btn_ekleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ekleActionPerformed
    // TODO add your handling code here:
    if (tbl_kisiliste.getSelectedRow() < 0) {
        JOptionPane.showMessageDialog(rootPane, "Bir kullanıcı Seçmelisiniz");
        return;
    }
    int id = (int) dtm1.getValueAt(tbl_kisiliste.getSelectedRow(), 0);

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

    Query q = em.createQuery("SELECT u FROM USER_1 u WHERE u.id=:id");
    q.setParameter("id", id);

    USER_1 gelen = (USER_1) q.getSingleResult();
    if (gelen == null) {

        return;
    }

    ARKADASLIK yeni = new ARKADASLIK();
    yeni.setId(3);
    yeni.setIdUserIstek(Frm_Login.loginuser.getId());
    yeni.setIdUserKabul(gelen.getId());
    yeni.setKabulDurumu(false);
    em.getTransaction().begin();
    em.persist(yeni);
    em.getTransaction().commit();


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

示例6: getAllBooks

import javax.persistence.Persistence; //导入方法依赖的package包/类
@Override
public List<Book> getAllBooks() {
	EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
	EntityManager em = entityManagerFactory.createEntityManager();
	
	// begining of transaction
	em.getTransaction().begin();
	Query query = em.createQuery("Select b from Book b");
	return query.getResultList();
}
 
开发者ID:PawelBogdan,项目名称:BecomeJavaHero,代码行数:11,代码来源:JPABookService.java

示例7: main

import javax.persistence.Persistence; //导入方法依赖的package包/类
public static void main(String[] args)
{
   // create an EntityManagerFactory for the persistence unit
   EntityManagerFactory entityManagerFactory = 
      Persistence.createEntityManagerFactory(
         "BooksDatabaseExamplesPU");

   // create an EntityManager for interacting with the persistence unit
   EntityManager entityManager = 
      entityManagerFactory.createEntityManager();

   // create a dynamic TypedQuery<Authors> that selects all authors
   TypedQuery<Authors> findAllAuthors = entityManager.createQuery(
      "SELECT author FROM Authors AS author", Authors.class);
   
   // display List of Authors
   System.out.printf("Authors Table of Books Database:%n%n");
   System.out.printf("%-12s%-13s%s%n", 
      "Author ID", "First Name", "Last Name");
   
   // get all authors, create a stream and display each author
   findAllAuthors.getResultList().stream()
      .forEach((author) -> 
         {
            System.out.printf("%-12d%-13s%s%n", author.getAuthorid(), 
               author.getFirstname(), author.getLastname());
         }
      );
}
 
开发者ID:cleitonferreira,项目名称:LivroJavaComoProgramar10Edicao,代码行数:30,代码来源:DisplayAuthors.java

示例8: setUp

import javax.persistence.Persistence; //导入方法依赖的package包/类
/**
 * Sets up a {@link SimpleJpaRepository} instance.
 */
@Before
public void setUp() {

	EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
	em = factory.createEntityManager();

	userRepository = new SimpleJpaRepository<User, Long>(User.class, em);

	em.getTransaction().begin();
}
 
开发者ID:Just-Fun,项目名称:spring-data-examples,代码行数:14,代码来源:BasicSample.java

示例9: getAllPublishers

import javax.persistence.Persistence; //导入方法依赖的package包/类
@Override
public List<Publisher> getAllPublishers() {
	EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
	EntityManager em = entityManagerFactory.createEntityManager();
	
	// begining of transaction
	em.getTransaction().begin();
	Query query = em.createQuery("Select p from Publisher p");
	return query.getResultList();
}
 
开发者ID:PawelBogdan,项目名称:BecomeJavaHero,代码行数:11,代码来源:JPAPublisherService.java

示例10: GetAllHasta

import javax.persistence.Persistence; //导入方法依赖的package包/类
public static  List<Hasta> GetAllHasta() {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("BP2_LAB2PU");
    EntityManager em = emf.createEntityManager();

    Query q = em.createQuery("SELECT h FROM Hasta h");
    List<Hasta> hastalar = q.getResultList();
    return hastalar;
}
 
开发者ID:sametkaya,项目名称:Java_Swing_Programming,代码行数:9,代码来源:Hasta.java

示例11: getBooksByCategory

import javax.persistence.Persistence; //导入方法依赖的package包/类
public List<Book> getBooksByCategory() {
	EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
	EntityManager em = entityManagerFactory.createEntityManager();
	
	// begining of transaction
	em.getTransaction().begin();
	Category category = em.find(Category.class, Integer.parseInt(categoryId));
	return category.getBooks();
}
 
开发者ID:PawelBogdan,项目名称:BecomeJavaHero,代码行数:10,代码来源:JPABookService.java

示例12: createDao

import javax.persistence.Persistence; //导入方法依赖的package包/类
public static OperationDao createDao() {
    OperationDao dao = new OperationDao();
    OperationDtoDao dtoDao = new OperationDtoDao();
    dtoDao.emf = Persistence.createEntityManagerFactory("OperationManagement");
    dao.dao = dtoDao;
    dao.operationFactory = new OperationFactory();
    return dao;
}
 
开发者ID:SAP,项目名称:cf-mta-deploy-service,代码行数:9,代码来源:AbstractOperationDaoParameterizedTest.java

示例13: createTestEMF

import javax.persistence.Persistence; //导入方法依赖的package包/类
private EntityManagerFactory createTestEMF() {
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    properties.put("javax.persistence.jdbc.url", "jdbc:derby:memory:TEST;create=true");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
    return emf;
}
 
开发者ID:apache,项目名称:aries-jpa,代码行数:8,代码来源:TaskServiceImplTest.java

示例14: createTestEMF

import javax.persistence.Persistence; //导入方法依赖的package包/类
private EntityManagerFactory createTestEMF() {
    Map<String, String> properties = new HashMap<String, String>();
    properties.put("javax.persistence.jdbc.driver", "org.apache.derby.jdbc.EmbeddedDriver");
    properties.put("javax.persistence.jdbc.url", "jdbc:derby:target/test;create=true");
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("tasklist", properties);
    return emf;
}
 
开发者ID:apache,项目名称:aries-jpa,代码行数:8,代码来源:TaskServiceImplTest.java

示例15: testIdPersistence

import javax.persistence.Persistence; //导入方法依赖的package包/类
@Test
public void testIdPersistence(){

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("DB");//same name as in persistence.xml
    EntityManager em = factory.createEntityManager();//it works as a cache/buffer until we commit a transaction

    User01 user01 = new User01();
    user01.setName("AName");
    user01.setSurname("ASurname");

    // by default, no id, until data committed to the database
    assertNull(user01.getId());

    //committing data to database needs to be inside a transaction
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    try{
        /*
            The following is actually executing this SQL statement:

            insert into User01 (name, surname, id) values (?, ?, ?)

         */
        em.persist(user01);

        //there can be several operations on the "cache" EntityManager before we actually commit the transaction
        tx.commit();
    } catch (Exception e){
        //abort the transaction if there was any exception
        tx.rollback();
        fail();//fail the test
    } finally {
        //in any case, make sure to close the opened resources
        em.close();
        factory.close();
    }

    //id should have now be set
    assertNotNull(user01.getId());
    System.out.println("GENERATED ID: "+user01.getId());
}
 
开发者ID:arcuri82,项目名称:testing_security_development_enterprise_systems,代码行数:43,代码来源:IdCreationTest.java


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