I tried this code for the model for me and it worked. It should be noted that you must use the @OneToOne annotation @OneToOne that ORM knows that you have a foreign key reference to another model.
The model is as follows:
@Entity // add unique constraint to user_id column @Table(name = "driver", uniqueConstraints = @UniqueConstraint(columnNames = "user_id") ) public class Driver extends Model { @Id public Long id; @OneToOne @JoinColumn(name = "user_id") public User user; }
It will generate an evolution of the script as follows:
create table driver ( id bigint not null, user_id bigint, constraint uq_driver_1 unique (user_id),
So, with this method, you can make sure that you have a unique user link in the driver table.
Additional Information
Since there is an additional restriction that is not handled by the framework, but with databases applied to the model (for example, the unique constraint), you can surround Model.save() or form.get().save() to check the input or handle the exception that has form.get().save() (save-the-model) with a try-catch to handle a PersistenceException .
source share