1 pièce(s) jointe(s)
Mapping OneToMany / ManyToOne
Bonjour,
J'ai un mapping particulier :
Pièce jointe 140396
##### USERS
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Entity
@Table(name = "users")
public class Users implements Serializable {
@Id
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
@Column(name = "enabled")
private Boolean enabled;
@Column(name = "email")
private String email;
@OneToMany(mappedBy = "users", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Authorities> authoritiesList; |
##### AUTHORITIES
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Entity
@Table(name = "authorities")
public class Authorities implements Serializable {
@EmbeddedId
protected AuthoritiesPK authoritiesPK;
/**
* ManyToOne Users
*/
@ManyToOne
@JoinColumn(name = "username", insertable = false, updatable = false)
private Users users; |
##### AUTHORITIESPK
Code:
1 2 3 4 5 6 7
| @Embeddable
public class AuthoritiesPK implements Serializable {
@Column(name = "username")
private String username;
@Column(name = "authority")
private String authority; |
Mais je galère à modifier ou supprimer un utilisateur avec les authorities :(
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public void deleteUser(Users pUser) {
EntityTransaction tx = getEM().getTransaction();
if (pUser.getAuthoritiesList() != null) {
tx.begin();
List<Authorities> authoritieses = pUser.getAuthoritiesList();
for (Authorities a : authoritieses) {
a.setUsers(null);
getEM().remove(a);
}
pUser.getAuthoritiesList().clear();
pUser = getEM().merge(pUser);
tx.commit();
}
tx.begin();
getEM().remove(pUser);
tx.commit();
} |
Code:
1 2 3 4 5 6 7
| protected EntityManager getEM() {
if (em == null) {
em = Persistence.createEntityManagerFactory(
JPA_UNIT_NAME).createEntityManager();
}
return em;
} |
MERCI DE VOTRE AIDE