0

Could you please tell me how you I might structure the following table/classes to avoid circular references? I have the following defined in a "Bachelor" degree class:

@Entity
public class Bachelor {
...
    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "fk_bachelor")
    private Study study;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "fk_prior")
    private List<Study> priorStudies;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "fk_exchange")
    private List<Study> exchangeStudies;
...
}

@Entity
class Study {

    private String title;
    private String placeOfStudy;
    etc
...
}

This is to represent a bachelor degree which has one Study record for details of the bachelor degree. If the applicant specifies they've completed a bachelor degree they can then specify one or more prior and exchange studies. An example of a bachelor degree plus one prior degree results is the following:

bachelor

id        |  fk_bachelor

10003     |  10000
study

id        |  title             |  fk_prior

10000     |  Bachelor of Arts  |

10001     |  Prior Degree      |  10003

This seems to work fine except if I need to delete items from the tables. I also received a warning about having circular references from the compiler. So I can't first delete from Bachelor because Study references it by fk_prior, and I can't first delete from Study because Bachelor references it. I'm guessing this isn't good practice to structure it like this.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Novarse
  • 51
  • 1
  • 10
  • The code you show does not have any circular references. – Perception Jan 17 '13 at 01:03
  • You have defined a `@OneToMany` relationship from `Bachelor` to `Study` with the `CascadeType.ALL` attribute, so when you delete a `Bachelor` all connected `Study` are deleted, no circular reference is involved. – remigio Jan 17 '13 at 07:17
  • I'm using a Postgres database and when I try "delete from bachelor" I get ERROR: update or delete on table "bachelor" violates foreign key constraint "fk_general_details_fk_bachelor" on table "general_details" DETAIL: Key (id)=(10003) is still referenced from table "general_details". deleting from Study results in ERROR: update or delete on table "study" violates foreign key constraint "fk_bachelor_fk_study" on table "bachelor" DETAIL: Key (id)=(10000) is still referenced from table "bachelor". – Novarse Jan 17 '13 at 12:55

1 Answers1

0

Bachelor has a FK to study and Study has 2 FKs to Bachelor. Different JPA providers will handle this differently, but you can help them out by nullling out one side. For instance, I'd recommend setting the bachelor's study to null; breaking the cycle would allow the study to be removed before the bachelor. Some databases also allow delaying constraint processing until the end of the transaction, when all the delete statements would have sorted themselves out.

Chris
  • 20,138
  • 2
  • 29
  • 43