0

I have a unidirectional OneToOne relation like

@Entity   
public class Citizen
{
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     protected Long id;

     @OneToOne(cascade = CascadeType.ALL)
     protected FileEntity photo;
     ...getters and setters....
}

@Entity
public class FileEntity
{
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     protected Long id;
}

Then when I go to remove a Citizen object the following error appear:

org.postgresql.util.PSQLException: ERROR: update o delete on <> violates the foreign key fk_citizen_file_id in the table <>

Why this happen??

JCF
  • 368
  • 3
  • 15
  • 1
    Does the instance you are calling delete on have a reference to a FileEntity? How did you read in the Citizen before calling remove on it? – Chris Oct 21 '15 at 19:14

1 Answers1

2

Hibernate performs delete operations in the database in the order they were performed.

Thus, either remove the FileEntity manually before removing the Citizen, or make the foreign key nullable in the database.

Community
  • 1
  • 1
Dragan Bozanovic
  • 23,102
  • 5
  • 43
  • 110