Hi David,

On Jun 19, 2009, at 2:28 PM, David Goodenough wrote:

I realise that this is not strickly an OpenJPA problem, rather a more
general JPA one, but there are experts here who might know the answer.

I have a number of classes which represent tables which have not only
an Id field, but also various other fields which are marked as unique.

When I persist and then try to flush a new object which has a non- unique value in the object (the user entered bad data) it breaks the transaction
and throws an error.  All of which is quite understandable.

The question is what is the best way to avoid it. Do I have to build the
checking into the application, or is there a more generic way which I
can use as a validation technique before I try to persist the object.

You could check each user-entered field against the database by using a JPAQL query, e.g. "SELECT FROM FOO WHERE uniqueField1 EQ ? OR uniqueField2 EQ ?" and fail if there is already an instance in the database.

But of course due to transaction isolation you might still encounter an exception when you flush. Depending on your database, and the isolation level you use, the above query might just lock the range that you query for... Ask your database vendor.

Craig


David

Craig L Russell
Architect, Sun Java Enterprise System http://db.apache.org/jdo
408 276-5638 mailto:[email protected]
P.S. A good JDO? O, Gasp!

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to