Hello,
I have a user entity which inherits from collaborator entity, and i use @Inheritance(strategy = InheritanceType.JOINED) in the Collaborator entity.
I do not find the way to persist a user entity that belongs to an existing collaborator, I can add a user object and it results to 2 new created rows in the data base but what i want to do is to add only a user object and not a new collaborator.
I can find a collaborator by Collaborator clbr=em.findById(2);
and i want to add a user that its collaborator is clbr but how to associate the new user object to this collaborator object ?
Below my entities
Code:
@Inheritance(strategy = InheritanceType.JOINED)
@Entity
@Table(name = "COLLABORATEUR")
public class Collaborator implements Serializable {
private static final long serialVersionUID = 2421761198342915033L;
/**
* Unique ID used as a primary key
*/
@Id
@Column(name = "MATRICULE")
protected Integer id;
/**
* The name of the collaborator
*/
@NotNull
@Column(name = "NOM")
protected String name;
/**
* The surname of the collaborator
*/
@NotNull
@Column(name = "PRENOM")
protected String surname;
/**
* The mail of the collaborator
*/
@Column(name = "MAIL")
protected String mail;
/**
* @return the mail
*/
public String getMail() {
return mail;
}
/**
* @param mail
* the mail to assign for the collaborator
*/
public void setMail(String mail) {
this.mail = mail;
}
/**
* @return the reference number
*/
public Integer getId() {
return id;
}
/**
* @param id
* the the reference number to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname
* the surname to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
/**
* @return the String representation of user
*/
public String toString() {
return String.format("[id=%d , name=%s, surName=%s, mail=%s, userName=%s]", getId(), getName(), getSurname(), getMail());
}
}
Code:
@Entity
@Table(name="UTILISATEUR")
public class User extends Collaborator implements Serializable {
private static final long serialVersionUID = 6568398464819949491L;
/**
* The username used for authenticating
*/
@NotNull
@Column(name="UTI_USERNAME")
private String username;
/**
* The password MD5 hash
*/
@Column(name="UTI_PASSWORD_HASH")
private String passwordHash;
/**
* The list of privileges of the user
*/
@ManyToMany(targetEntity = Privilege.class)
@JoinTable(name = "UTILISATEUR_PRIVILEGE", joinColumns = @JoinColumn(name = "UTI_ID"), inverseJoinColumns = @JoinColumn(name = "PRIVILEGE_ID"))
private Set<Privilege> privileges;
/**
* @return the username
*/
public String getUsername() {
return username;
}
/**
* @param username the username to set
*/
public void setUsername(String username) {
this.username = username;
}
/**
* @return the passwordHash
*/
public String getPasswordHash() {
return passwordHash;
}
/**
* @param passwordHash the passwordHash to set
*/
public void setPasswordHash(String passwordHash) {
this.passwordHash = passwordHash;
}
/**
* @return the privileges
*/
public Set<Privilege> getPrivileges() {
return privileges;
}
/**
* @param privileges the privileges to set
*/
public void setPrivileges(Set<Privilege> privileges) {
this.privileges = privileges;
}
/**
* @return the String representation of user
*/
public String toString() {
return String.format("[id=%d , userName=%s, password=%s, collaborator=%s]", getId(), getUsername(), getPasswordHash()/*, getCollaborator().getName()*/);
}
}