org.hibernate.Queryexception: could not resolve Property: Request of: Classes.Client

Asked

Viewed 2,056 times

0

I’m having trouble mounting a query in HQL in Hibernate,:

Exception in thread "AWT-Eventqueue-0" org.hibernate.Queryexception: could not resolve Property: Request of: Classes.Client [select c from Classes.Client c Inner Join c.Request as Request]

The query I made in Mysql is this but in Hibernate is giving error:

select cliente.Nome,pedido.Tipo_Servico from cliente join pedido;

Any help I appreciate

Query screen

  SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

  Session session = sessionFactory.openSession();

   List tabela = session.createQuery("select c from Cliente c inner join c.Pedido as Pedido").list();
   for (Iterator iterator = tabela.iterator(); iterator.hasNext();){
   Cliente clien = (Cliente) iterator.next(); 
  Pedido ped = (Pedido) iterator.next(); 
   DefaultTableModel model = (DefaultTableModel) TBL_Orcamento.getModel();
   model.addRow(new Object[]{


   clien.getNome(),
   ped.getTipo_Servico(),

     });

    }

Mapping of the XML Client

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="Classes.Cliente" table="cliente" >
  <id name="ID_Cliente" column="ID_Cliente">
      <generator class="native"/>
  </id>
     <property name="Nome" type="string" length ="50" column="Nome" />

  </class>
</hibernate-mapping>

Mapping of the XML Request

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="Classes.Pedido" table="pedido">
  <id name="ID_Pedido" column="ID_Pedido">
      <generator class="native"/>
  </id>    
      <property name="Tipo_Servico" type="string" length ="50" column="Tipo_Servico"/>
      <one-to-one name="clienteID_Cliente" cascade="all"/>
   </class>
</hibernate-mapping>

Client class

package Classes;


public class Cliente {

    private Integer ID_Cliente;
    private String Nome;


    public Cliente(){

    }

    public Cliente(Integer ID_Cliente){
    this.ID_Cliente=ID_Cliente; 

    }


    public String getNome() {
        return Nome;
    }

    public void setNome(String Nome) {
        this.Nome = Nome;
    }

    public Integer getID_Cliente() {
        return ID_Cliente;
    }

    public void setID_Cliente(Integer ID_Cliente) {
        this.ID_Cliente = ID_Cliente;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (ID_Cliente != null ? ID_Cliente.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Cliente)) {
            return false;
        }
        Cliente other = (Cliente) object;
        return !((this.ID_Cliente == null && other.ID_Cliente != null) || (this.ID_Cliente != null && !this.ID_Cliente.equals(other.ID_Cliente)));
    }



    @Override
    public String toString() {
        return "Classes.Cliente{" + "ID_Cliente=" + ID_Cliente + '}';
    }



}

Request Class

package Classes;


public class Pedido {
    private Integer ID_Pedido;
    private String Tipo_Servico;
    private Cliente clienteID_Cliente;



    public Pedido() {

    }

    public Pedido(Integer ID_Pedido) {
        this.ID_Pedido = ID_Pedido;
    }

      public Integer getID_Pedido() {
        return ID_Pedido;
    }

    public void setID_Pedido(Integer ID_Pedido) {
        this.ID_Pedido = ID_Pedido;
    }

    public String getTipo_Servico() {
        return Tipo_Servico;
    }

    public Cliente getClienteID_Cliente() {
        return clienteID_Cliente;
    }

    public void setClienteID_Cliente(Cliente clienteID_Cliente) {
        this.clienteID_Cliente = clienteID_Cliente;
    }

    public void setTipo_Servico(String Tipo_Servico) {
        this.Tipo_Servico = Tipo_Servico;
    }

     @Override
    public int hashCode() {
        int hash = 0;
        hash += (ID_Pedido != null ? ID_Pedido.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Pedido)) {
            return false;
        }
        Pedido other = (Pedido) object;
        return !((this.ID_Pedido == null && other.ID_Pedido != null) || (this.ID_Pedido != null && !this.ID_Pedido.equals(other.ID_Pedido)));
    }



    @Override
    public String toString() {
        return "Classes.Pedido{" + "ID_Pedido=" + ID_Pedido + '}';
    }



}
  • If "request" is a "client" property, it is likely to be lower case. Therefore, the query should be Join c.request instead of Regulation c.Request. How about using annotation instead of xml to map entities? It seems simpler to me.

  • it’s all capitalized even

  • Write the code according to the Java convention that will make things easier for you. For example: as the capital letter drew too much attention to me, I did not notice that the letter itself Join It’s wrong. Don’t need to Join there if request is a property of client. Just "browse" to the request, either in the query or in the consumption of the returned entities. Ex: "...from c Where c.pedido.valorTotal > 100". Include class code if you need more help. This is a very manly subject and if no answer has appeared it is because the question is incomplete.

  • This is how I did the query in Mysql but not the right one in Hibernate: select client. Name, request.Type_service from client Join request;

  • I edited the question to see if I made it easier to understand

  • Place your classes Customer and request.

  • I’ve put the classes there now

Show 2 more comments

1 answer

0


Queries made in HQL or JPQL have as reference the name of its classes, attributes and the relationship between them. Based on the relationship expressed in your mapping the query should be done this way.

SELECT p FROM Pedido p JOIN FETCH p.clienteID_Cliente

Query screen

SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

Session session = sessionFactory.openSession();

List<Pedido> pedidos = session.createQuery("SELECT p FROM Pedido p JOIN FETCH p.clienteID_Cliente", Pedido.class).getResultList();
for (Pedido pedido : pedidos) {
    Cliente cliente = pedido.getClienteID_Cliente();
    DefaultTableModel model = (DefaultTableModel) TBL_Orcamento.getModel();
    model.addRow(new Object[]{
        cliente.getNome(),
        pedido.getTipo_Servico()
    });
}
session.close();

Note: Do not use the method list(), it is marked as obsolete. I use the method getResultList().

  • Thanks, I just did not understand why the query was reversing making the query from the request to the client

  • since in Mysql it is the client for the order

  • @Douglaslopes Because you mapped that way. If you look at your classes, you’ll notice that your Pedido keeps a reference to the Cliente, i.e., the Pedido knows the Cliente who did. However, the Cliente has no reference to the Pedido accomplished. Therefore, he cannot "inform you" what requests he made.

  • ah, ta but strange just follow what I did in mysql and it is quite different one query to another

Browser other questions tagged

You are not signed in. Login or sign up in order to post.