Search in sources :

Example 1 with ClassWithUniqueField

use of org.datanucleus.tests.applicationid.ClassWithUniqueField in project tests by datanucleus.

the class ApplicationIdPersistenceTest method testUniqueConstraint.

public void testUniqueConstraint() throws Exception {
    try {
        PersistenceManager pm = pmf.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            ClassWithUniqueField u1 = new ClassWithUniqueField(1, "First Name", "Value");
            pm.makePersistent(u1);
            ClassWithUniqueField u2 = new ClassWithUniqueField(2, "Second Name", "Value");
            pm.makePersistent(u2);
            tx.commit();
            fail("Allowed to insert dupd objects when unique constraint exists");
        } catch (JDOException e) {
        // Expected
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ClassWithUniqueField.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) ClassWithUniqueField(org.datanucleus.tests.applicationid.ClassWithUniqueField) JDOException(javax.jdo.JDOException)

Aggregations

JDOException (javax.jdo.JDOException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 ClassWithUniqueField (org.datanucleus.tests.applicationid.ClassWithUniqueField)1