Search in sources :

Example 51 with EntityTransaction

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

the class TestSyncHibernate method loadByEmail.

private UserTestDbo loadByEmail(String email) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory(HibernateAppMeta.PERSISTENCE_TEST_UNIT);
    EntityManager mgr = factory.createEntityManager();
    EntityTransaction tx = mgr.getTransaction();
    tx.begin();
    UserTestDbo user = UserTestDbo.findByEmailId(mgr, email);
    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)

Example 52 with EntityTransaction

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

the class TestFlashAndSelect 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 manager = new UserTestDbo();
    manager.setEmail("asdf@asf.com");
    manager.setName("somadsf");
    UserTestDbo user = new UserTestDbo();
    user.setEmail(email);
    user.setName("SomeName");
    user.setFirstName("Dean");
    user.setLastName("Hill");
    user.setLevelOfEducation(LevelEducation.KINDERGARTEN);
    user.setManager(manager);
    UserRoleDbo role1 = new UserRoleDbo(user, Role.DELINQUINT);
    UserRoleDbo role2 = new UserRoleDbo(user, Role.BADASS);
    mgr.persist(manager);
    mgr.persist(user);
    mgr.persist(role1);
    mgr.persist(role2);
    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) UserRoleDbo(org.webpieces.plugins.hibernate.app.dbo.UserRoleDbo)

Example 53 with EntityTransaction

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

the class TestSyncHibernate method verifyLazyLoad.

public static void verifyLazyLoad(int id) {
    //verify lazy load is working so we know test is testing what it should be
    EntityManagerFactory factory = Persistence.createEntityManagerFactory(HibernateAppMeta.PERSISTENCE_TEST_UNIT);
    EntityManager mgr = factory.createEntityManager();
    EntityTransaction tx = mgr.getTransaction();
    tx.begin();
    UserTestDbo user = mgr.find(UserTestDbo.class, id);
    UserTestDbo manager = user.getManager();
    Assert.assertEquals("somadsf", manager.getName());
    mgr.flush();
    tx.commit();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) UserTestDbo(org.webpieces.plugins.hibernate.app.dbo.UserTestDbo) EntityManagerFactory(javax.persistence.EntityManagerFactory)

Example 54 with EntityTransaction

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

the class PopulateDatabase method createSomeData.

private void createSomeData() {
    EntityManager mgr = factory.createEntityManager();
    List<UserDbo> users = UserDbo.findAll(mgr);
    if (users.size() > 0)
        //This database has users, exit immediately to not screw up existing data 
        return;
    EntityTransaction tx = mgr.getTransaction();
    tx.begin();
    UserDbo user1 = new UserDbo();
    user1.setEmail("dean@somewhere.com");
    user1.setName("SomeName");
    user1.setFirstName("Dean");
    user1.setLastName("Hill");
    UserDbo user2 = new UserDbo();
    user2.setEmail("bob@somewhere.com");
    user2.setName("Bob'sName");
    user2.setFirstName("Bob");
    user2.setLastName("LastBob");
    user2.setLevelOfEducation(EducationEnum.MIDDLE_SCHOOL);
    UserRole role1 = new UserRole(user2, RoleEnum.DELINQUINT);
    UserRole role2 = new UserRole(user2, RoleEnum.BADASS);
    mgr.persist(user1);
    mgr.persist(user2);
    mgr.persist(role1);
    mgr.persist(role2);
    mgr.flush();
    tx.commit();
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) UserDbo(WEBPIECESxPACKAGE.base.libs.UserDbo) UserRole(WEBPIECESxPACKAGE.base.libs.UserRole)

Example 55 with EntityTransaction

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

the class InumService method findInumByObject.

/**
 * get an inum from inum DB by inum value
 *
 * @return InumSqlEntry
 */
public InumSqlEntry findInumByObject(EntityManager inumEntryManager, String inum) {
    boolean successs = false;
    EntityTransaction entityTransaction = inumEntryManager.getTransaction();
    entityTransaction.begin();
    InumSqlEntry result = null;
    try {
        InumSqlEntry tempInum = new InumSqlEntry();
        tempInum.setInum(inum);
        // find inum
        result = inumEntryManager.find(InumSqlEntry.class, tempInum);
        if (result != null) {
            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)

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