Java Code Examples for javax.persistence.Persistence.createEntityManagerFactory()

The following are Jave code examples for showing how to use createEntityManagerFactory() of the javax.persistence.Persistence class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: java-jdbc   File: HibernateTest.java   Source Code and License Vote up 9 votes
@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());
}
 
Example 2
Project: bibliometrics   File: RolePermissionDAO.java   Source Code and License Vote up 7 votes
/**
 * 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;
}
 
Example 3
Project: BecomeJavaHero   File: App7.java   Source Code and License Vote up 6 votes
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();
}
 
Example 4
Project: testing_security_development_enterprise_systems   File: IdHandlingTest.java   Source Code and License Vote up 6 votes
@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
     */
}
 
Example 5
Project: Java_Swing_Programming   File: Frm_KullaniciSayfasi.java   Source Code and License Vote up 5 votes
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();


}
 
Example 6
Project: BecomeJavaHero   File: JPABookService.java   Source Code and License Vote up 5 votes
@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();
}
 
Example 7
Project: LivroJavaComoProgramar10Edicao   File: DisplayAuthors.java   Source Code and License Vote up 5 votes
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());
         }
      );
}
 
Example 8
Project: spring-data-examples   File: BasicSample.java   Source Code and License Vote up 5 votes
/**
 * 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();
}
 
Example 9
Project: BecomeJavaHero   File: JPAPublisherService.java   Source Code and License Vote up 5 votes
@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();
}
 
Example 10
Project: Java_Swing_Programming   File: Hasta.java   Source Code and License Vote up 5 votes
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;
}
 
Example 11
Project: BecomeJavaHero   File: JPABookService.java   Source Code and License Vote up 5 votes
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();
}
 
Example 12
Project: cf-mta-deploy-service   File: AbstractOperationDaoParameterizedTest.java   Source Code and License Vote up 5 votes
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;
}
 
Example 13
Project: aries-jpa   File: TaskServiceImplTest.java   Source Code and License Vote up 5 votes
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;
}
 
Example 14
Project: aries-jpa   File: TaskServiceImplTest.java   Source Code and License Vote up 5 votes
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;
}
 
Example 15
Project: testing_security_development_enterprise_systems   File: IdCreationTest.java   Source Code and License Vote up 4 votes
@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());
}
 
Example 16
Project: Trabalhos-FIAP   File: DaoGenerico.java   Source Code and License Vote up 4 votes
protected DaoGenerico(Class<T> classe) {
	this.classe = classe;
	this.emf = Persistence.createEntityManagerFactory("TelegramBotDigitalBank");
	this.em = this.emf.createEntityManager();
}
 
Example 17
Project: syndesis   File: ConnectorTest.java   Source Code and License Vote up 4 votes
@BeforeClass
public static void setUp() {
	EntityManagerFactory factory = Persistence.createEntityManagerFactory("io.syndesis.runtime.db");
       em = factory.createEntityManager();
}
 
Example 18
Project: programacion4-final   File: UsersType.java   Source Code and License Vote up 4 votes
public EntityManager getEntityManager(){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("warehouse_proyectPU");
    EntityManager em = emf.createEntityManager();
    return em;
}
 
Example 19
Project: Mod-Tools   File: PersistenceProvider.java   Source Code and License Vote up 4 votes
public PersistenceProvider() {
    entityManager = Persistence.createEntityManagerFactory("de.idrinth_Stellaris.ModTools");
}
 
Example 20
Project: testing_security_development_enterprise_systems   File: UserTest.java   Source Code and License Vote up 4 votes
@Before
public void init() {
    factory = Persistence.createEntityManagerFactory("DB");
    em = factory.createEntityManager();
}