2017-02-25 25 views
0

我一直試圖解決這個問題。我已經閱讀了多個網站和文檔,但我可以通過這個。 我在ObjectDB中使用Java。objectDB:如何檢索包含OneToMany的類

我有一個名爲Pedido的人,有一個參考ManyToOne對一個Clase Cliente。

@Entity 
public class Pedido { 
    @Id @GeneratedValue 
    private long id; 

    @Embedded private Direccion origen; 
    @ManyToOne(optional=true) 
     private Cliente cliente; //Opcional 

    @Basic(optional=false) private Date fechaYhora; //Fecha en que se debe enviar el coche 

} 

@Entity 
public class Cliente extends Persona{ 
    @OneToMany(mappedBy="Pedido.cliente") 
     public List<Pedido> pedidos; 

} 

人物角色,沒什麼特別的。只有幾個變量。 String,int和一個嵌入類。

問題是,當我嘗試獲取客戶端列表時,出現錯誤。

static public List<Cliente> getClientes() throws Exception{ 
     em = EMF.createEntityManager(); 
     List<Cliente> results = null; 
     try{ 
      TypedQuery<Cliente> query = em.createQuery("SELECT c FROM Cliente c", Cliente.class); 
      results = query.getResultList(); 
     }finally{ 
      em.close(); 
     } 
     return results; 
    } 

在此方法結束時,「結果」爲空。即使是查詢也是如此。和em.close()我得到一個異常:

com.objectdb.o._PersistenceException:無法使用反射

什麼是錯寫的 場場entidades.Cliente.pedidos值有了這個?我想我在oneToMany - manyToOne上做了一個糟糕的連接。但我無法弄清楚。

我該如何進行正確的連接?如何檢索列表sucefull?

回答

0

你要改變的mappedBy屬性的值:

@OneToMany(mappedBy="cliente") 

即規定在業主方的字段名稱不類名。

相關問題