wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Why? uniqueConstraints is not working! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why? uniqueConstraints is not working!" Watch "Why? uniqueConstraints is not working!" New topic
Author

Why? uniqueConstraints is not working!

Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
Hi Ranchers,

What I interperted: uniqueConstraints (or unique=true) is used to allow entering only unique values to specified column(s), of a table.

I have tried to use uniqueConstraints using annotation and .hbm.xml mapping files (seperately) however neither of them is not working.

I'm able to insert multiple records with the same 'name', instead of unique name. Please guide.

Annotated:



Mapping File:





Thank you in advance!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Try using the @Column annotation wiht the unique=true attribute if your unique constraint is on one fields only.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
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.


Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
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.

Rahul Babbar
Prathamesh Gaddam
Ranch Hand

Joined: Feb 18, 2008
Posts: 58
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
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
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
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why? uniqueConstraints is not working!
 
Similar Threads
Hibernate does not return any result
Hibernate Association
joins in Hibernate Annotation
Hibernate Association
Hibernte will delete rows from a table, but it won't insert rows