Search in sources :

Example 21 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ImmutableEntityNaturalIdTest method testNaturalIdCheck.

@Test
public void testNaturalIdCheck() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Parent p = new Parent("alex");
    Child c = new Child("billy", p);
    s.persist(p);
    s.persist(c);
    t.commit();
    s.close();
    Field name = c.getClass().getDeclaredField("name");
    name.setAccessible(true);
    name.set(c, "phil");
    s = openSession();
    t = s.beginTransaction();
    try {
        s.saveOrUpdate(c);
        s.flush();
        fail("should have failed because immutable natural ID was altered");
    } catch (PersistenceException e) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
    name.set(c, "billy");
    s = openSession();
    t = s.beginTransaction();
    s.delete(c);
    s.delete(p);
    t.commit();
    s.close();
}
Also used : Field(java.lang.reflect.Field) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 22 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testNestedEntityNewerThanTopLevel.

@Test
public void testNestedEntityNewerThanTopLevel() {
    Item item = new Item();
    item.setName("item");
    Category category = new Category();
    category.setName("category");
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item);
    s.persist(category);
    tx.commit();
    s.close();
    // Get category1_1 from a different session.
    s = openSession();
    Category category1_1 = (Category) s.get(Category.class, category.getId());
    s.close();
    // Get and update category1_2 to increment its version.
    s = openSession();
    tx = s.beginTransaction();
    Category category1_2 = (Category) s.get(Category.class, category.getId());
    category1_2.setName("new name");
    tx.commit();
    s.close();
    assertTrue(category1_2.getVersion() > category1_1.getVersion());
    category1_1.setExampleItem(item);
    item.setCategory(category1_2);
    s = openSession();
    tx = s.beginTransaction();
    try {
        // nested representation is newer than top lever representation.
        category1_1 = (Category) s.merge(category1_1);
        fail("should have failed because one representation is an older version.");
    } catch (PersistenceException e) {
        // expected
        assertTyping(StaleObjectStateException.class, e.getCause());
    } finally {
        tx.rollback();
        s.close();
    }
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 23 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class MergeTest method testMergeStaleVersionFails.

@Test
public void testMergeStaleVersionFails() throws Exception {
    Session s = openSession();
    s.beginTransaction();
    VersionedEntity entity = new VersionedEntity("entity", "entity");
    s.persist(entity);
    s.getTransaction().commit();
    s.close();
    // make the detached 'entity' reference stale...
    s = openSession();
    s.beginTransaction();
    VersionedEntity entity2 = (VersionedEntity) s.get(VersionedEntity.class, entity.getId());
    entity2.setName("entity-name");
    s.getTransaction().commit();
    s.close();
    // now try to reattch it
    s = openSession();
    s.beginTransaction();
    try {
        s.merge(entity);
        s.getTransaction().commit();
        fail("was expecting staleness error");
    } catch (PersistenceException e) {
        // expected
        assertTyping(StaleObjectStateException.class, e.getCause());
    } finally {
        s.getTransaction().rollback();
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class OptimisticLockTest method testDeleteOptimisticLockFailure.

private void testDeleteOptimisticLockFailure(String entityName) {
    Session mainSession = openSession();
    mainSession.beginTransaction();
    Document doc = new Document();
    doc.setTitle("Hibernate in Action");
    doc.setAuthor("Bauer et al");
    doc.setSummary("Very boring book about persistence");
    doc.setText("blah blah yada yada yada");
    doc.setPubDate(new PublicationDate(2004));
    mainSession.save(entityName, doc);
    mainSession.flush();
    doc.setSummary("A modern classic");
    mainSession.flush();
    doc.getPubDate().setMonth(Integer.valueOf(3));
    mainSession.flush();
    mainSession.getTransaction().commit();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.get(entityName, doc.getId());
    Session otherSession = openSession();
    otherSession.beginTransaction();
    Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
    otherDoc.setSummary("my other summary");
    otherSession.flush();
    otherSession.getTransaction().commit();
    otherSession.close();
    try {
        mainSession.delete(doc);
        mainSession.flush();
        fail("expecting opt lock failure");
    } catch (StaleObjectStateException e) {
    // expected
    } catch (PersistenceException e) {
        // expected
        checkException(mainSession, e);
    }
    mainSession.clear();
    mainSession.getTransaction().rollback();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.load(entityName, doc.getId());
    mainSession.delete(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
}
Also used : PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session)

Example 25 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class OptimisticLockTest method testUpdateOptimisticLockFailure.

private void testUpdateOptimisticLockFailure(String entityName) {
    Session mainSession = openSession();
    mainSession.beginTransaction();
    Document doc = new Document();
    doc.setTitle("Hibernate in Action");
    doc.setAuthor("Bauer et al");
    doc.setSummary("Very boring book about persistence");
    doc.setText("blah blah yada yada yada");
    doc.setPubDate(new PublicationDate(2004));
    mainSession.save(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.get(entityName, doc.getId());
    Session otherSession = sessionFactory().openSession();
    otherSession.beginTransaction();
    Document otherDoc = (Document) otherSession.get(entityName, doc.getId());
    otherDoc.setSummary("A modern classic");
    otherSession.getTransaction().commit();
    otherSession.close();
    try {
        doc.setSummary("A machiavellian achievement of epic proportions");
        mainSession.flush();
        fail("expecting opt lock failure");
    } catch (PersistenceException e) {
        // expected
        checkException(mainSession, e);
    }
    mainSession.clear();
    mainSession.getTransaction().rollback();
    mainSession.close();
    mainSession = openSession();
    mainSession.beginTransaction();
    doc = (Document) mainSession.load(entityName, doc.getId());
    mainSession.delete(entityName, doc);
    mainSession.getTransaction().commit();
    mainSession.close();
}
Also used : PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session)

Aggregations

PersistenceException (javax.persistence.PersistenceException)191 Test (org.junit.Test)73 Session (org.hibernate.Session)57 EntityManager (javax.persistence.EntityManager)39 Transaction (org.hibernate.Transaction)34 EntityTransaction (javax.persistence.EntityTransaction)21 IOException (java.io.IOException)18 Query (javax.persistence.Query)18 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)18 List (java.util.List)16 ArrayList (java.util.ArrayList)14 TypedQuery (javax.persistence.TypedQuery)11 JPAQuery (org.datanucleus.api.jpa.JPAQuery)10 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 HashMap (java.util.HashMap)6 Iterator (java.util.Iterator)6 Person (org.datanucleus.samples.annotations.models.company.Person)6 MessagesEvent (com.openmeap.event.MessagesEvent)5 SQLException (java.sql.SQLException)5