You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Martin Dirichs <na...@dirichs.fastmail.fm> on 2009/05/07 14:47:47 UTC
Unique colums automatically made NOT NULL
Hi,
when creating a database schema with OpenJPA, columns with a unique
constraint are automatically defined as non null. Even if the column is
explicitly anntotated as nullable (with @Column(nullable=true)), the
resulting table definition marks it as "NOT NULL".
Looking at the code it is not difficult to see why this happens. In
org.apache.openjpa.jdbc.schema.Unique it says:
/**
* Adds the given column.
* The added column is set to non-nullable because a unique constraint
* on the database requires that its constituent columns are NOT NULL.
* @see Column#setNotNull(boolean)
*/
public void addColumn(Column col) {
super.addColumn(col);
col.setNotNull(true);
}
This is strange, since nullable unique columns are perfectly legal with
regard to ANSI SQL-92 as well as all databases I've work with so far, which
includes Oracle, MySQL, Postgresql and h2. In fact, this is a crucial
difference between columns defined as PRIMARY KEY and those only bound by a
UNIQUE constraint: the latter allows null values for the columns, unless
these have been explicitly defined as NOT NULL.
Is there a deeper reason for this behaviour of OpenJPA which I'm not aware
of?
-Martin.
--
View this message in context: http://n2.nabble.com/Unique-colums-automatically-made-NOT-NULL-tp2827809p2827809.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.