@SuppressWarnings("unchecked") @Override public <T> T persist(T object) { check(); this.em.persist( object ); if( this.pessimisticLocking ) { this.em.flush(); Object primaryKey = getFieldValueWithAnnotation(object, Id.class); return (T) this.em.find( object.getClass(), primaryKey, LockModeType.PESSIMISTIC_WRITE ); } return object; }
@Test @Priority(10) public void initData() { // Revision 1 EntityManager em = getEntityManager(); em.getTransaction().begin(); StrTestEntity fe = new StrTestEntity( "x" ); em.persist( fe ); em.flush(); em.remove( em.find( StrTestEntity.class, fe.getId() ) ); em.flush(); em.getTransaction().commit(); // id = fe.getId(); }
Map<String, Client> clients = (Map<String, Client>) em.getDelegate(); Client client = clients.get(PERSISTENCE_UNIT); PrimeUser result = em.find(PrimeUser.class, key); Assert.assertNotNull(result); em.remove(result); em.flush(); em.close(); em = emf.createEntityManager(); client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); result = em.find(PrimeUser.class, key); Assert.assertNull(result);
protected static <T> T doTransactionalOptimisticUpdate(String name, UpdateOperation<T> operation, Class<? extends T> entityClass, Object identifier, PlatformTransactionManager transactionManager, EntityManager entityManager) { TransactionStatus transactionStatus = TransactionUtils.createTransaction( name, TransactionDefinition.PROPAGATION_REQUIRES_NEW, transactionManager); T entity; try { entity = entityManager.find(entityClass, identifier); if (operation instanceof ValidatedUpdateOperation && !((ValidatedUpdateOperation<T>) operation).isValid(entity)) { log.debug("Entity state was found to be invalid while trying to perform " + name + " on " + entityClass.getSimpleName() + " with id: " + identifier); throw new OptimisticLockInvalidStateException("Unable to perform " + name + " on " + entityClass.getSimpleName() + " with id: " + identifier + ". Aborting update due to invalid state."); } operation.update(entity); entityManager.flush(); TransactionUtils.finalizeTransaction(transactionStatus, transactionManager, false); } catch (RuntimeException e) { TransactionUtils.finalizeTransaction(transactionStatus, transactionManager, true); throw e; } return entity; } }
@Test @Priority(10) public void initData() { // Revision 1 EntityManager em = getEntityManager(); em.getTransaction().begin(); StrTestEntity fe = new StrTestEntity( "x" ); em.persist( fe ); em.flush(); em.getTransaction().commit(); // Revision 2 em.getTransaction().begin(); fe = em.find( StrTestEntity.class, fe.getId() ); fe.setStr( "y" ); em.flush(); em.remove( em.find( StrTestEntity.class, fe.getId() ) ); em.flush(); em.getTransaction().commit(); // id = fe.getId(); }
Client client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); PrimeUser result = em.find(PrimeUser.class, key); Assert.assertNotNull(result); Assert.assertEquals("After merge", result.getTweetBody()); // assertion em.remove(result); em.flush(); em.close(); em = emf.createEntityManager(); clients = (Map<String, Client>) em.getDelegate(); client = clients.get(PERSISTENCE_UNIT); result = em.find(PrimeUser.class, key); Assert.assertNull(result);
person.setName("John Doe"); entityManager.persist( person ); entityManager.remove( person ); entityManager.persist( person ); Long personId = person.getId(); Person person = entityManager.find( Person.class, personId ); book.setId( 1L ); book.setIsbn( "123-456-7890" ); entityManager.persist( book ); book.setAuthor( session.load( Person.class, personId ) ); Person person = entityManager.find( Person.class, personId ); person.setName("John Doe"); entityManager.flush(); Person person = entityManager.find( Person.class, personId );
Client client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); CassandraPrimeUser result = em.find(CassandraPrimeUser.class, key); Assert.assertNotNull(result); Assert.assertNull(result.getKey().getFullName()); em.remove(result); em.flush(); em.close(); em = emf.createEntityManager(); client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); result = em.find(CassandraPrimeUser.class, key); Assert.assertNull(result);
@Override @Transactional("blTransactionManager") public void delete(OrderItem orderItem) { if (!em.contains(orderItem)) { orderItem = readOrderItemById(orderItem.getId()); } if (GiftWrapOrderItem.class.isAssignableFrom(orderItem.getClass())) { final GiftWrapOrderItem giftItem = (GiftWrapOrderItem) orderItem; for (OrderItem wrappedItem : giftItem.getWrappedItems()) { wrappedItem.setGiftWrapOrderItem(null); wrappedItem = save(wrappedItem); } } em.remove(orderItem); em.flush(); }
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();
@Test public void compositeKeytest() { emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT, puProperties); em = emf.createEntityManager(); UUID timeLineId = UUID.randomUUID(); CassandraCompoundKey key = new CassandraCompoundKey("shahid", 1, timeLineId); CassandraPrimeUser user = new CassandraPrimeUser(key); user.setTweetBody("my first tweet"); user.setTweetDate(currentDate); em.persist(user); CassandraPrimeUser user2 = new CassandraPrimeUser(key); user2.setTweetBody("my second tweet"); user2.setTweetDate(currentDate); em.flush(); // optional,just to clear persistence cache. em.clear(); CassandraPrimeUser u = em.find(CassandraPrimeUser.class, key); Assert.assertEquals("my first tweet", u.getTweetBody()); em.merge(user2); u = em.find(CassandraPrimeUser.class, key); Assert.assertEquals("my second tweet", u.getTweetBody()); em.remove(user2); }
Client client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); CassandraPrimeUser result = em.find(CassandraPrimeUser.class, key); Assert.assertNotNull(result); Assert.assertNull(result.getKey().getFullName()); em.remove(result); em.flush(); em.close(); em = emf.createEntityManager(); client = clients.get(PERSISTENCE_UNIT); ((CassandraClientBase) client).setCqlVersion("3.0.0"); result = em.find(CassandraPrimeUser.class, key); Assert.assertNull(result);