Perhaps I dont understand your goals but.....

Postgres:

create table test (
        ID                      SERIAL                  PRIMARY KEY,
        name            varchar(255)            NULL UNIQUE
);

insert into test(name) values(NULL);
insert into test(name) values(NULL); // WILL RESULT in contraint failure and should
insert into test(name) values('A');

If you intend for the field to ALWAYS have a value for the row to be valid then NOT NULL is required. If you intend for the rows to be unique but dont require the field for the row to be valid, it might make more sense to use a separate table. Having a column on a table that is UNIQUE but NULLABLE is a bad design and can lead to confusion.


Thomas
On Dec 8, 2009, at 3:46 AM, cmoulliard wrote:


I have set the field name with a default value in the class ProcessDefinition
but the error is still there. If I remove the annotation on the method
getName, it works !!!

  //@Column(unique = true)
  public String getName() {
      return name;
  }

What is the solution for HSQL DB ?

Remark : Even if the reason of the message displayed in the error is that the field null, the message displayed is not very helpfull for the user (Violation of unique constraint $$: duplicate value(s) for column) -- > must Violation of unique constraint $$: cannot insert null value(s) for column
!!!

Regards,

Charles


Daryl Stultz wrote:

On Mon, Dec 7, 2009 at 11:37 AM, KARR, DAVID (ATTCINW)
<[email protected]>wrote:

It doesn't make sense to have a unique constraint on a
NULLABLE column.


I disagree. A property can be optional and be required to be unique when
it
is specified. In my application I have such a case. I omitted the Unique annotation from my class but left the constraint in the database (OpenJPA refused to insert null into a foreign key column). Using PostgreSQL, nulls
are ignored regarding uniqueness.

--
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:[email protected]



--
View this message in context: 
http://n2.nabble.com/Violation-of-unique-constraint-duplicate-value-s-for-column-tp4126767p4131234.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to