English 中文(简体)
JPA 2.0 EclipseLink Check for unique
原标题:

I have a collumn as unique=true.. in Exam class.... I found that because transactions are commited automaticaly so to force the commit i use em.commit()

However i would like to know how to check if it is unique.Running a query isnt a solution because it may be an instert after checking because of the concurency....

Which is the best way to check for uniqness?

List<Exam_Normal> exam_normals = exam.getExam_Normal();
    exam.setExam_Normal(null);

    try {
        em.persist(exam);
        em.flush();

        Long i = 0L;
        if (exam_normals != null) {
            for (Exam_Normal e_n : exam_normals) {
                i++;
                e_n.setItem(i);
                e_n.setId(exam);
                em.persist(e_n);
            }
        }
    } catch (Exception e) {
        System.out.print("sfalma--");
    }
}

d

最佳回答

Unfortunately with JPA there is no way to avoid a transaction rollback upon a Unique Constraint violation as the specification requires this Exception to mark the transaction for rollback. Also, because the row may not exist when you issue the lock call using JPA 2.0 APIs the lock call will not ensure that only the locking thread can insert the object. A lock would prevent the update of the Entity but not the insert.

You would need to perform the persist as you have in your code but keep it as close to the beginning of the transaction as possible or have the operation occur in its own transaction.

If the persist must be part of a larger transaction and the failure to persist does not preclude this part of your application from succeeding then you should store the Entity instances from this transaction and you will be able to merge them into any subsequent transaction once your application recovers from the rollback.

问题回答

However I would like to know how to check if it is unique. Running a query isn t a solution because it may be an insert after checking because of the concurrency....

JPA 2.0 permits pessimistic locking and adds three lock modes for that. This might be an option for your use case.

Some references:

I assume that the column is not a primary key (@Id) in which case your application have to guarantee uniqueness. Guarantee of Uniqueness of the field will probably have to come from elsewhere. What type of value is the field? Here are some ideas

If it is a counter then you can probably use @Singleton stateful bean (JavaEE 6. See this.

If you are using EE5, then a stateless bean with an @Entity that maps to a auto generated key.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签