Search in sources :

Example 31 with EntityTransaction

use of javax.persistence.EntityTransaction in project oxTrust by GluuFederation.

the class InumService method addInum.

/**
 * Add an Inum to the DB
 *
 * @return True if user exist
 */
public boolean addInum(EntityManager inumEntryManager, String inum, String type) {
    boolean successs = false;
    EntityTransaction entityTransaction = inumEntryManager.getTransaction();
    entityTransaction.begin();
    try {
        // Prepare inum
        InumSqlEntry inumEntry = new InumSqlEntry();
        inumEntry.setInum(inum);
        inumEntry.setType(type);
        // add inum
        inumEntryManager.persist(inumEntry);
        successs = true;
    } finally {
        if (successs) {
            // Commit transaction
            entityTransaction.commit();
        } else {
            // Rollback transaction
            entityTransaction.rollback();
        }
    }
    return successs;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) InumSqlEntry(org.gluu.oxtrust.model.sql.InumSqlEntry)

Example 32 with EntityTransaction

use of javax.persistence.EntityTransaction in project oxTrust by GluuFederation.

the class InumService method addInumByObject.

/**
 * Add an Inum to the DB by object
 *
 * @return True if user exist
 */
public boolean addInumByObject(EntityManager inumEntryManager, InumSqlEntry inumEntry) {
    boolean successs = false;
    EntityTransaction entityTransaction = inumEntryManager.getTransaction();
    entityTransaction.begin();
    try {
        // add inum
        inumEntryManager.persist(inumEntry);
        successs = true;
    } finally {
        if (successs) {
            // Commit transaction
            entityTransaction.commit();
        } else {
            // Rollback transaction
            entityTransaction.rollback();
        }
    }
    return successs;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction)

Example 33 with EntityTransaction

use of javax.persistence.EntityTransaction in project tomee by apache.

the class JpaServlet method doGet.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/plain");
    ServletOutputStream out = response.getOutputStream();
    out.println("@PersistenceUnit=" + emf);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();
    JpaBean jpaBean = new JpaBean();
    jpaBean.setName("JpaBean");
    em.persist(jpaBean);
    transaction.commit();
    transaction.begin();
    Query query = em.createQuery("SELECT j FROM JpaBean j WHERE j.name='JpaBean'");
    jpaBean = (JpaBean) query.getSingleResult();
    out.println("Loaded " + jpaBean);
    em.remove(jpaBean);
    transaction.commit();
    transaction.begin();
    query = em.createQuery("SELECT count(j) FROM JpaBean j WHERE j.name='JpaBean'");
    int count = ((Number) query.getSingleResult()).intValue();
    if (count == 0) {
        out.println("Removed " + jpaBean);
    } else {
        out.println("ERROR: unable to remove" + jpaBean);
    }
    transaction.commit();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 34 with EntityTransaction

use of javax.persistence.EntityTransaction in project hibernate-orm by hibernate.

the class IdentifierReuseTest method saveUpdateAndRemoveEntity.

private void saveUpdateAndRemoveEntity(EntityManager entityManager, Integer id) {
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();
    IntNoAutoIdTestEntity entity = new IntNoAutoIdTestEntity(0, id);
    entityManager.persist(entity);
    assertEquals(id, entity.getId());
    transaction.commit();
    transaction.begin();
    entity = entityManager.find(IntNoAutoIdTestEntity.class, id);
    entity.setNumVal(1);
    entity = entityManager.merge(entity);
    assertEquals(id, entity.getId());
    transaction.commit();
    transaction.begin();
    entity = entityManager.find(IntNoAutoIdTestEntity.class, id);
    assertNotNull(entity);
    entityManager.remove(entity);
    transaction.commit();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) IntNoAutoIdTestEntity(org.hibernate.envers.test.entities.IntNoAutoIdTestEntity)

Example 35 with EntityTransaction

use of javax.persistence.EntityTransaction in project Truck-Factor by aserg-ufmg.

the class NewFileInfoDAO method cleanFilter.

public int cleanFilter() {
    String hql = "UPDATE  newfileinfo " + "SET filtered = \'FALSE', filterinfo = \'\';";
    Query q = em.createNativeQuery(hql);
    int rows = 0;
    EntityTransaction tx = this.em.getTransaction();
    try {
        tx.begin();
        rows = q.executeUpdate();
        tx.commit();
    } catch (RuntimeException e) {
        if (tx != null && tx.isActive())
            tx.rollback();
        throw e;
    } finally {
        this.em.clear();
    }
    return rows;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) Query(javax.persistence.Query)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)59 EntityManager (javax.persistence.EntityManager)38 Test (org.junit.Test)12 EntityManagerFactory (javax.persistence.EntityManagerFactory)9 Query (javax.persistence.Query)8 UserTestDbo (org.webpieces.plugins.hibernate.app.dbo.UserTestDbo)6 InumSqlEntry (org.gluu.oxtrust.model.sql.InumSqlEntry)4 Person (org.hibernate.userguide.model.Person)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Transactional (com.google.inject.persist.Transactional)2 Priority (org.hibernate.envers.test.Priority)2 DefaultTransactionAttribute (org.springframework.transaction.interceptor.DefaultTransactionAttribute)2 UserDbo (WEBPIECESxPACKAGE.base.libs.UserDbo)1 UserRole (WEBPIECESxPACKAGE.base.libs.UserRole)1 FileType (aserg.gtf.model.FileType)1 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1