Error making associations in XML with Hibernate

Asked

Viewed 56 times

0

I’m having trouble making associations Many-to-one. Any help I appreciate

Exception in thread "AWT-Eventqueue-0" org.hibernate.Mappingexception: An Association from the table material refers to an unmapped class: Budget

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 + '}';
    }
   
    
    
}

XML mapping of the 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>

Request Class

package Classes;


public class Pedido {
    private Integer ID_Pedido;
    private String Tipo_Servico;
    private Cliente cliente;
    private Orcamento orcamento;  
    
    
    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 getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Orcamento getOrcamento() {
        return orcamento;
    }

    public void setOrcamento(Orcamento orcamento) {
        this.orcamento = orcamento;
    }

    
    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 + '}';
    }
    
    
    
}

XML Mapping of the 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="tipoServico" type="string" length ="50" column="Tipo_Servico"/>
      <many-to-one name="cliente" column="ID_Cliente" class="Cliente"/>
   </class>
</hibernate-mapping>

Orcamento class

package Classes;

import java.io.Serializable;

public class Orcamento implements Serializable {

   
    private Integer ID_Orcamento;
    private String nomeCliente;
    private String tipoServico;
    private Double totalOrcamento;
    private Pedido pedido;

    public Orcamento() {
    }

    public Orcamento(Integer ID_Orcamento) {
        this.ID_Orcamento = ID_Orcamento;
    }

      public Integer getID_Orcamento() {
        return ID_Orcamento;
    }

    public void setID_Orcamento(Integer ID_Orcamento) {
        this.ID_Orcamento = ID_Orcamento;
    }

    public Pedido getPedido() {
        return pedido;
    }

    public void setPedido(Pedido pedido) {
        this.pedido = pedido;
    }
   
    public String getNomeCliente() {
        return nomeCliente;
    }

    public void setNomeCliente(String nomeCliente) {
        this.nomeCliente = nomeCliente;
    }

    public String getTipoServico() {
        return tipoServico;
    }

    public void setTipoServico(String tipoServico) {
        this.tipoServico = tipoServico;
    }

    public Double getTotalOrcamento() {
        return totalOrcamento;
    }

    public void setTotalOrcamento(Double totalOrcamento) {
        this.totalOrcamento = totalOrcamento;
    }

   

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (ID_Orcamento != null ? ID_Orcamento.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 Orcamento)) {
            return false;
        }
        Orcamento other = (Orcamento) object;
        if ((this.ID_Orcamento == null && other.ID_Orcamento != null) || (this.ID_Orcamento != null && !this.ID_Orcamento.equals(other.ID_Orcamento))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Classes.Orcamento[ iDOrcamento=" + ID_Orcamento + " ]";
    }
    
}

XML mapping of the budget

<?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.Orcamento" table="orcamento">
  <id name="ID_Orcamento" column="ID_Orcamento">
      <generator class="native"/>
  </id>    
      <property name="nomeCliente" type="string" length ="50" column="Nome_Cliente" />
      <property name="tipoServico" type="string" length ="50" column="Tipo_Servico"/>
      <property name="totalOrcamento" type="double" length="50" column="Total_Orcamento"/>
      <many-to-one name="pedido" column="ID_Pedido" class="Pedido" />
 </class>
</hibernate-mapping>

Material class

package Classes;

import java.io.Serializable;


public class Material implements Serializable {


    private Integer IDMaterial;
    private String descricaoProduto;
    private Integer quantidade;
    private Double valorUnitario;
    private Double valorTotal;
    private Orcamento orcamento;

    public Material() {
    }

    public Material(Integer IDMaterial) {
        this.IDMaterial = IDMaterial;
    }

    public Integer getIDMaterial() {
        return IDMaterial;
    }

    public void setIDMaterial(Integer IDMaterial) {
        this.IDMaterial = IDMaterial;
    }

    public Orcamento getOrcamento() {
        return orcamento;
    }

    public void setOrcamento(Orcamento orcamento) {
        this.orcamento = orcamento;
    }

       
    public String getDescricaoProduto() {
        return descricaoProduto;
    }

    public void setDescricaoProduto(String descricaoProduto) {
        this.descricaoProduto = descricaoProduto;
    }

    public Integer getQuantidade() {
        return quantidade;
    }

    public void setQuantidade(Integer quantidade) {
        this.quantidade = quantidade;
    }

    public Double getValorUnitario() {
        return valorUnitario;
    }

    public void setValorUnitario(Double valorUnitario) {
        this.valorUnitario = valorUnitario;
    }

    public Double getValorTotal() {
        return valorTotal;
    }

    public void setValorTotal(Double valorTotal) {
        this.valorTotal = valorTotal;
    }


    @Override
    public int hashCode() {
        int hash = 0;
        hash += (IDMaterial != null ? IDMaterial.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 Material)) {
            return false;
        }
        Material other = (Material) object;
        if ((this.IDMaterial == null && other.IDMaterial != null) || (this.IDMaterial != null && !this.IDMaterial.equals(other.IDMaterial))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "Classes.Material[ iDMaterial=" + IDMaterial + " ]";
    }
    
}

XML mapping of Material

<?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.Material" table="material">
       <id name="IDMaterial" column="IDMaterial">
             <generator class="native"/>
       </id>
        <property name="descricaoProduto" type="string" length ="50" column="Descricao_Produto" />
        <property name="quantidade" type="integer" column="Quantidade"/>
        <property name="valorUnitario" type="double" column="Valor_Unitario"/>
        <property name="valorTotal" type="double" column="Valor_Total"/>
        <many-to-one name="orcamento" column="ID_Orcamento" class="Orcamento"/>
  </class>   
</hibernate-mapping>

Hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/prestadora</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.show_sql">true</property>
    <mapping resource="Classes/Funcionario.hbm.xml"/>
    <mapping resource="Classes/Material.hbm.xml"/>
    <mapping resource="Classes/Orcamento.hbm.xml"/>
    <mapping resource="Classes/Cliente.hbm.xml"/>
    <mapping resource="Classes/Pedido.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

  • 1

    Some reason why you use mapping via XML instead of mapping by annotations?

  • That’s how I’m learning in my course, there’s something wrong with doing so ? or not ?

  • It’s just that this way of using XML is a very old way, it’s archaic. It is still supported for compatibility reasons and to support some very rare specific cases that annotations do not support. 99% of the time, currently you will do everything through JPA annotations, package javax.persistence.

  • I just want to solve this problem even when I finish the course I try to rewrite this mapping ai kk

  • if you can help me solve this problem then I thank you

No answers

Browser other questions tagged

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