use of javax.persistence.NonUniqueResultException in project invesdwin-context-persistence by subes.
the class SimpleTestService method getByName.
public SimpleTestEntity getByName(final String name) {
final SimpleTestEntity example = new SimpleTestEntity();
example.setName(name);
final List<SimpleTestEntity> l = dao.findAll(example);
if (l.size() == 0) {
return null;
} else if (l.size() > 1) {
throw new NonUniqueResultException(String.valueOf(l.size()));
} else {
return l.get(0);
}
}
use of javax.persistence.NonUniqueResultException in project microservices by pwillhan.
the class CreateExecuteQueries method executeQueries.
@Test
public void executeQueries() throws Exception {
TestDataCategoriesItems testData = storeTestData();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
tx.begin();
EntityManager em = JPA.createEntityManager();
{
// Get a list
Query query = em.createQuery("select i from Item i");
List<Item> items = query.getResultList();
assertEquals(items.size(), 3);
}
{
// Get a list of scalar values
Query query = em.createQuery("select i.name from Item i");
List<String> itemNames = query.getResultList();
assertEquals(itemNames.size(), 3);
}
{
// Single result
TypedQuery<Item> query = em.createQuery("select i from Item i where i.id = :id", Item.class).setParameter("id", ITEM_ID);
Item item = query.getSingleResult();
assertEquals(item.getId(), ITEM_ID);
}
{
// Single scalar result
TypedQuery<String> query = em.createQuery("select i.name from Item i where i.id = :id", String.class).setParameter("id", ITEM_ID);
String itemName = query.getSingleResult();
assertEquals(em.find(Item.class, ITEM_ID).getName(), itemName);
}
{
// No (single) result
boolean gotException = false;
try {
TypedQuery<Item> query = em.createQuery("select i from Item i where i.id = :id", Item.class).setParameter("id", 1234l);
Item item = query.getSingleResult();
// ...
} catch (NoResultException ex) {
// ...
gotException = true;
}
assertTrue(gotException);
}
{
// Not a unique result
boolean gotException = false;
try {
Query query = em.createQuery("select i from Item i where name like '%a%'");
Item item = (Item) query.getSingleResult();
// ...
} catch (NonUniqueResultException ex) {
// ...
gotException = true;
}
assertTrue(gotException);
}
{
// Scrolling with a database cursor
Session session = em.unwrap(Session.class);
org.hibernate.Query query = session.createQuery("select i from Item i order by i.id asc");
org.hibernate.ScrollableResults cursor = query.scroll(org.hibernate.ScrollMode.SCROLL_INSENSITIVE);
// Jump to third result row
cursor.setRowNumber(2);
// Get first "column"
Item item = (Item) cursor.get(0);
// Required!
cursor.close();
assertEquals(item.getName(), "Baz");
}
{
// Iterating through a result
Session session = em.unwrap(Session.class);
org.hibernate.Query query = session.createQuery("select i from Item i");
int count = 0;
// select ID from ITEM
Iterator<Item> it = query.iterate();
while (it.hasNext()) {
// select * from ITEM where ID = ?
Item next = it.next();
// ...
count++;
}
// Iterator must be closed, either when the Session
// is closed or manually:
Hibernate.close(it);
assertEquals(count, 3);
}
tx.commit();
em.close();
} finally {
TM.rollback();
}
}
use of javax.persistence.NonUniqueResultException in project rubia-forums by flashboss.
the class ForumsModuleImpl method uniqueElement.
static <T> T uniqueElement(List<T> list) throws NonUniqueResultException {
int size = list.size();
if (size == 0)
return null;
T first = list.get(0);
for (int i = 1; i < size; i++) {
if (list.get(i) != first) {
throw new NonUniqueResultException(list.size() + "");
}
}
return first;
}
use of javax.persistence.NonUniqueResultException in project invesdwin-context-persistence by subes.
the class SimpleTestService method getByName.
public SimpleTestEntity getByName(final String name) {
final SimpleTestEntity example = new SimpleTestEntity();
example.setName(name);
final List<SimpleTestEntity> l = dao.findAll(example);
if (l.size() == 0) {
return null;
} else if (l.size() > 1) {
throw new NonUniqueResultException(String.valueOf(l.size()));
} else {
return l.get(0);
}
}
use of javax.persistence.NonUniqueResultException in project invesdwin-context-persistence by subes.
the class TestService method getByName.
public TestEntity getByName(final String name) {
final TestEntity example = new TestEntity();
example.setName(name);
final List<TestEntity> l = dao.findAll(example);
if (l.size() == 0) {
return null;
} else if (l.size() > 1) {
throw new NonUniqueResultException(String.valueOf(l.size()));
} else {
return l.get(0);
}
}
Aggregations