2017-06-14 60 views
0

我想在JSF項目執行Hibernate查詢,但我得到這個錯誤信息:錯誤與內部聯接查詢在Hibernate中

Caused by: org.hibernate.QueryException: could not resolve property: agencia of: entity.Conta 

這裏是我的查詢方法:

public static Conta procurarSenha(String agencia, String conta, String cpf) { 
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("BANCO"); 
    EntityManager em = factory.createEntityManager(); 
    try { 
     TypedQuery<Conta> query = em.createQuery("SELECT con from Conta c INNER JOIN c.usuario u ON c.USER_CPF = u.cpf " 
       + "WHERE c.agencia = :ag AND c.conta = :ct AND u.cpf = :cp", Conta.class); 
     query.setParameter("ag", agencia); 
     query.setParameter("ct", conta); 
     query.setParameter("cp", cpf); 
     List<Conta> contas = query.getResultList(); 
     em.clear(); 
     em.close(); 
     factory.close(); 
     if (contas != null && contas.size() > 0) { 
      return contas.get(0); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

這裏的CONTA類:

@Entity 
public class Conta { 
@EmbeddedId 
private DadosConta contaUsuario = new DadosConta(); 
private String senha; 
private String tipoConta; 
private double saldo; 
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) 
@JoinColumn(name="USER_CPF", unique= true, nullable=false, insertable=true, 
updatable=true) 
private Usuario usuario = new Usuario(); 
@OneToMany(mappedBy="contaOrigem", fetch = FetchType.LAZY) 
private List<Transacao> transacoes = new ArrayList<>(); 
... getters and setters 

這裏的DadosConta(嵌入式編號)類:

@Embeddable 
public class DadosConta { 
private String agencia; 
private String conta; 
...getters and setters 

而這裏的Usuario類:

@Entity 
@Table(name = "usuario") 
public class Usuario { 

private String nome; 
private String cpf; 
private String rg; 
private Date dataNasc; 
private String telefone; 
private String email; 
private String logradouro; 
private String numero; 
private String cep; 
...getters and setters 

當我查詢了其中的一個,沒有內部連接,它工作正常,但是當我查詢了這些類之間的連接它顯示的錯誤信息。這個查詢在MySQL中是正確的,但在Hibernate中它不起作用。任何幫助?

謝謝。

+0

儘管錯誤消息指出了一些不同的內容,但這不是SQL,但是您在使用JPQL的情況下,不允許使用「ON」子句(用不同的方式來制定相同的查詢)。 – Tiny

回答

0

你可以試試下面的查詢:

SELECT con from Conta c INNER JOIN c.usuario u WHERE 
c.contaUsuario.agencia = :ag AND c.conta = :ct AND u.cpf = :cp 

你CompoundedKey在變量名contaUsuario所以你必須通過它引用。

也由微小提到你不能使用INNER JOIN王氏ON爲你書面方式JPQL不是SQL

+0

謝謝,它的工作原理,我認爲這不是必要的,因爲agencia是來自Conta的Composite Key,但它是另一個類,我們需要使用類名稱,而不是列,對嗎?謝謝。 –

+0

準確地說,它是JPQL語法,因此您必須使用類名稱和屬性。 –

0

您在conta類找外地agencia。而不是查詢c.agencia,您需要查詢c.contaUsuario.agencia