use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.
the class EnableDiscardPersistenceContextOnCloseTest method testDiscardOnClose.
@Test
public void testDiscardOnClose() {
EntityManager em = entityManagerFactory().createEntityManager();
Wallet wallet = new Wallet();
wallet.setSerial("123");
try {
em.getTransaction().begin();
em.persist(wallet);
assertEquals(1, connectionProvider.getAcquiredConnections().size());
em.close();
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertTrue(em.getTransaction().isActive());
} finally {
try {
em.getTransaction().rollback();
fail("Should throw IllegalStateException because the Connection is already closed!");
} catch (IllegalStateException expected) {
}
}
}
use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.
the class QueryTest method testPositionalParameterWithUserError.
@Test
public void testPositionalParameterWithUserError() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
Wallet w = new Wallet();
w.setBrand("Lacoste");
w.setModel("Minimic");
w.setSerial("0100202002");
em.persist(w);
em.flush();
// using jpa-style, position index should match syntax '?<position'.
Query jpaQuery = em.createQuery("select w from Wallet w where w.brand = ?1 and w.model = ?3");
jpaQuery.setParameter(1, "Lacoste");
try {
jpaQuery.setParameter(2, "Expensive");
fail("Should fail due to a user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
// using jpa-style, position index specified not in query - test exception type
jpaQuery = em.createQuery("select w from Wallet w ");
try {
Parameter parameter = jpaQuery.getParameter(1);
fail("Should fail due to a user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
// using jpa-style, position index specified not in query - test exception type
jpaQuery = em.createQuery("select w from Wallet w");
try {
Parameter<Integer> parameter = jpaQuery.getParameter(1, Integer.class);
fail("Should fail due to user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
// using hql-style, should be 0-based
Query hqlQuery = em.createQuery("select w from Wallet w where w.brand = ? and w.model = ?");
try {
hqlQuery.setParameter(1, "Lacoste");
hqlQuery.setParameter(2, "Expensive");
fail("Should fail due to a user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
} finally {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.
the class QueryTest method testNamedParameterWithUserError.
@Test
@TestForIssue(jiraKey = "HHH-10803")
public void testNamedParameterWithUserError() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
Wallet w = new Wallet();
w.setBrand("Lacoste");
w.setModel("Minimic");
w.setSerial("0100202002");
em.persist(w);
em.flush();
Query jpaQuery = em.createQuery("select w from Wallet w");
try {
Parameter<?> parameter = jpaQuery.getParameter("brand");
fail("Should fail due to user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
jpaQuery = em.createQuery("select w from Wallet w");
try {
Parameter<String> parameter = jpaQuery.getParameter("brand", String.class);
fail("Should fail due to user error in parameters");
} catch (Exception e) {
assertTyping(IllegalArgumentException.class, e);
}
} finally {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.
the class DisableDiscardPersistenceContextOnCloseTest method testDiscardOnClose.
@Test
public void testDiscardOnClose() throws SQLException {
EntityManager em = entityManagerFactory().createEntityManager();
Wallet wallet = new Wallet();
wallet.setSerial("123");
try {
em.getTransaction().begin();
em.persist(wallet);
assertEquals(1, connectionProvider.getAcquiredConnections().size());
em.close();
assertEquals(1, connectionProvider.getAcquiredConnections().size());
assertTrue(em.getTransaction().isActive());
} finally {
assertEquals(1, connectionProvider.getAcquiredConnections().size());
em.getTransaction().rollback();
assertEquals(0, connectionProvider.getAcquiredConnections().size());
assertFalse(em.getTransaction().isActive());
}
}
use of org.hibernate.jpa.test.Wallet in project hibernate-orm by hibernate.
the class QueryTest method testNativeQuestionMarkParameter.
@Test
public void testNativeQuestionMarkParameter() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
Wallet w = new Wallet();
w.setBrand("Lacoste");
w.setModel("Minimic");
w.setSerial("0100202002");
em.persist(w);
em.getTransaction().commit();
em.getTransaction().begin();
Query query = em.createNativeQuery("select * from Wallet w where w.brand = ?", Wallet.class);
query.setParameter(1, "Lacoste");
w = (Wallet) query.getSingleResult();
assertNotNull(w);
em.remove(w);
em.getTransaction().commit();
} catch (Exception e) {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
throw e;
} finally {
em.close();
}
}
Aggregations