I have the following entity classes:
public class Usuario implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Id")
private Integer id;
@Size(max = 255)
@Column(name = "Senha")
private String senha;
@Size(max = 255)
@Column(name = "Nome")
private String nome;
@ManyToMany(mappedBy = "usuarioCollection", fetch = FetchType.EAGER)
private Collection<Grupo> grupoCollection;
}
and
public class Grupo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "Id")
private Integer id;
@Size(max = 255)
@Column(name = "Nome")
private String nome;
@JoinTable(name = "usuario_grupo", joinColumns = {
@JoinColumn(name = "GrupoID", referencedColumnName = "Id")}, inverseJoinColumns = {
@JoinColumn(name = "UsuarioID", referencedColumnName = "Id")})
@ManyToMany(fetch = FetchType.EAGER)
private Collection<Usuario> usuarioCollection;
}
These codes were generated by netbeans.
When I try to persist it with JPA
entityManager.persist(usuario);
It executes, but there is no registers in the usuario_grupo table.
ie, the usuario is registered in the table (the grupo already exists in the table grupo), but the usuario_grupo doesn’t.
Do you know how I can do this properly?
NOTE: some names are in portuguese.
JPA does not like
Collection. It works withSet(unordered collections),List(ordered collections), I believe also withHashMap(I have not needed that so I am not sure).If your bean must have the
Collectionattribute, make ittransientand make a new attibute (the get and set methods, at least) that manages thatCollectionas aSetorList.