How to Use UUID as Foreign Key in Postgresql with hibernate
08:10 15 Jun 2020

I was trying to persist a Person with a Set of Emails using JPA, but I couldn get there.

I'm getting this exception -

Caused by: org.postgresql.util.PSQLException: ERROR: relation "person" does not exist

I have a "Person" and a Set of "Email" for that Person. What am I doing wrong?

My code is below

creting table

CREATE TABLE "Person" (
  publicId uuid DEFAULT uuid_generate_v4 (),
  name VARCHAR(64) NOT NULL
  PRIMARY KEY (publicId)
);

CREATE TABLE "Email" (
  emailId uuid DEFAULT uuid_generate_v4 (),
  email VARCHAR(64) NOT NULL,
  PRIMARY KEY (emailId),
  FOREIGN KEY (person) REFERENCES Person (publicId)
);

Entities

Person

@Id
@Column(name = "publicId")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Type(type="org.hibernate.type.PostgresUUIDType")
private UUID publicId;

@NotBlank
@Column
private String name;

@OneToMany(mappedBy="person")
private Set emails;

Email

@Id
@Column(name = "emailId")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Type(type="org.hibernate.type.PostgresUUIDType")
private UUID emailId;

@NotBlank
@Column
private String email;

@ManyToOne
@JoinColumn(name="publicId", nullable=false)
private Person person;

and my DTOs

DtoPersonCreate

@NotBlank
@VerifiedName
private String name;


@NotEmpty(message = "Input email list cannot be empty.")
private Set emails;

DtoEmail

@NotBlank
private String email;
postgresql