Ismael Upright wrote:Consider the following entity:
Question: why can I put 2 Persons with the same surname into the database?
Paul Sturrock wrote:Mark, doesn't the JPA specification mean the UniqueConstraint annotation (the "unique" attribute in this case being shorthand for this annotation) requires Hibernate to include a unique constraint when generating DDL?
Mark Spritzler wrote:
I am not sure, but when I read that link, it seems to say no it doesn't. But that doesn't make sense. But if what the OP says is true, then obviously a unique constraint is not being created in the database.
Mark
The UniqueConstraint annotation is used to specify that a unique constraint is to be included in
the generated DDL for a primary or secondary table.
Ismael Upright wrote:I have
hibernate.hbm2ddl.auto = create-drop
Everytime I launch the application and connect to the database, all tables are dropped and created from scratch.
Paul Sturrock wrote:Are we getting confused here - Hibernate validator is a tool to validate the properties of JavaBeans, it implies nothing about the constraints created in a database when a JavaBean is used to generate DDL. The JPA however specification does state:
The UniqueConstraint annotation is used to specify that a unique constraint is to be included in
the generated DDL for a primary or secondary table.
if Hibernate is not doing this I would say this is a huge hole in Hibernate's JPA implementation, and a very surprising one at that. A quick skim of the Hibernate JIRA doesn't turn up any simmilar bug in this area though.
Mark Spritzler wrote:
Also, the UniqueConstraint annotation is not being used in the OP's example. But I think he could try to add it and see what happens.
Column Annotation Elements
boolean unique (Optional) Whether the property is a unique key.
This is a shortcut for the UniqueConstraint annotation
at the table level and is useful for when the
unique key constraint is only a single field. This
constraint applies in addition to any constraint
entailed by primary key mapping and to constraints
specified at the table level.
UNIQUE
Specifies that values in the column must be unique. NULL values are not allowed.
Ismael Upright wrote:Yes, I see it.
"[UNIQUE:] Specifies that values in the columns must be unique. The identified columns must be defined as NOT NULL."
So the problem is in the specific dialect, not Hibernate itself.
Thanks everyone.
Paul Sturrock wrote:
If you'll allow me to get on my soapbox now, this is one of many reasons why allowing the ORM to generate your schema is not something that I'd ever recommend.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |