Reputation: 233
I'm using Spring 3.2, Hibernate 4 and MySQL. I have a self referencing class called Lecturers which has annotations implementing a parent/child one to many relationship. I have a problem with implementing a controller and form for saving a parent and child from the same table. It's a self-referencing class.
My DB:
CREATE TABLE `lecturers` (
`lecturer_id` BIGINT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT NULL,
`email` VARCHAR(255) NULL DEFAULT NULL,
`checker_id` BIGINT(20) NULL DEFAULT NULL,
PRIMARY KEY (`lecturer_id`),
FOREIGN KEY (`checker_id`) REFERENCES `lecturers` (`lecturer_id`)
The Java class
@ManyToOne
@JoinColumn(name="checker_id")
private Lecturer checker;
@OneToMany(cascade = CascadeType.ALL, mappedBy="checker", orphanRemoval=true)
private List<Lecturer> lecturers = new ArrayList<Lecturer>();
And the class also has this method
@Transient
public void addLecturer(Lecturer lecturer) {
if(lecturers == null) {
lecturers = new ArrayList<Lecturer>();
//lecturers = new HashSet<Lecturer>();
}
lecturer.setChecker(this);
lecturer.setLecturers(lecturers);
//lecturer.setLecturers(lecturers);
lecturers.add(lecturer);
// TODO Auto-generated method stub
}
I then set up a DAO and Service layer for implementing a CRUD operations. The create method is this:
Session session = sessionFactory.getCurrentSession();
transaction = session.beginTransaction();
// Create new lecturers
Lecturer lecturer1 = new Lecturer();
lecturer1.setName(name);
lecturer1.setEmail(email);
Lecturer lecturer2 = new Lecturer();
lecturer2.setName(name);
lecturer2.setEmail(email);
// Create new checker
Lecturer checker = new Lecturer();
checker.setName(name);
checker.setEmail(email);
checker.setChecker(checker);
List<Lecturer> lecturers = new ArrayList<Lecturer>();
lecturers.add(lecturer1);
lecturers.add(lecturer2);
lecturer1.setChecker(checker);
lecturer2.setChecker(checker);
checker.addLecturer(lecturer1);
checker.addLecturer(lecturer2);
checker.setLecturers(lecturers);
session.save(checker);
session.save(lecturer1);
session.save(lecturer2);
My requirement is now to provide a form that will be used to match a parent (Checker) to one or more children (Lecturers) and save the match to the database. I'm asking how I should go about saving the relationship. Should I create the parent and children separately, then match a parent using the id to a children selected from say a drop down list? I'm not sure how to make sure the relationship between a checker and its respective lecturers is saved.
I then created a main class for testing the relationship and to see if it works. Inserting data into the db works but when I want to list it I get this:
Name: Mark
Email: [email protected]
Checker: com.professional.project.domain.Lecturer@439942
ID: 22
I should get the name of the checker back which I already added but it's not coming back.
I would appreciate some help on how to proceed.
Upvotes: 0
Views: 589
Reputation: 691785
First of all, your addLecturer() method has a bug. It shouldn't set the lecturers list of the child to the current lecturer's list:
public void addLecturer(Lecturer lecturer) {
if (lecturers == null) {
lecturers = new ArrayList<Lecturer>(); // OK : lazy initialization
}
lecturer.setChecker(this); // OK : set the parent of the child to this
lecturer.setLecturers(lecturers); // this line should be removed : the child's children shouldn't be the same as this lecturer's children
lecturers.add(lecturer); // OK : ad the child to the list of children
}
When you get a lecturer, you obtain the following as the checker :
Checker: com.professional.project.domain.Lecturer@439942
The above is just the result of the call to the default toString()
method on the checker. To get its name, call getName()
on the checker. If you want the toString() method to return the name, then implement it that way:
@Override
public String toString() {
return name;
}
Upvotes: 1