Code:
@Entity
class Teacher implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
@NotNull
String name;
@OneToMany(mappedBy = "teacher",cascade = CascadeType.REMOVE)
List<Course> courses;
}
@Entity
public class Course implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
@Column(nullable = false)
@NotNull
String name;
@ManyToOne(cascade = CascadeType.REMOVE)
Teacher teacher;
}
Three questions:
Must I put
CascadeType.REMOVE
in both entities? I want it to work so that if I delete the teacher the course will be deleted automatically.In my database my id doesn't work normally. I want it so that the id increments by one each time, but it currently increases by some random number. Why?
I have 2 more
@Entity
Classes but with@ManyToMany
relationship. What must I to do when I want to persist a new object in the database?