-2

I am developing web application using spring and hibernate. I am using one to one mapping between two tables employee and PersonelDetails.

below are my bean classes

=======================Employee=====================================

@Entity
@Table(name="employee")
public class Employee {
    @Id
    @Column
    @GeneratedValue
    private int empid;
    @Column
    private String firstName;
    @Column
    private String lastName;
    @Column
    private String email;
    @Column
    private String password;
    @Column
    private boolean isAdmin;

    @Column
    private boolean isActive;

    @Column
    private boolean isLocked;
//getter setters

====================PersonalDetails class====================

@Entity
@Table(name="PersonalDetails")
public class PersonalDetails {
    @Column
    @Id
    private int empid;
    @Column
    private String personalEmail;
    @Column
    private String mob;
    @Column
    private String permenantAdress;
    @Column
    private String currentAddress;
    @Column
    private String gender;
    @Column
    private String maritialStatus;

    @MapsId
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "empid", referencedColumnName = "empid")
    @ForeignKey(name="empid")
    private Employee employee;

//getter setters

In my application table employee is filled by Admin user while creating new employee after that employyee himself fill personalDetails table by login to his accountCreated by Admin)

Now when I try to send personal details bean to hibernate layer first I have to get the employee bean from employee table then call setEmployee method over personalDetails class and save employee bean in personalDetails and send to hibernate layer for saving in database.

So while getting employee bean from database and again send back through personalDetails bean leads to a performance issue.

Can anyone help here to clarify while saving data in child table(PersonalDetails) is it really mandatory to pass parent object(Employee) ?

=======================code to store personalDetails===============

@RequestMapping(value="addpersonal")
    public ModelAndView addPersonalDetails(@ModelAttribute("personalDetails") PersonalDetails personalDetails) {
        //personalDetails.setEmpid(1);
        personalDetails.setCurrentAddress("niljyoti");
        personalDetails.setMob("9405715872");
        personalDetails.setPermenantAdress("address");
        Employee e = empService.getEmployeebyUserName(uname);
        personalDetails.setEmployee(e);
        personalDetailsService.addPersonalDetails(personalDetails);
        return new ModelAndView("home");

    }
Prasad Parab
  • 437
  • 1
  • 7
  • 26

1 Answers1

0

On read:

You can change fetch strategy if you are worried. Based od JPA spec, default fetch type for @OneToOne is EAGER.

By setting fetch = FetchType.LAZY, instead of real PersonalDetails object, an object of a subclass behaving as a proxy is returned. Hence, selecting from employee table starts only after getEmployee is called.

On write:

You need to specify connection between entities, in your model, the only way is the employee field. However, you can specify mappedBy, see answer to this question:

Java: Hibernate @OneToOne mapping

Community
  • 1
  • 1
Tomas F.
  • 610
  • 1
  • 6
  • 13