Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

@Column(unique=true) and uncatchable exception?

chaminda_javanetApr 9 2008 — edited Apr 9 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 7 2008
Added on Apr 9 2008
2 comments
2,102 views