@Column(unique=true) and uncatchable exception?
Hi,
I have an Entity Bean with a field for unique values like this:
@Entity
public class Account {
@Id
@GeneratedValue
private int id;
@Column(nullable=false, unique=true)
private String username;
@Column(nullable=false)
private String password;
// Getters and setters
}
What I want is to warn the user, that his chosen username is already
taken. So I tried using
try {
em.persist(account);
} catch (Exception e) {
logger.error("--- User already exists!");
throw new UserAlreadyExistsException();
}
but I never get my error message! But what I get is this:
Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.DatabaseException
Internal Exception: org.apache.derby.client.am.SqlException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL060928022422750' defined on 'ACCOUNT'.Error Code: -1
Call:INSERT INTO ACCOUNT (ID, USERNAME, PASSWORD) VALUES (?, ?, ?)
bind => [2, db, geheim]
Query:InsertObjectQuery(Username: db)
at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:295)
... and a pretty long stacktrace.
So this looks like a runtime exception is thrown pretty down deep and I
have no chance to catch it and prevent my application from failing :-( I can't
go deeper than putting a try-catch-block around em.create(), right?
Can anyone help me out here, please? Anyone else thinks that this sounds
like a bug?
Thanks
Cham