Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Why? uniqueConstraints is not working!

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try using the @Column annotation wiht the unique=true attribute if your unique constraint is on one fields only.
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.


 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prathamesh Gaddam
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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...
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic