Hibernate @Id and Oracle Single constraint

Advertisement

Can I use a hibernate entity with @Id on a single constraint instead of a primary key on Oracle databases? Will this be JPA compliant?

The answer

This sounds a little strange, but seems possible:
http://www.objectdb.com/api/java/jpa/Id:

The field or property to which the ID annotation is applied must be one of the following types : any Java primitive type; any type of primitive envelope; Chain; java.util.Date; java.sql.Date; java.math.BigDecimal; java.math.BigInteger.

But be aware:
(1) For example, in Oracle UNIQUE does NOT imply NOT NULL, you must make sure of your choice.
(2) You can not use a FOREIGN KEY constraint in your database.
(3) You need to be careful with Id generation (http://www.objectdb.com/api/java/jpa/GeneratedValue) if you want to insert new entities (not only read existing ones).
(4) If you insert a new entity, you must generate a value for the database ID.

And these are just a few inconveniences that have come to mind quickly ...