Search in sources :

Example 26 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 27 with EntityTransaction

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

the class InumService method findInum.

/**
	 * get an inum from inum DB by inum value
	 * 
	 * @return InumSqlEntry
	 */
public InumSqlEntry findInum(EntityManager inumEntryManager, String inum) {
    boolean successs = false;
    EntityTransaction entityTransaction = inumEntryManager.getTransaction();
    entityTransaction.begin();
    InumSqlEntry result = null;
    try {
        @SuppressWarnings("unchecked") List<InumSqlEntry> inumList = inumEntryManager.createQuery("select u from inumTable u where u.inum = :inum").setParameter("inum", inum).setMaxResults(1).getResultList();
        if (inumList.size() == 0) {
            result = null;
        } else {
            result = inumList.get(0);
            successs = true;
        }
    } finally {
        if (successs) {
            // Commit transaction
            entityTransaction.commit();
        } else {
            // Rollback transaction
            entityTransaction.rollback();
        }
    }
    return result;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) InumSqlEntry(org.gluu.oxtrust.model.sql.InumSqlEntry)

Example 28 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 29 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 30 with EntityTransaction

use of javax.persistence.EntityTransaction in project webpieces by deanhiller.

the class TestAjaxHibernate method loadDataInDb.

public static UserTestDbo loadDataInDb() {
    String email = "dean2@sync.xsoftware.biz";
    //populate database....
    EntityManagerFactory factory = Persistence.createEntityManagerFactory(HibernateAppMeta.PERSISTENCE_TEST_UNIT);
    EntityManager mgr = factory.createEntityManager();
    EntityTransaction tx = mgr.getTransaction();
    tx.begin();
    UserTestDbo user = new UserTestDbo();
    user.setEmail(email);
    user.setName("SomeName");
    user.setFirstName("Dean");
    user.setLastName("Hill");
    mgr.persist(user);
    mgr.flush();
    tx.commit();
    return user;
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) UserTestDbo(org.webpieces.plugins.hibernate.app.dbo.UserTestDbo) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Aggregations

EntityTransaction (javax.persistence.EntityTransaction)56 EntityManager (javax.persistence.EntityManager)36 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