Я создал метод для сохранения пользовательских данных в таблицах базы данных, каждая таблица имеет разные столбцы, и я хочу указать метод, который может автоматически сохранять данные 1 столбца в другую таблицу.
1-й управляемый компонент I создано в основном хранить данные в таблице «Пользователь», и я хочу сохранить «курс» в таблице «Студент». Вот отображение гибернации для каждой таблицы и управляемого компонента:
LoginBean. java
public class LoginBean {
private String fullName_;
private String gender_;
private String phoneNumber_;
private String IC_;
private String email_;
private String Address_;
private String password_;
private String course;
List<String> courseOption;
public LoginBean(){
courseOption = new ArrayList<>();
courseOption.add("Information Technology");
courseOption.add("Business Management");
courseOption.add("Engineering");
courseOption.add("Design");
public List<String> getCourseOption() {
return courseOption;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
public String getFullName_() {
return fullName_;
}
public void setFullName_(String fullName_) {
this.fullName_ = fullName_;
}
public String getGender_() {
return gender_;
}
public void setGender_(String gender_) {
this.gender_ = gender_;
}
public String getPhoneNumber_() {
return phoneNumber_;
}
public void setPhoneNumber_(String phoneNumber_) {
this.phoneNumber_ = phoneNumber_;
}
public String getIC_() {
return IC_;
}
public void setIC_(String IC_) {
this.IC_ = IC_;
}
public String getEmail_() {
return email_;
}
public void setEmail_(String email_) {
this.email_ = email_;
}
public String getAddress_() {
return Address_;
}
public void setAddress_(String Address_) {
this.Address_ = Address_;
}
public String getPassword_() {
return password_;
}
public void setPassword_(String password_) {
this.password_ = password_;
}
public String saveStudent(LoginBean loginBean){
UserDao dao = new UserDao(); //METODE SIMPAN KE DATABASE!!!
User user = new User();
user.setFullName(loginBean.getFullName_());
user.setGender(loginBean.getGender_());
user.setPhoneNumber(Integer.parseInt(loginBean.getPhoneNumber_()));
user.setIc(loginBean.getIC_());
user.setEmail(loginBean.getEmail_());
user.setAddress(loginBean.getAddress_());
user.setPassword(loginBean.getPassword_());
dao.saveStudent(user);//untuk menyimpan di database
Map<String,Object> sessionMapObj = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
sessionMapObj.put("msg", "Data "+user.getIc() +"successfull!");
return"/sukses.xhtml?faces-redirect=true";
}
Пользователь. java (база данных)
public class User implements java.io.Serializable {
private Integer userId;
private String fullName;
private String gender;
private Integer phoneNumber;
private String ic;
private String email;
private String address;
private String password;
private Set students = new HashSet(0);
private Set staffs = new HashSet(0);
public User() {
}
public User(String fullName, String gender, Integer phoneNumber, String ic, String email, String address, String password, Set students, Set staffs) {
this.fullName = fullName;
this.gender = gender;
this.phoneNumber = phoneNumber;
this.ic = ic;
this.email = email;
this.address = address;
this.password = password;
this.students = students;
this.staffs = staffs;
}
public Integer getUserId() {
return this.userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getFullName() {
return this.fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getGender() {
return this.gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Integer getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(Integer phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getIc() {
return this.ic;
}
public void setIc(String ic) {
this.ic = ic;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public Set getStudents() {
return this.students;
}
public void setStudents(Set students) {
this.students = students;
}
public Set getStaffs() {
return this.staffs;
}
public void setStaffs(Set staffs) {
this.staffs = staffs;
}
Студент. java (база данных)
public class Student implements java.io.Serializable {
private Integer id;
private Booking booking;
private User user;
private Date date;
private String course;
public Student() {
}
public Student(Booking booking, User user, Date date, String course) {
this.booking = booking;
this.user = user;
this.date = date;
this.course = course;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public Booking getBooking() {
return this.booking;
}
public void setBooking(Booking booking) {
this.booking = booking;
}
public User getUser() {
return this.user;
}
public void setUser(User user) {
this.user = user;
}
public Date getDate() {
return this.date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCourse() {
return this.course;
}
public void setCourse(String course) {
this.course = course;
}
Примечание: я сделал метод "courseOption" в "LoginBean", чтобы установить пример selectOneradio на странице x html.