Search in sources :

Example 41 with View

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

the class OrmBehaviorTest method testPersistWithUnManagedAttribute.

/*
     * Test that persist with un-managed attribute works (it didn't work in OpenJPA 2.2+ and worked in OpenJPA pre-2.2)
     */
@Test
public void testPersistWithUnManagedAttribute() throws Exception {
    Group group = new Group();
    groupId = group.getId();
    group.setName("Old Name");
    Transaction tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager().persist(group);
        tx.commit();
    } finally {
        tx.end();
    }
    // Let's imagine that this entity was loaded with MyBatis
    Group g = new Group();
    g.setId(groupId);
    g.setName("Old Name");
    User user = new User();
    userId = user.getId();
    user.setLogin("typednativesqlquery");
    user.setGroup(g);
    user.setName("Test");
    tx = cont.persistence().createTransaction();
    try {
        cont.persistence().getEntityManager().persist(user);
        tx.commitRetaining();
        user = cont.persistence().getEntityManager().find(User.class, userId, new View(User.class).addProperty("group"));
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertEquals(groupId, user.getGroup().getId());
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 42 with View

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

the class PersistenceHelperTest method testCheckLoaded.

@Test
public void testCheckLoaded() {
    Server server = new Server();
    cont.persistence().runInTransaction((em) -> {
        em.persist(server);
    });
    View view = new View(Server.class).addProperty("name").addProperty("data").setLoadPartialEntities(true);
    Server reloadedServer = cont.persistence().callInTransaction((em) -> {
        return em.find(Server.class, server.getId(), view);
    });
    // fine
    PersistenceHelper.checkLoaded(reloadedServer, "name");
    try {
        PersistenceHelper.checkLoaded(reloadedServer, "data", "running");
        Assert.fail("Must throw exception");
    } catch (IllegalArgumentException e) {
        Assert.assertTrue(e.getMessage().contains("Server.running"));
    }
}
Also used : Server(com.haulmont.cuba.core.entity.Server) View(com.haulmont.cuba.core.global.View)

Example 43 with View

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

the class PersistenceTest method testLoadByCombinedView.

@Test
public void testLoadByCombinedView() throws Exception {
    User user;
    Transaction tx = cont.persistence().createTransaction();
    try {
        // load by single view
        EntityManager em = cont.persistence().getEntityManager();
        user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true));
        assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
        assertFalse(cont.persistence().getTools().isLoaded(user, "name"));
        tx.commitRetaining();
        // load by combined view
        em = cont.persistence().getEntityManager();
        user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true), new View(User.class, false).addProperty("name").setLoadPartialEntities(true));
        assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
        assertTrue(cont.persistence().getTools().isLoaded(user, "name"));
        tx.commitRetaining();
        // load by complex combined view
        em = cont.persistence().getEntityManager();
        user = em.find(User.class, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"), new View(User.class, false).addProperty("login").setLoadPartialEntities(true), new View(User.class, false).addProperty("group", new View(Group.class).addProperty("name")).setLoadPartialEntities(true));
        assertTrue(cont.persistence().getTools().isLoaded(user, "login"));
        assertFalse(cont.persistence().getTools().isLoaded(user, "name"));
        assertTrue(cont.persistence().getTools().isLoaded(user, "group"));
        assertTrue(cont.persistence().getTools().isLoaded(user.getGroup(), "name"));
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) View(com.haulmont.cuba.core.global.View) Test(org.junit.Test)

Example 44 with View

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

the class UnfetchedAttributeTest method tearDown.

@After
public void tearDown() throws Exception {
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        // load default 'Company' group
        Query q = em.createQuery("select g from sec$Group g where g.name = ?1");
        q.setView(new View(Group.class, false).addProperty("name"));
        q.setParameter(1, "Company");
        // noinspection unchecked
        List<Group> groups = q.getResultList();
        if (!groups.isEmpty()) {
            Group defaultGroup = groups.get(0);
            // load 'admin' user
            q = em.createQuery("select u from sec$User u where u.id = ?1");
            q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))));
            q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
            List<User> list = q.getResultList();
            if (!list.isEmpty()) {
                User user = list.get(0);
                // set the default value
                user.setGroup(defaultGroup);
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : Group(com.haulmont.cuba.security.entity.Group) User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) View(com.haulmont.cuba.core.global.View) After(org.junit.After)

Example 45 with View

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

the class UnfetchedAttributeTest method testGet.

@Test
public void testGet() throws Exception {
    User user = null;
    Transaction tx = cont.persistence().createTransaction();
    try {
        EntityManager em = cont.persistence().getEntityManager();
        Query q = em.createQuery("select u from sec$User u where u.id = ?1");
        q.setView(new View(User.class, false).addProperty("login").addProperty("userRoles", new View(UserRole.class).addProperty("role", new View(Role.class).addProperty("name"))));
        q.setParameter(1, UUID.fromString("60885987-1b61-4247-94c7-dff348347f93"));
        List<User> list = q.getResultList();
        if (!list.isEmpty()) {
            user = list.get(0);
            // lazy fetch
            user.getGroup();
        }
        tx.commit();
    } finally {
        tx.end();
    }
    user = reserialize(user);
    assertNotNull(user);
    assertNotNull(user.getUserRoles());
    user.getUserRoles().size();
    assertNotNull(user.getGroup());
}
Also used : User(com.haulmont.cuba.security.entity.User) UserRole(com.haulmont.cuba.security.entity.UserRole) 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