1

I'm having some problems with Hibernate relationships and Save-Update-Delete. Lets say I have the following:

 public class Subject{
      @Id
      private int ID;
      private String Name;
      //getters and setters
 }


 public class Grade{
      @Id
      private int ID;
      private String Name;
      //getters and setters
 }


 public class Course{
      @Id
      private int ID;
      @ManyToOne
      private Grade G;
      @ManyToOne
      private Subject S;
      //getters and setters
 }


 public class Teacher extends User{
      @ManyToMany
      List<Course> Courses = new ArrayList<Course>();
      //...
      //getters and setters
 }

And the thing is that, I can't simply delete a Subject, because it has relationships. I mean, when I delete a Subject, all the Courses which has that subject have to be deleted too. And, if a Course is deleted, the Subject that "belongs" to that Course should be existing though. And the same happens with the Grade and the Course.

My solution (temporarily) is: The method which deletes the Subject, calls the method which deletes all the Courses that have that subject BEFORE deleting the Subject itself.

But I'm sure this is not the correct way. I know that CascadeType should solve this problem, but I don't know which to use, neither when.

Now we are talking about Hibernate, should all relationships be bidirectional?

Quarktum
  • 669
  • 1
  • 8
  • 26

1 Answers1

0

The Course entity is referenced by 3 entities and is not always the owning side. In this case, you should not use a cascade at all. The relationship would be in an inconsistent state after such a cascaded removal.

Cascades are fine, when two entities have a one-to-many relationship, but for more entities and for many-to-many relationships (as well as for the many-side of a many-to-one relationship), you have to do it 'manually'.

kostja
  • 60,521
  • 48
  • 179
  • 224
  • And orphanRemoval=true attribute should be add – mstzn Jan 14 '14 at 08:47
  • Yes! That's look right. But there is another problem. The class Teacher has an attribute @ManyToMany private List Course; Then, I can't change the structure of Course.class; I mean, if I want to access to a Subject which Teacher is T, I simply write T.getCourse().get(0).getSubject().getName(); So, if I change the classes the way you say, how am I gonna access to a Subject of the Teacher T? – Quarktum Jan 14 '14 at 08:51
  • I have not said anything about modifying the Course class (except for variable naming) - so the access to subject from the teacher can stay the same. – kostja Jan 14 '14 at 08:57
  • The question whether to use a cascade or not is highly dependable on the number and type of relationships an entity has. You cannot senssibly cascade in a many-to-many relationship. Can you please add the Teacher entity to your question - it changes the answer. – kostja Jan 14 '14 at 08:59
  • I've added the Teacher class to the question. I think I'm gonna take the answer "do it manually because there are more than 2 entities involved". Ty so much! – Quarktum Jan 14 '14 at 09:05
  • You're welcome :) I will update the answer to match the question. – kostja Jan 14 '14 at 09:25