use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class NonDetachedTest method testCreateNew_DM.
@Test
public void testCreateNew_DM() throws Exception {
// check versioned entity
Group companyGroupCopy = metadata.getTools().copy(companyGroup);
assertTrue(PersistenceHelper.isNew(companyGroupCopy));
PersistenceHelper.makeDetached(companyGroupCopy);
User user = persistence.callInTransaction((em) -> em.find(User.class, this.user.getId()));
User userCopy = metadata.getTools().copy(user);
assertNull(userCopy.getGroup());
assertTrue(PersistenceHelper.isNew(userCopy));
PersistenceHelper.makeDetached(userCopy);
userCopy.setId(UuidProvider.createUuid());
userCopy.setVersion(null);
userCopy.setGroup(companyGroupCopy);
userCopy.setName("new name");
try {
AppBeans.get(DataManager.class).commit(userCopy);
user = persistence.callInTransaction((em) -> em.find(User.class, userCopy.getId()));
assertEquals("new name", user.getName());
} finally {
cont.deleteRecord(userCopy);
}
// check non-versioned entity
Server server = metadata.create(Server.class);
server.setName("server-" + server.getId());
assertTrue(PersistenceHelper.isNew(server));
PersistenceHelper.makeDetached(server);
try {
AppBeans.get(DataManager.class).commit(server);
Server loaded = persistence.callInTransaction(em -> em.find(Server.class, server.getId()));
assertNotNull(loaded);
} finally {
cont.deleteRecord(server);
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method __testRollbackAndCatch.
private void __testRollbackAndCatch() {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
Server server = new Server();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
throwException();
tx.commit();
} catch (RuntimeException e) {
System.out.println("Caught exception: " + e.getMessage());
throw e;
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method __testRollback.
private void __testRollback() {
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
Server server = new Server();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
throwException();
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method testCommit.
@Test
public void testCommit() {
UUID id;
Transaction tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
assertNotNull(em);
Server server = new Server();
id = server.getId();
server.setName("localhost");
server.setRunning(true);
em.persist(server);
tx.commit();
} finally {
tx.end();
}
tx = cont.persistence().createTransaction();
try {
EntityManager em = cont.persistence().getEntityManager();
Server server = em.find(Server.class, id);
assertEquals(id, server.getId());
server.setRunning(false);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.entity.Server in project cuba by cuba-platform.
the class TransactionTest method testReadOnly.
@Test
public void testReadOnly() throws Exception {
try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
TypedQuery<User> query = persistence.getEntityManager().createQuery("select u from sec$User u where u.id = ?1", User.class);
query.setParameter(1, TestSupport.ADMIN_USER_ID);
User result = query.getSingleResult();
tx.commit();
}
// read-only transaction cannot be committed if it contains changed entities
UUID id = persistence.callInTransaction(em -> {
Server server = new Server();
server.setName("localhost");
em.persist(server);
return server.getId();
});
try (Transaction tx = persistence.createTransaction(new TransactionParams().setReadOnly(true))) {
TypedQuery<Server> query = persistence.getEntityManager().createQuery("select e from sys$Server e where e.id = ?1", Server.class);
query.setParameter(1, id);
Server server = query.getSingleResult();
server.setName("changed");
try {
tx.commit();
fail();
} catch (IllegalStateException e) {
// ok
}
}
}
Aggregations