I have what I think is a common scenario in JDO. I have a simple persistent class, say
@PersistenceCapable
public class Person {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.INCREMENT)
private long id;
@Persistent
@Unique
private String name
//constructor, equals, hashcode, etc...
}
Then I want to do a simple add, but throw a custom exception if the add fails because the unique constraint was violated
public void addPerson(String name) throws NotUniqueException {
PersistenceManager pm = getPM();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Person toAdd = new Person(name);
pm.makePersistent(toAdd);
// how do I throw a NotUniqueException if the transaction fails due to
// "name" not being unique?
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
How do I detect this error condition in JDO? I'm fine with having to do a query first to check whether it will succeed, as long as the check and add operation are atomic. I'm just new to JDO transactions and am not sure what guarantees I get if I do the query and checking in the transaction.
According to this answer, with JPA I would have to dig through the exception chain for the vender-specific cause exception and use that information. Is that true of JDO?