I'm using Java Persistence API for an already existing project and as shown in the picture there is a relative employe to each assure depending on his id.Before i have used hibernate reverse engineering to generete the entities but i don't know how to deal with this case.If it is possible to help me to link "Adminisitrateur" entity to the assure for same to employe in other term each "administrateur" has relatives assure i will be thankfull with little explanation if it is possible.
Here the persistance code of the entities: User entity:
package persistence;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity(name = "User")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String prenom;
@Column(unique = true)
private String matricule;
private String password;
@Column(unique = true)
private String email;
@OneToMany(mappedBy = "employer", fetch = FetchType.EAGER)
private Set<Demande> demandes;
private static final long serialVersionUID = 1L;
public User() {
}
public User(String name, String prenom, String matricule, String password, String email) {
super();
this.name = name;
this.prenom = prenom;
this.matricule = matricule;
this.password = password;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Set<Demande> getDemandes() {
return demandes;
}
public void setDemandes(Set<Demande> demandes) {
this.demandes = demandes;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public String getMatricule() {
return matricule;
}
public void setMatricule(String matricule) {
this.matricule = matricule;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Employer entity:
package persistence;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
/**
* Entity implementation class for Entity: Employer
*
*/
@Entity
public class Employer extends User implements Serializable {
@OneToMany(mappedBy = "employerRelative", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private List<Assure> assures;
private static final long serialVersionUID = 1L;
public Employer() {
}
public Employer(String name, String prenom, String matricule, String password, String email) {
super(name, prenom, matricule, password, email);
}
public List<Assure> getAssures() {
return assures;
}
public void setAssures(List<Assure> assures) {
this.assures = assures;
}
public void linkAssures(List<Assure> assures) {
this.assures = assures;
for (Assure a : assures) {
a.setEmployerRelative(this);
}
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
Administrateur entity:
package persistence;
import java.io.Serializable;
import javax.persistence.*;
import persistence.User;
/**
* Entity implementation class for Entity: Administrateur
*
*/
@Entity
public class Administrateur extends User implements Serializable {
private static final long serialVersionUID = 1L;
public Administrateur() {
super();
}
public Administrateur(String name, String prenom, String login, String password, String email) {
super(name, prenom, login, password, email);
// TODO Auto-generated constructor stub
}
}
Assure entity: package persistence;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
/**
* Entity implementation class for Entity: Assure
*
*/
@Entity
public class Assure extends User implements Serializable {
@ManyToOne
private Employer employerRelative;
private String type;
private static final long serialVersionUID = 1L;
public Assure() {
super();
}
public Assure(String name, String prenom, String matricule, String password, String email) {
super(name, prenom, matricule, password, email);
}
public Employer getEmployerRelative() {
return employerRelative;
}
public void setEmployerRelative(Employer employerRelative) {
this.employerRelative = employerRelative;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
here we are talking about extends from user class. to do that with hibernate user class should not be annotated with @Entity
. you should use @MappedSupperClass
or more better @Inheritance
annotation.