Boa noite pessoal
Estou com problema na hora de executar uma Criteria da minha classe Usuario, ao invés de retornar um registro (pois só tem um usuário no banco) tras 3 vezes (pois o usuário tem três permissões).
abaixo segue as classes:
Usuario.class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.tebosoftware.modelo.usuario;
import br.tebosoftware.modelo.Modelo;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
/**
*
* @author Shubacca
*/
@Entity
public class Usuario implements Modelo {
private static final long serialVersionUID = 2727163234350096305L;
@Id
@Column(nullable = false, length = 50)
private String usuario;
@Column(nullable = false, length = 50)
private String nome;
@Column(nullable = false, length = 15)
private String senha;
@Column(nullable = false)
private boolean ativo;
@OneToMany(mappedBy = "permissaoId.usuario", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
private Set<Permissao> permissoes;
public Usuario() {
ativo = true;
addPermissao("ROLE_USUARIO");
}
public boolean isAtivo() {
return ativo;
}
public void setAtivo(boolean ativo) {
this.ativo = ativo;
}
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Set<Permissao> getPermissoes() {
if (permissoes == null) {
permissoes = new HashSet<Permissao>();
}
return permissoes;
}
public void setPermissoes(Set<Permissao> permissoes) {
this.permissoes = permissoes;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public void addPermissao(String permissao) {
getPermissoes().add(new Permissao(this, permissao));
}
public void removePermissao(String permissao) {
for (Permissao p : getPermissoes()) {
if (p.getPermissao().equals(permissao)) {
getPermissoes().remove(p);
break;
}
}
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Usuario other = (Usuario) obj;
if ((this.usuario == null) ? (other.usuario != null) : !this.usuario.equals(other.usuario)) {
return false;
}
if ((this.nome == null) ? (other.nome != null) : !this.nome.equals(other.nome)) {
return false;
}
if ((this.senha == null) ? (other.senha != null) : !this.senha.equals(other.senha)) {
return false;
}
if (this.ativo != other.ativo) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 37 * hash + (this.usuario != null ? this.usuario.hashCode() : 0);
hash = 37 * hash + (this.nome != null ? this.nome.hashCode() : 0);
hash = 37 * hash + (this.senha != null ? this.senha.hashCode() : 0);
hash = 37 * hash + (this.ativo ? 1 : 0);
return hash;
}
@Override
public void setId(Serializable id) {
setUsuario((String) id);
}
@Override
public Serializable getId() {
return getUsuario();
}
public boolean temPermissao(String permissao) {
boolean resp = false;
for (Permissao p : getPermissoes()) {
if (p.getPermissao().equals(permissao)) {
resp = true;
break;
}
}
return resp;
}
}
Permissao.class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.tebosoftware.modelo.usuario;
import br.tebosoftware.modelo.Modelo;
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
/**
*
* @author Shubacca
*/
@Entity
public class Permissao implements Modelo {
private static final long serialVersionUID = -7966675191119996664L;
@EmbeddedId
private PermissaoId permissaoId;
public Permissao() {
this(new PermissaoId());
}
public Permissao(PermissaoId permissaoId) {
this.permissaoId = permissaoId;
}
public Permissao(Usuario usuario, String permissao) {
this(new PermissaoId(usuario, permissao));
}
public PermissaoId getPermissaoId() {
if (permissaoId == null) {
permissaoId = new PermissaoId();
}
return permissaoId;
}
public void setPermissaoId(PermissaoId permissaoId) {
this.permissaoId = permissaoId;
}
public void setUsuario(Usuario usuario) {
getPermissaoId().setUsuario(usuario);
}
public Usuario getUsuario() {
return getPermissaoId().getUsuario();
}
public void setPermissao(String permissao) {
getPermissaoId().setPermissao(permissao);
}
public String getPermissao() {
return getPermissaoId().getPermissao();
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Permissao other = (Permissao) obj;
if (this.permissaoId != other.permissaoId && (this.permissaoId == null || !this.permissaoId.equals(other.permissaoId))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + (this.permissaoId != null ? this.permissaoId.hashCode() : 0);
return hash;
}
public void setId(Serializable id) {
setPermissaoId((PermissaoId) id);
}
public Serializable getId() {
return getPermissaoId();
}
}
PermissaoId.class
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.tebosoftware.modelo.usuario;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
/**
*
* @author Shubacca
*/
@Embeddable
public class PermissaoId implements Serializable {
private static final long serialVersionUID = -4375079709352388333L;
@ManyToOne(optional = false, fetch=FetchType.EAGER)
private Usuario usuario;
@Column(nullable = false, length = 20)
private String permissao;
public PermissaoId() {
}
public PermissaoId(Usuario usuario, String permissao) {
this.usuario = usuario;
this.permissao = permissao;
}
public String getPermissao() {
return permissao;
}
public void setPermissao(String permissao) {
this.permissao = permissao;
}
public Usuario getUsuario() {
return usuario;
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
}
abaixo o código para executar o criteria
public Usuario carregar(String login) throws RNException {
try {
return usuarioDAO.buscar(login);
} catch (DAOException e) {
throw new RNException(e);
}
}
desde já agradeço