patternjavaMinor
Hibernate @OneToMany relationship
Viewed 0 times
hibernateonetomanyrelationship
Problem
I started to play around with Hibernate since yesterday and came up with the following example of one-to-many relationship example, but I am not sure if I am doing right and I have no one around me knowing Hibernate. Please take a quick look at it, then maybe point out anything wrong.
My goal is to use a one-to-many relationship with annotations to perform basic Insert and Update.
The Entity classes are from here.
Student
Phone
```
@Entity
@Table(name = "PHONE")
public class Phone {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {
}
My goal is to use a one-to-many relationship with annotations to perform basic Insert and Update.
The Entity classes are from here.
Student
@Entity
@Table(name = "STUDENT")
public class Student {
private long studentId;
private String studentName;
private Set studentPhoneNumbers = new HashSet(0);
public Student() {
}
public Student(String studentName, Set studentPhoneNumbers) {
this.studentName = studentName;
this.studentPhoneNumbers = studentPhoneNumbers;
}
@Id
@GeneratedValue
@Column(name = "STUDENT_ID")
public long getStudentId() {
return this.studentId;
}
public void setStudentId(long studentId) {
this.studentId = studentId;
}
@Column(name = "STUDENT_NAME", nullable = false, length = 100)
public String getStudentName() {
return this.studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "STUDENT_PHONE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "PHONE_ID") })
public Set getStudentPhoneNumbers() {
return this.studentPhoneNumbers;
}
public void setStudentPhoneNumbers(Set studentPhoneNumbers) {
this.studentPhoneNumbers = studentPhoneNumbers;
}
//I added in this method for updating
public void addPhone(Phone phone) {
this.studentPhoneNumbers.add(phone);
}
}Phone
```
@Entity
@Table(name = "PHONE")
public class Phone {
private long phoneId;
private String phoneType;
private String phoneNumber;
public Phone() {
}
Solution
Just 3 little remarks because it really looks good already.
-
You want the setters of the id's to be private. They are generated
values by Hibernate and other classes shouldn't mess with them.
-
Also for the default constructors of
needs only default package visibility. So best to remove the public
access modifiers in your default constructors.
-
I would also create a
-
You want the setters of the id's to be private. They are generated
values by Hibernate and other classes shouldn't mess with them.
-
Also for the default constructors of
Student and Phone, Hibernateneeds only default package visibility. So best to remove the public
access modifiers in your default constructors.
-
I would also create a
StudentDao class instead of the 3 *Main classes. And if you like, you can use Spring Transaction annotations to reduce the transaction boilerplate code.Context
StackExchange Code Review Q#3293, answer score: 4
Revisions (0)
No revisions yet.