views:

156

answers:

1

Hello,

I have a code using JPA with Hibernate 3.3.x. This Java code can be used with schemas stored either on Oracle 10g or MySQL 5.1.x. Tables are defined with constraints to define unique records. When a constraint violation occurs, I want to retrieve the constraint name from the exception.

With Oracle, the constraint name is properly retrieved. With MySQL, the constraint name is NULL.

Any idea how to get the constraint name with MySQL?

Thanks

Said

A: 

Are you specifying the constraint names in MySQL at the database level? in Oracle the constraints get default names if not specified, however in MySQL I don't know, I think I heard once that if you don't specify a name for the constraint MySQL will not put a default one!!

Omar Al Kababji
Yes I have set a name as illustrated below.ALTER TABLE MY_TABLE ADD CONSTRAINT MY_CONSTRAINT UNIQUE (Col1, Col2, Col3);I have a DDL script to create the database schema for MySQL and Oracle 10g. Constraints are defined in the same way in bith DDL scripts.When I debug my application, I saw that in Oracle that the constraint name is the one expected (MY_Constraint basd on my illustration).When I change the persistence unit to switch on MySQL, I receive NULL as constraint name.
eloudsa
did you try to take a look at the constraint names of your MySQL database using an SQL client to see they are shown or not?
Omar Al Kababji