Reputation: 169
I am creating a simple program in spring boot using JPA to show student and phone number relationship.
CODE
Student Entity
package student.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Long id;
String name;
int roll;
@OneToOne
PhoneNumber num;
public Student() {
super();
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public int getRoll() {
return roll;
}
public PhoneNumber getNum() {
return num;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setRoll(int roll) {
this.roll = roll;
}
public void setNum(PhoneNumber num) {
this.num = num;
}
}
PhoneNumber Entity
package student.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class PhoneNumber {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
Long id;
String num;
String type;
@OneToOne
Student stu;
public PhoneNumber() {
super();
}
public PhoneNumber(String num, String type) {
super();
this.num = num;
this.type = type;
}
public Long getId() {
return id;
}
public String getNum() {
return num;
}
public void setId(Long id) {
this.id = id;
}
public void setNum(String num) {
this.num = num;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
}
Student and Phone number repo
package student.repo;
import org.springframework.data.repository.CrudRepository;
import student.entity.Student;
public interface StudentRepo extends CrudRepository<Student, Long> {
}
package student.repo;
import org.springframework.data.repository.CrudRepository;
import student.entity.PhoneNumber;
public interface PhoneNumberRepo extends CrudRepository<PhoneNumber, Long>{
}
SeedFile to enter dummy data
package student;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import student.entity.PhoneNumber;
import student.entity.Student;
import student.repo.PhoneNumberRepo;
import student.repo.StudentRepo;
@Component
public class SeedDb implements CommandLineRunner {
private StudentRepo studentRepo;
private PhoneNumberRepo phoneNumberRepo;
public SeedDb(StudentRepo studentRepo, PhoneNumberRepo phoneNumberRepo) {
super();
this.studentRepo = studentRepo;
this.phoneNumberRepo = phoneNumberRepo;
}
@Override
public void run(String... args) throws Exception {
System.out.println("######################");
Student stu1 = new Student();
stu1.setName("X");
stu1.setRoll(4);
PhoneNumber p = new PhoneNumber("9090909090","Home");
phoneNumberRepo.save(p); //saving phone number to database
stu1.setNum(p);
studentRepo.save(stu1);
}
}
In this program (in seedDb file), I have to manually save the Phone number using phoneNumberRepo before setting it to the student but there are tutorials online where this step is not needed.
Also, the id of student is not saved to the phone number as shown in screenshot
Please tell me how to set PhoneNumber to student without saving it to the database i.e. when I save student, phoneNumber automatically get saved to the database and how to set id of student to the Phone number automatically.
Upvotes: 1
Views: 426
Reputation: 3834
The problem in your code is that you set the PhoneNumber in your Student, but you never set the Student in your PhoneNumber.
As for the bidirectional relationship, you need to use the cascade
parameter in the @OneToOne
annotation of one of the two entity you're creating the relationship out of. The rule of the thumb is, cascading should be done from parent to children. In your case, the Student is the parent class, so you should do the following:
@OneToOne(cascade = CascadeType.ALL)
PhoneNumber num;
And create a student like so:
Student student = new Student();
student.setName("John Doe");
student.setRoll(4);
PhoneNumber phoneNumber = new PhoneNumber("9090909090", "Home");
student.setNum(phoneNumber);
phoneNumber.setStu(student);
studentRepository.save(student);
Result:
Upvotes: 1