0

I came up with an example demonstrating the one-to-one relationship between Employee class and EmployeeDetail class:

public class Employee {

    private Long empId;
    private String name;
    private EmployeeDetail employeeDetail;

    //gettter and setter

}

public class EmployeeDetail{

    private Long empDetailsId;
    private String empFullName;
    private String empMailId;

    private Employee employee;

    //getter and setter..
}

In the Employee class, there's an EmployeeDetail field, and in EmployeeDetail class, there's an Employee field. I understand that as each Employee has its own EmployeeDetail and each EmployeeDetail belongs to only one Employee, but there're 2 points that confuse me:

  1. What if two or more Employees have the same EmployeeDetail (and vice versa)? Is there any way to handle this in Java code or I can only do that in a relational database management system?

  2. In SQL, foreign keys (IDs) represent the relationship between two tables, but in the above example, they use class objects instead. Please help me explain that

Michael
  • 3,093
  • 7
  • 39
  • 83
dwight
  • 1
  • 4
  • Possible duplicate of [Hibernate ManyToOne vs OneToOne](https://stackoverflow.com/questions/18460469/hibernate-manytoone-vs-onetoone) – HBo Apr 13 '18 at 14:08

2 Answers2

0
  1. In an one-to-one relation, an EmployeeDetail can only belong to one Employee. If an EmployeeDetail should be able to belong to multiple Employees you will need a Many-to-one relationship (Many Employees to one Employee Detail).

  2. The reason the foreign keys are noted by class objects is that this is most likely a Hibernate example, which uses Java Objects for database management. (Even if it misses some annotation for a clear Hibernate example)

Here you can find an example about Hibernate and database relations

0

look at this ex :

  @Entity
@Table(name="d_agent_auth")
public class AgentAuth implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int idAuth;
    @NotNull
    private String password;
    private Date dateCreation;

    @OneToOne
    @JoinColumn(name="code_agent")
    private Agent agent;

    public AgentAuth() {
        super();
    }
}

there is two way a navigable one sens and two sens that's means in the agent class you will not find a key reference agentAuth or two sens means that's in the agent you will find it :

@Entity
@Table(name="d_agent")
public class Agent implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    private String codeAgent;

    @ManyToMany(mappedBy="DAgents", cascade=CascadeType.MERGE)
    private List<Profil> profil;

    @OneToMany(mappedBy="DAgent")
    private List<SuiviDossier> DSuiviDossiers;

    @OneToMany(mappedBy="DAgent")
    private List<SuiviLot> suiviLots;

    @OneToMany(mappedBy="agent")
    private List<Affectation> affecter;

    public Agent() {
        super();
    }
}
Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35