Paul Sturrock wrote:Try using the @Column annotation wiht the unique=true attribute if your unique constraint is on one fields only.
Thank you, Paul !
The problem is same, accepting exisiting names. No exception is thrown. Kindly assist.
Joined: Jun 28, 2008
ok...can you check one thing....
Are you using hibernate to create the tables or you already have the tables in place.
Probably you have already created the tables and there in no unique index on "Name" column.
Check that there should be a unique index on the "Name" column. If there is not, put the unique index.
I think the unique="true" should be used by hibernate only when you are autogenerating the schema, so that it creates the table with the unique index on that column.
There is no way the hibernate can prevent you from inserting duplicate data in the table if there is no unique index on the table in the DB but you specify in the hbm or using annotation because ultimately, hibernate will not know whether the data you are inserting correspond to duplicate "Name" column unless it explicitly fires the query.
Hope it helps.
Joined: Feb 18, 2008
Thank you very much, Rahul!
That mean, the constraints are to be imposed on DB (for exisiting tables) and hibernate will not restrict/constraints itself, for such tables. Please correct ant of my mis-interpretation.
Yes! I have not imposed any such (unique) constraint on my existing tables on SQLServer2000.
How can place such constraints (for unique names) on tables and any way to pass SQL exceptions to hibernate or its automated?
Omar Al Kababji
Joined: Jan 13, 2009
Hi, the uniqueConstraint and many others are used only if you want automatic schema generation, which means that you let hibernate to generate your database, and these constraints will be applied then, the same is the length of a String property, it will be used only so that when creating the schema it will replace the (x) variable with it varchar(x).
and this is the whole purpose of this meta data, however if you don't automatically generate your schema, and instead you are writing it by hand then hibernate will just ignore these values, as if they did not exist.
Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Joined: Jun 28, 2008
How can place such constraints (for unique names) on tables
You just need to ALter the table and add the constraint.
A query like "Create unique index Employee_Name on EMPLOYEE(NAME)" should be executed in case of Oracle.
I don't have much idea about the corresponding statement in Sqlserver, although it should be pretty similar.
any way to pass SQL exceptions to hibernate or its automated?
I think you can test it yourself, just try to insert the duplicate row, see what exception comes....mostly it'll be a hibernate exception and not the DB exception...