Search in sources :

Example 1 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testQueryById.

@Test
public void testQueryById() throws Exception {
    IdentityEntity foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    IdentityEntity loaded;
    try (Transaction tx = persistence.createTransaction()) {
        TypedQuery<IdentityEntity> query = persistence.getEntityManager().createQuery("select e from test$IdentityEntity e where e.id = ?1", IdentityEntity.class);
        query.setParameter(1, foo.getId());
        loaded = query.getSingleResult();
        tx.commit();
    }
    assertEquals(foo, loaded);
    try (Transaction tx = persistence.createTransaction()) {
        TypedQuery<IdentityEntity> query = persistence.getEntityManager().createQuery("select e from test$IdentityEntity e where e.id = :id", IdentityEntity.class);
        query.setParameter("id", foo.getId());
        loaded = query.getSingleResult();
        tx.commit();
    }
    assertEquals(foo, loaded);
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Example 2 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testPersistAndMerge.

@Test
public void testPersistAndMerge() throws Exception {
    IdentityEntity foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    UUID uuid = foo.getId().getUuid();
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    assertNotNull(foo.getId().get());
    assertEquals(uuid, foo.getId().getUuid());
    foo.setName("bar");
    IdentityEntity bar;
    try (Transaction tx = persistence.createTransaction()) {
        bar = persistence.getEntityManager().merge(foo);
        tx.commit();
    }
    assertEquals(uuid, foo.getId().getUuid());
    assertEquals(foo, bar);
    bar.setName("baz");
    IdentityEntity baz;
    try (Transaction tx = persistence.createTransaction()) {
        baz = persistence.getEntityManager().merge(foo);
        tx.commit();
    }
    assertEquals(uuid, foo.getId().getUuid());
    assertEquals(foo, baz);
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) UUID(java.util.UUID) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Example 3 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testIdCopying.

@Test
public void testIdCopying() {
    IdentityEntity entity1 = metadata.create(IdentityEntity.class);
    assertNull(entity1.getId().get());
    IdentityEntity entity2 = metadata.create(IdentityEntity.class);
    entity2.setId(entity1.getId());
    IdentityEntity entity3 = metadata.create(IdentityEntity.class);
    entity3.setId(entity1.getId());
    assertTrue(entity2.getId() != entity3.getId());
}
Also used : IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Example 4 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testFindAndDelete.

@Test
public void testFindAndDelete() throws Exception {
    IdentityEntity foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    IdentityEntity loaded;
    try (Transaction tx = persistence.createTransaction()) {
        loaded = persistence.getEntityManager().find(IdentityEntity.class, foo.getId());
        tx.commit();
    }
    assertEquals(foo, loaded);
    try (Transaction tx = persistence.createTransaction()) {
        loaded = persistence.getEntityManager().find(IdentityEntity.class, loaded.getId());
        persistence.getEntityManager().remove(loaded);
        tx.commit();
    }
    assertTrue(BaseEntityInternalAccess.isRemoved(loaded));
    try (Transaction tx = persistence.createTransaction()) {
        loaded = persistence.getEntityManager().find(IdentityEntity.class, loaded.getId());
        assertNull(loaded);
        tx.commit();
    }
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Example 5 with IdentityEntity

use of com.haulmont.cuba.testmodel.primary_keys.IdentityEntity in project cuba by cuba-platform.

the class IdentityTest method testLoadByIdValue.

@Test
public void testLoadByIdValue() throws Exception {
    IdentityEntity foo = metadata.create(IdentityEntity.class);
    foo.setName("foo");
    foo.setEmail("foo@mail.com");
    try (Transaction tx = persistence.createTransaction()) {
        persistence.getEntityManager().persist(foo);
        tx.commit();
    }
    Long idVal = foo.getId().get();
    IdentityEntity loaded;
    try (Transaction tx = persistence.createTransaction()) {
        loaded = persistence.getEntityManager().find(IdentityEntity.class, IdProxy.of(idVal));
        tx.commit();
    }
    assertEquals(foo, loaded);
    try (Transaction tx = persistence.createTransaction()) {
        TypedQuery<IdentityEntity> query = persistence.getEntityManager().createQuery("select e from test$IdentityEntity e where e.id = :id", IdentityEntity.class);
        query.setParameter("id", idVal);
        loaded = query.getSingleResult();
        tx.commit();
    }
    assertEquals(foo, loaded);
    try (Transaction tx = persistence.createTransaction()) {
        TypedQuery<IdentityEntity> query = persistence.getEntityManager().createQuery("select e from test$IdentityEntity e where e.id = :id", IdentityEntity.class);
        query.setParameter("id", IdProxy.of(idVal));
        loaded = query.getSingleResult();
        tx.commit();
    }
    assertEquals(foo, loaded);
}
Also used : Transaction(com.haulmont.cuba.core.Transaction) IdentityEntity(com.haulmont.cuba.testmodel.primary_keys.IdentityEntity) Test(org.junit.Test)

Aggregations

IdentityEntity (com.haulmont.cuba.testmodel.primary_keys.IdentityEntity)7 Test (org.junit.Test)7 Transaction (com.haulmont.cuba.core.Transaction)5 Field (java.lang.reflect.Field)1 UUID (java.util.UUID)1