Search in sources :

Example 66 with EntityManager

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

the class EntityManagerTest method testMerge.

@Test
public void testMerge() throws Exception {
    UUID newUserId = UUID.randomUUID();
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = new User();
        user.setId(newUserId);
        user.setName("testMerge");
        user.setLogin("testMerge");
        user.setPassword("testMerge");
        user.setGroup(em.getReference(Group.class, groupId));
        user = em.merge(user);
        User userFromPersistentContext = em.find(User.class, newUserId);
        assertEquals(user, userFromPersistentContext);
        tx.commit();
    } finally {
        tx.end();
        cont.deleteRecord("SEC_USER", newUserId);
    }
}
Also used : 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) UUID(java.util.UUID) Test(org.junit.Test)

Example 67 with EntityManager

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

the class SerializationTest method testDetachedSerialization.

@Test
public void testDetachedSerialization() throws Exception {
    View view = getView();
    User user;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId, view);
        assertNotNull(user);
        tx.commit();
    } finally {
        tx.end();
    }
    KryoSerialization kryoSerialization = new KryoSerialization();
    User kryoUser = (User) kryoSerialization.deserialize(kryoSerialization.serialize(user));
    StandardSerialization standardSerialization = new StandardSerialization();
    User standardUser = (User) standardSerialization.deserialize(standardSerialization.serialize(user));
    assertEquals(standardUser, kryoUser);
    assertEquals(standardUser.getLogin(), kryoUser.getLogin());
    assertEquals(standardUser.getName(), kryoUser.getName());
    assertEquals(standardUser.getPosition(), kryoUser.getPosition());
    assertEquals(standardUser.getGroup(), kryoUser.getGroup());
    assertEquals(standardUser.getGroup().getName(), kryoUser.getGroup().getName());
    assertEquals(standardUser.getUserRoles().get(0), kryoUser.getUserRoles().get(0));
    assertEquals(standardUser.getUserRoles().get(0).getRole(), kryoUser.getUserRoles().get(0).getRole());
    assertEquals(standardUser.getUserRoles().get(0).getRole().getName(), kryoUser.getUserRoles().get(0).getRole().getName());
}
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)

Example 68 with EntityManager

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

the class SerializationTest method setUp.

@Before
public void setUp() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        User user = new User();
        userId = user.getId();
        user.setName("testUser");
        user.setLogin("testLogin");
        user.setPosition("manager");
        user.setGroup(em.find(Group.class, UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93")));
        em.persist(user);
        UserRole userRole = new UserRole();
        userRoleId = userRole.getId();
        userRole.setUser(user);
        userRole.setRole(em.find(Role.class, UUID.fromString("0c018061-b26f-4de2-a5be-dff348347f93")));
        em.persist(userRole);
        tx.commit();
    } finally {
        tx.end();
    }
}
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)

Example 69 with EntityManager

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

the class SerializationTest method testEmptyDetachedSerialization.

@Test
public void testEmptyDetachedSerialization() throws Exception {
    User user;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId);
        assertNotNull(user);
        tx.commit();
    } finally {
        tx.end();
    }
    KryoSerialization kryoSerialization = new KryoSerialization();
    User kryoUser = (User) kryoSerialization.deserialize(kryoSerialization.serialize(user));
    StandardSerialization standardSerialization = new StandardSerialization();
    User standardUser = (User) standardSerialization.deserialize(standardSerialization.serialize(user));
    assertEquals(standardUser, kryoUser);
    assertEquals(standardUser.getLogin(), kryoUser.getLogin());
    assertEquals(standardUser.getName(), kryoUser.getName());
    assertEquals(standardUser.getPosition(), kryoUser.getPosition());
    try {
        standardUser.getGroup();
        Assert.fail();
    } catch (Exception ignored) {
    }
    try {
        kryoUser.getGroup();
        Assert.fail();
    } catch (Exception ignored) {
    }
    try {
        standardUser.getUserRoles().size();
        Assert.fail();
    } catch (Exception ignored) {
    }
    try {
        kryoUser.getUserRoles().size();
        Assert.fail();
    } catch (Exception ignored) {
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) User(com.haulmont.cuba.security.entity.User) Transaction(com.haulmont.cuba.core.Transaction)

Example 70 with EntityManager

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

the class QueryCacheTestClass method testNativeQuery.

@Test
public void testNativeQuery() {
    appender.clearMessages();
    assertEquals(0, queryCache.size());
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        TypedQuery<User> query = em.createNativeQuery("select * from SEC_USER", User.class);
        query.setCacheable(true);
        query.getResultList();
        tx.commit();
    }
    assertEquals(0, queryCache.size());
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

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