Search in sources :

Example 36 with View

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

the class EclipseLinkDetachedTest method testSerializedFetchGroupMerge.

@Test
public void testSerializedFetchGroupMerge() throws Exception {
    Transaction tx;
    EntityManager em;
    User user;
    tx = cont.persistence().createTransaction();
    try {
        em = cont.persistence().getEntityManager();
        View view = new View(User.class).addProperty("login").setLoadPartialEntities(true);
        user = em.find(User.class, userId, view);
        assertNotNull(user);
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertEquals("testLogin", user.getLogin());
    // exception on getting not loaded references
    try {
        user.getName();
        fail();
    } catch (Exception ignored) {
    }
    try {
        user.getGroup();
        fail();
    } catch (Exception ignored) {
    }
    try {
        user.getUserRoles().size();
        fail();
    } catch (Exception ignored) {
    }
    user.setLogin("testLogin-1");
    // merge
    tx = cont.persistence().createTransaction();
    try {
        em = cont.persistence().getEntityManager();
        user = em.merge(user);
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertEquals("testLogin-1", user.getLogin());
    // loaded by mapping rules
    assertEquals("testUser", user.getName());
    // exception on getting not loaded references
    try {
        user.getGroup();
        fail();
    } catch (Exception ignored) {
    }
    try {
        user.getUserRoles().size();
        fail();
    } catch (Exception ignored) {
    }
    // find without view
    tx = cont.persistence().createTransaction();
    try {
        em = cont.persistence().getEntityManager();
        user = em.find(User.class, userId);
        assertNotNull(user);
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertEquals("testLogin-1", user.getLogin());
    assertEquals("testUser", user.getName());
}
Also used : User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 37 with View

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

the class EclipseLinkQueriesTest method testJoinOnWithToManyView2.

// join on, view contains ToMany attribute, fetch = JOIN
@Test
public void testJoinOnWithToManyView2() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        View view = new View(Group.class).addProperty("constraints", new View(Constraint.class, View.LOCAL), FetchMode.JOIN);
        TypedQuery<Group> query = em.createQuery("select g from sec$Group g join sys$QueryResult qr on qr.entityId = g.id where qr.queryKey = 1", Group.class);
        query.setView(view);
        List<Group> result = query.getResultList();
        tx.commit();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) Constraint(com.haulmont.cuba.security.entity.Constraint) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 38 with View

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

the class EclipseLinkQueriesTest method testJoinOnWithParentReference.

// join on, view contains parent attribute
@Test
public void testJoinOnWithParentReference() throws Exception {
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        View view = new View(Group.class).addProperty("parent");
        TypedQuery<Group> query = em.createQuery("select g from sec$Group g join sys$QueryResult qr on qr.entityId = g.id where qr.queryKey = 1", Group.class);
        query.setView(view);
        List<Group> result = query.getResultList();
        tx.commit();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 39 with View

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

the class EclipseLinkQueriesTest method testCrossJoinViewParentReference.

// cross join, view with the reference to the parent entity
@Test
public void testCrossJoinViewParentReference() throws Exception {
    List<Group> result;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        View view = new View(Group.class).addProperty("parent");
        TypedQuery<Group> query = em.createQuery("select g from sec$Group g, sec$User u where u.group = g", Group.class);
        query.setView(view);
        result = query.getResultList();
        tx.commit();
    }
    for (Group g : result) {
        g = reserialize(g);
        if (g.equals(rootGroup))
            assertNull(g.getParent());
        else if (g.equals(group))
            assertEquals(rootGroup, g.getParent());
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 40 with View

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

the class EclipseLinkQueriesTest method testCrossJoinWithToManyView.

// cross join, view has ToMany reference
@Test
public void testCrossJoinWithToManyView() throws Exception {
    List<Group> result;
    try (Transaction tx = cont.persistence().createTransaction()) {
        EntityManager em = cont.entityManager();
        View view = new View(Group.class).addProperty("constraints");
        TypedQuery<Group> query = em.createQuery("select g from sec$Group g, sec$User u where u.group = g", Group.class);
        query.setView(view);
        result = query.getResultList();
        tx.commit();
    }
    for (Group group : result) {
        group = reserialize(group);
        group.getConstraints().size();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Aggregations

View (com.haulmont.cuba.core.global.View)55 Test (org.junit.Test)24 EntityManager (com.haulmont.cuba.core.EntityManager)22 Transaction (com.haulmont.cuba.core.Transaction)21 User (com.haulmont.cuba.security.entity.User)19 Group (com.haulmont.cuba.security.entity.Group)10 MetaClass (com.haulmont.chile.core.model.MetaClass)5 ViewProperty (com.haulmont.cuba.core.global.ViewProperty)5 UserRole (com.haulmont.cuba.security.entity.UserRole)5 SoftDeleteOneToOneA (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneA)3 MetaProperty (com.haulmont.chile.core.model.MetaProperty)2 Entity (com.haulmont.cuba.core.entity.Entity)2 ViewRepository (com.haulmont.cuba.core.global.ViewRepository)2 Role (com.haulmont.cuba.security.entity.Role)2 SoftDeleteOneToOneB (com.haulmont.cuba.testmodel.softdelete_one_to_one.SoftDeleteOneToOneB)2 QueryRunner (com.haulmont.bali.db.QueryRunner)1 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 DynamicAttributesMetaProperty (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesMetaProperty)1 QueryResult (com.haulmont.cuba.core.entity.QueryResult)1 Server (com.haulmont.cuba.core.entity.Server)1