我需要选择多个实体,用 JPA 2.0 映射并且我正在使用 Hibernate。
我正在验证用户登录。我有一个ClienteDTO
扩展抽象类Pessoa
(人)的类(又名客户端)
@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
}
我的数据库是这样生成的:
表 ClienteDTO(客户端)id = 1000
佩索阿表 (人)
id = 1000
nome = ABC
email = xpto@xpto.xpto
cpf = 2131313124124
表登录
idlogincliente = 1000
usuario (user) = admin
senha (password) = 8c48747q4s9c4q65
我正在使用@NamedQuery
atLogin
的实体。我使用以下方法调用 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 上,我没有从表单中加密密码。无论如何,这个线程可能会以某种方式帮助人们。多谢你们。