users@glassfish.java.net

Unique Constraint Question

From: <glassfish_at_javadesktop.org>
Date: Sat, 01 Sep 2007 08:38:16 PDT

I'm using GF v2rc3, with EJB 3.0, JSF, and Hibernate 3.0 as the ORM. I'm trying to figure out how to handle the violation of a unique constraint on a table column. The column is defined like this:

    @Column(length = 32, nullable = false, unique= true)
    public String getUsername() {
        return _username;
    }

When I store a User object that violates this unique constraint, I get useful stack trace information on the EJB side so that I know what's going on, but I can't find any way for the application to detect what happened. The exception thrown to JSF doesn't seem to contain any useful information. Furthermore, it does not appear to be possible to catch the exception on the EJB-side so that I can thrown an application exception containing more useful information.

The only idea I have is to query to check whether constraints will be violated prior to storing data. Of course, this would require multiple queries should there be multiple columns with constraints. This would be an awful solution, of course, but is it the only solution available?

Has anyone found a better way to solve this issue?

Thanks,

Rick Horowitz
[Message sent by forum member 'rickhoro' (rickhoro)]

http://forums.java.net/jive/thread.jspa?messageID=233567