我需要选择多个实体,用JPA 2.0映射,我正在使用Hibernate。
我正在验证用户登录。我有一个类ClienteDTO
(aka Client),它扩展了一个抽象类Pessoa
(Person)
@Entity
@Table(name = "cliente")
@PrimaryKeyJoinColumn(name = "id")
public class ClienteDTO extends Pessoa implements Serializable {
private static final long serialVersionUID = 9057647385901589752L;
@OneToOne(mappedBy="cliente", cascade=CascadeType.ALL)
private LoginDTO login;
public ClienteDTO() {
}
//get set
}
这是我的班级Pessoa
(葡萄牙语中的“人”)
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@SequenceGenerator(name = "PESSOA_SEQ", sequenceName = "s_pessoa", initialValue = 1000, allocationSize = 20)
public abstract class Pessoa implements Serializable {
private static final long serialVersionUID = 5777501399478522675L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PESSOA_SEQ")
@Column(unique = true, nullable = false, updatable = false)
private Long id;
private String nome;
private String email;
private String cpf;
public Pessoa() {
}
//get set
}
这是Login
类。
@Entity
@Table(name = "login")
@NamedQueries({
@NamedQuery(name = "obterUsuarioPorUsuarioSenha", query = "select l from LoginDTO where l.usuario = :usuarioParam and l.senha = :senhaParam"),
})
public class LoginDTO implements Serializable {
private static final long serialVersionUID = 1718733412549158088L;
@Id
@Column(nullable = false)
@GeneratedValue(generator = "gen")
@GenericGenerator(name = "gen", strategy = "foreign", parameters = @Parameter(name = "property", value = "cliente"))
private Long idLoginCliente;
@OneToOne(fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
private ClienteDTO cliente;
@Column(unique = true, nullable = false)
private String usuario;
@Column(nullable = true)
private String senha;
public LoginDTO() {
}
// get set
}
我的数据库生成如下:
Table ClienteDTO(客户端) id = 1000
表Pessoa(人)
id = 1000
nome = ABC
email = xpto@xpto.xpto
cpf = 2131313124124
表登录
idlogincliente = 1000
usuario (user) = admin
senha (password) = 8c48747q4s9c4q65
我在@NamedQuery
的实体使用Login
。我使用以下方法调用EJB:
public LoginDTO efetuarLogin(LoginDTO usuarioDTO) throws Exception {
Query q = em.createNamedQuery("obterUsuarioPorUsuarioSenha");
q.setParameter("usuarioParam", usuarioDTO.getUsuario());
q.setParameter("senhaParam", usuarioDTO.getSenha());
try {
return (LoginDTO) q.getSingleResult();
} catch (NoResultException ne) {
throw new NoResultException(ne.getMessage());
}
}
问题是我无法进行查询。在这种情况下,我收到了错误
引起:javax.persistence.NoResultException:找不到查询的实体
这是因为FETCH?或者实体的关系不正确?错误的查询?
=====================================
答案:
我只是个白痴。正如你所看到的,我正在加密我的密码..在我的托管bean上,我没有加密来自表单的密码。无论如何,这个主题可能会以某种方式帮助人们。谢谢你们。