EntityManager em = ...; EntityTransaction tx = em.getTransaction(); tx.begin(); for ( int i=0; i<100000; i++ ) { Customer customer = new Customer(.....); em.persist(customer); if ( i % 20 == 0 ) { //20, same as the JDBC batch size //flush a batch of inserts and release memory: em.flush(); em.clear(); } } tx.commit(); session.close();
EntityManager em = emf.createEntityManager(); em.persist(emp); em.close(); // Note: em can be closed before JTA tx committed. em = emf.createEntityManager(); em.persist(emp); Employee emp = em.find(...); // load the employee em.close(); emf.close();
@Test @Priority(10) public void initData() { EntityManager em = getEntityManager(); em.getTransaction().begin(); DateTestEntity dte = new DateTestEntity( new Date( 12345000 ) ); em.persist( dte ); id1 = dte.getId(); em.getTransaction().commit(); em.getTransaction().begin(); dte = em.find( DateTestEntity.class, id1 ); dte.setDateValue( new Date( 45678000 ) ); em.getTransaction().commit(); }
@Test public void validConstructorTest() throws Exception { emf = Persistence.createEntityManagerFactory("hbaseTest"); em = emf.createEntityManager(); BookEntity book = new BookEntity(); book.setBookId(1); book.setTitle("The Complete Reference"); book.setAuthor("Herbert Schildt"); book.setPages(500); em.persist(book); em.clear(); BookEntity book1 = em.find(BookEntity.class, 1); Assert.assertNotNull(book1); Assert.assertEquals(1, book1.getBookId()); Assert.assertEquals("The Complete Reference", book1.getTitle()); Assert.assertEquals("Herbert Schildt", book1.getAuthor()); Assert.assertEquals(500, book1.getPages()); em.remove(book1); BookEntity book2 = em.find(BookEntity.class, 1); Assert.assertNull(book2); em.close(); emf.close(); }
@Test public void testTypedQuery() { Object p1 = prepareData("1", 10); Object p2 = prepareData("2", 20); Object p3 = prepareData("3", 15); em.persist(p1); em.persist(p2); em.persist(p3); TypedQuery<Person> query = em.createQuery("Select p from Person p", Person.class); List<Person> results = query.getResultList(); Assert.assertNotNull(query); Assert.assertNotNull(results); }
@Test @Priority(10) public void initData() { final EntityManager em = getEntityManager(); final SetOwningEntity setOwningEntity = new SetOwningEntity( 1, "parent" ); final SetOwnedEntity setOwnedEntity = new SetOwnedEntity( 2, "child" ); // Revision 1: Initial persist em.getTransaction().begin(); em.persist( setOwningEntity ); em.persist( setOwnedEntity ); em.getTransaction().commit(); em.clear(); ing_id = setOwningEntity.getId(); ed_id = setOwnedEntity.getId(); }
@Test public void testUUID() { EntityManagerFactory emf = Persistence.createEntityManagerFactory("cass_pu"); UUID key = UUID.randomUUID(); CassandraUUIDEntity entity = new CassandraUUIDEntity(); entity.setAge(10); entity.setName("vivek"); entity.setUuidKey(key); EntityManager em = emf.createEntityManager(); em.persist(entity); CassandraUUIDEntity result = em.find(CassandraUUIDEntity.class, key); Assert.assertNotNull(result); Assert.assertEquals(key, result.getUuidKey()); Assert.assertEquals("vivek", result.getName()); Assert.assertEquals(new Integer(10), result.getAge()); }
@Test public void testLazyLoading() { try { Person tony = new Person(); tony.setFirstName("Tony"); tony.setLastName("Blair"); tony.setDriversLicense(new DriversLicense("8439DK")); sharedEntityManager.persist(tony); setComplete(); endTransaction(); startNewTransaction(); sharedEntityManager.clear(); Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId()); assertNotSame(newTony, tony); endTransaction(); assertNotNull(newTony.getDriversLicense()); newTony.getDriversLicense().getSerialNumber(); } finally { deleteFromTables("person", "drivers_license"); } }
EntityManager em = emf.createEntityManager(); try { jtaTx.begin(); em.joinTransaction(); em.persist(emp); em.persist(emp); em.close(); // Note: em can be closed before JTA tx committed.
protected <S extends Score, E extends AbstractTestJpaEntity<S>> void findAssertAndChangeScore( Class<E> jpaEntityClass, Long id, S oldScore, S newScore) { try { transactionManager.begin(); EntityManager em = entityManagerFactory.createEntityManager(); E jpaEntity = em.find(jpaEntityClass, id); em.persist(jpaEntity); assertEquals(oldScore, jpaEntity.getScore()); jpaEntity.setScore(newScore); jpaEntity = em.merge(jpaEntity); transactionManager.commit(); } catch (NotSupportedException | SystemException | RollbackException | HeuristicRollbackException | HeuristicMixedException e) { throw new RuntimeException("Transaction failed.", e); } }
public void uniqueKey() { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("default"); ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory(); ValidatorContext validatorContext = validatorFactory.usingContext(); validatorContext.constraintValidatorFactory(new ConstraintValidatorFactoryImpl(entityManagerFactory)); Validator validator = validatorContext.getValidator(); EntityManager em = entityManagerFactory.createEntityManager(); User se = new User("abc", poizon); Set<ConstraintViolation<User>> violations = validator.validate(se); System.out.println("Size:- " + violations.size()); em.getTransaction().begin(); em.persist(se); em.getTransaction().commit(); User se1 = new User("abc"); violations = validator.validate(se1); System.out.println("Size:- " + violations.size()); }
private void withBatch() { int entityCount = 100; //tag::batch-session-batch-insert-example[] EntityManager entityManager = null; EntityTransaction txn = null; try { entityManager = entityManagerFactory().createEntityManager(); txn = entityManager.getTransaction(); txn.begin(); int batchSize = 25; for ( int i = 0; i < entityCount; i++ ) { if ( i > 0 && i % batchSize == 0 ) { //flush a batch of inserts and release memory entityManager.flush(); entityManager.clear(); } Person Person = new Person( String.format( "Person %d", i ) ); entityManager.persist( Person ); } txn.commit(); } catch (RuntimeException e) { if ( txn != null && txn.isActive()) txn.rollback(); throw e; } finally { if (entityManager != null) { entityManager.close(); } } //end::batch-session-batch-insert-example[] }
@Override public void saveMapping(Context context, Long ksessionId, String ownerId) { EntityManagerInfo info = getEntityManager(context); EntityManager em = info.getEntityManager(); em.persist(new ContextMappingInfo(resolveContext(context, em).getContextId().toString(), ksessionId, ownerId)); if (!info.isShared()) { em.close(); } }
@Test @Priority(10) public void initData() { EntityManager em = getEntityManager(); // Revision 1 em.getTransaction().begin(); StrTestEntity entity = new StrTestEntity( "x" ); em.persist( entity ); em.getTransaction().commit(); id = entity.getId(); // Revision 2 em.getTransaction().begin(); entity = em.find( StrTestEntity.class, entity.getId() ); entity.setStr( "y" ); entity = em.merge( entity ); em.getTransaction().commit(); em.close(); }
@Test @Priority(10) public void initData() { EntityManager em = getEntityManager(); em.getTransaction().begin(); PropertyAccessTypeEntity pate = new PropertyAccessTypeEntity( "data" ); em.persist( pate ); id1 = pate.getId(); em.getTransaction().commit(); em.getTransaction().begin(); pate = em.find( PropertyAccessTypeEntity.class, id1 ); pate.writeData( "data2" ); em.getTransaction().commit(); }
@Test(expected = RuntimeException.class) public void testTransactionRollback() throws InterruptedException { // Trying to persist an entity - however the listener should throw an exception, so the entity // shouldn't be persisted EntityManager em = getEntityManager(); em.getTransaction().begin(); StrTestEntity te = new StrTestEntity( "x" ); em.persist( te ); em.getTransaction().commit(); }