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.
– Caffé
it’s all capitalized even
– user63891
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.
– Caffé
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;
– user63891
I edited the question to see if I made it easier to understand
– user63891
Place your classes Customer and request.
– Felipe Marinho
I’ve put the classes there now
– user63891