Search in sources :

Example 61 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntityManagerTest method testMergeWithOneToManyCollection.

@Test
public void testMergeWithOneToManyCollection() throws Exception {
    User user = null;
    UserRole userRole1 = null, userRole2 = null;
    Role role1 = null, role2 = null;
    try {
        // creates user with one role
        Transaction tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            user = new User();
            user.setName("testMerge1");
            user.setLogin("testMerge1");
            user.setPassword("testMerge1");
            user.setGroup(em.getReference(Group.class, groupId));
            em.persist(user);
            role1 = new Role();
            role1.setName("role1");
            em.persist(role1);
            role2 = new Role();
            role2.setName("role2");
            em.persist(role2);
            userRole1 = new UserRole();
            userRole1.setRole(role1);
            userRole1.setUser(user);
            em.persist(userRole1);
            tx.commit();
        } finally {
            tx.end();
        }
        // creates new user role and merge it
        tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            userRole2 = new UserRole();
            userRole2.setRole(role2);
            userRole2.setUser(user);
            em.merge(userRole2);
            tx.commit();
        } finally {
            tx.end();
        }
        // check user versions, version shouldn't be changed
        tx = cont.persistence().createTransaction();
        try {
            EntityManager em = cont.persistence().getEntityManager();
            assertEquals((Integer) 1, em.find(User.class, user.getId()).getVersion());
            tx.commit();
        } finally {
            tx.end();
        }
    } finally {
        cont.deleteRecord(userRole1);
        cont.deleteRecord(userRole2);
        cont.deleteRecord(user);
        cont.deleteRecord(role1);
        cont.deleteRecord(role2);
    }
}
Also used : Role(com.haulmont.cuba.security.entity.Role) UserRole(com.haulmont.cuba.security.entity.UserRole) Group(com.haulmont.cuba.security.entity.Group) EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) UserRole(com.haulmont.cuba.security.entity.UserRole) Test(org.junit.Test)

Example 62 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntityManagerTest method testFind.

// Commented in EL: EntityManager has no setView() method anymore
// public void testViewPropagation() throws Exception {
// 
// View view = new View(User.class, false)
// .addProperty("name")
// .addProperty("login")
// .addProperty("group", new View(Group.class)
// .addProperty("name"));
// 
// User user;
// 
// Transaction tx = cont.persistence().createTransaction();
// try {
// EntityManager em = cont.persistence().getEntityManager();
// em.setView(view);
// 
// TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
// query.setParameter(1, userId);
// user = query.getSingleResult();
// 
// tx.commit();
// } finally {
// tx.end();
// }
// 
// assertNull(user.getCreatedBy());
// assertNotNull(user.getGroup());
// }
@Test
public void testFind() throws Exception {
    View view = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId, view);
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 63 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntityManagerTest method testQueryView.

@Test
public void testQueryView() throws Exception {
    View view = new View(User.class, "testQueryView", false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class, false).addProperty("name"));
    ((AbstractViewRepository) cont.metadata().getViewRepository()).storeView(cont.metadata().getSession().getClassNN(User.class), view);
    User user;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        TypedQuery<User> query = em.createQuery("select u from sec$User u where u.id = ?1", User.class);
        query.setParameter(1, userId);
        query.setViewName("testQueryView");
        user = query.getSingleResult();
        tx.commit();
    }
    user = reserialize(user);
    assertNotNull(user.getCreatedBy());
    assertNotNull(user.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 64 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntityManagerTest method testFindSeparateViews.

@Test
public void testFindSeparateViews() throws Exception {
    View view1 = new View(User.class, false).addProperty("name").addProperty("login");
    View view2 = new View(User.class, false).addProperty("name").addProperty("login").addProperty("group", new View(Group.class).addProperty("name"));
    User user1, user2;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        user1 = em.find(User.class, userId, view1);
        user2 = em.find(User.class, user2Id, view2);
        tx.commit();
    }
    user1 = reserialize(user1);
    user2 = reserialize(user2);
    assertNotNull(user1);
    assertNotNull(user1.getCreatedBy());
    try {
        user1.getGroup();
        fail();
    } catch (Exception ignored) {
    }
    assertNotNull(user2);
    assertNotNull(user2.getCreatedBy());
    assertNotNull(user2.getGroup());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) View(com.haulmont.cuba.core.global.View) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test)

Example 65 with EntityManager

use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.

the class EntityManagerTest method testReload.

@Test
public void testReload() throws Exception {
    User originalUser, reloadedUser;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        originalUser = em.find(User.class, userId, "user.browse");
        tx.commit();
    }
    assertNotNull(originalUser);
    assertFalse(PersistenceHelper.isLoaded(originalUser, "userRoles"));
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.persistence().getEntityManager();
        reloadedUser = em.reload(originalUser, "user.edit");
        tx.commit();
    }
    assertNotNull(reloadedUser);
    assertTrue(PersistenceHelper.isLoaded(reloadedUser, "userRoles"));
    assertTrue(originalUser != reloadedUser);
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction) Test(org.junit.Test)

Aggregations

EntityManager (com.haulmont.cuba.core.EntityManager)167 Transaction (com.haulmont.cuba.core.Transaction)140 Query (com.haulmont.cuba.core.Query)27 User (com.haulmont.cuba.security.entity.User)27 Test (org.junit.Test)25 View (com.haulmont.cuba.core.global.View)22 MetaClass (com.haulmont.chile.core.model.MetaClass)14 Group (com.haulmont.cuba.security.entity.Group)12 Before (org.junit.Before)11 Entity (com.haulmont.cuba.core.entity.Entity)10 SendingMessage (com.haulmont.cuba.core.entity.SendingMessage)8 UUID (java.util.UUID)7 Nullable (javax.annotation.Nullable)7 TypedQuery (com.haulmont.cuba.core.TypedQuery)6 List (java.util.List)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)5 Role (com.haulmont.cuba.security.entity.Role)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)5 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)4