Properties and NULL values
In Figure 935, “Database schema mapping instances of
hibintro.v1.model.User
. ” the primary key
uid
property's value must not be NULL
. This
is an immediate consequence of the
javax.persistence.Id
annotation and the fact
that databases don't allow NULL values for key attributes.
The cname
property however may be null. Sometimes
we want to ensure the corresponding database attributes to be set, at
least carrying an empty string value. This can be achieved by adding a
javax.persistence.Column
(nullable
= false)
annotation:
Java |
|
SQL |
|
This results in a corresponding database constraint ❶. Attempts to store instances with null values now fail:
Java |
|
Log | Exception in thread "main" javax.persistence.PersistenceException:
org.hibernate.PropertyValueException: not-null property references a null or transient value :
hibintro.v4.User.cname
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
...
at hibintro.v4.PersistSingleUser.main(PersistSingleUser.java:22) |
The database constraint violation causes the JDBC™ driver to throw this exception.