2016-12-01 88 views
0

project即使在類中定義,我得到的錯誤:org.hibernate.MappingException:未知實體

這就是我想要創建的類。

我已經更改了導入並重新編輯了註釋,但錯誤仍在繼續。我也改變了更新創造,但一切都沒有改變,我真的不知道發生了什麼事情

package br.com.javaparaweb.financeiro.usuario; 

import java.io.*; 
import java.util.*; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 

//import java.util.Date; 




    @Entity 
    public class Usuario implements Serializable { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
@Id 
@GeneratedValue 
private Integer codigo; 
private String nome; 
private String email; 
@org.hibernate.annotations.NaturalId 
private String login; 
private String senha; 
private Date nascimento; 
private String celular; 
private String idioma; 
private boolean ativo; 

GET AND SETS 

@Override 
public int hashCode() { 
    final int prime = 31; 
    int result = 1; 
    result = prime * result + (ativo ? 1231 : 1237); 
    result = prime * result + ((celular == null) ? 0 : celular.hashCode()); 
    result = prime * result + ((codigo == null) ? 0 : codigo.hashCode()); 
    result = prime * result + ((email == null) ? 0 : email.hashCode()); 
    result = prime * result + ((idioma == null) ? 0 : idioma.hashCode()); 
    result = prime * result + ((login == null) ? 0 : login.hashCode()); 
    result = prime * result + ((nascimento == null) ? 0 : nascimento.hashCode()); 
    result = prime * result + ((nome == null) ? 0 : nome.hashCode()); 
    result = prime * result + ((senha == null) ? 0 : senha.hashCode()); 
    return result; 
} 
@Override 
public boolean equals(Object obj) { 
    if (this == obj) 
     return true; 
    if (obj == null) 
     return false; 
    if (getClass() != obj.getClass()) 
     return false; 
    Usuario other = (Usuario) obj; 
    if (ativo != other.ativo) 
     return false; 
    if (celular == null) { 
     if (other.celular != null) 
      return false; 
    } else if (!celular.equals(other.celular)) 
     return false; 
    if (codigo == null) { 
     if (other.codigo != null) 
      return false; 
    } else if (!codigo.equals(other.codigo)) 
     return false; 
    if (email == null) { 
     if (other.email != null) 
      return false; 
    } else if (!email.equals(other.email)) 
     return false; 
    if (idioma == null) { 
     if (other.idioma != null) 
      return false; 
    } else if (!idioma.equals(other.idioma)) 
     return false; 
    if (login == null) { 
     if (other.login != null) 
      return false; 
    } else if (!login.equals(other.login)) 
     return false; 
    if (nascimento == null) { 
     if (other.nascimento != null) 
      return false; 
    } else if (!nascimento.equals(other.nascimento)) 
     return false; 
    if (nome == null) { 
     if (other.nome != null) 
      return false; 
    } else if (!nome.equals(other.nome)) 
     return false; 
    if (senha == null) { 
     if (other.senha != null) 
      return false; 
    } else if (!senha.equals(other.senha)) 
     return false; 
    return true; 
} 

} 

這就是cfg.xml文件,你可以看到,這個類是正確映射,什麼都可以錯了嗎?

<?xml version="1.0" encoding="UTF-8"?> 
<hibernate-configuration> 
<session-factory> 
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/financeiro</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">90402020</property> 
    <property name = "current_session_context_class">thread</property> 
    <property name = "hibernate.hbm2ddl.auto">update</property>   

    <mapping class="br.com.javaparaweb.financeiro.usuario.Usuario"/> 

</session-factory> 
</hibernate-configuration> 

private static SessionFactory buildSessionFactory(){ 

    try{ 
     Configuration cfg = new Configuration(); 
     cfg.configure("hibernate.cfg.xml"); 
     StandardServiceRegistryBuilder registradorServiço = new StandardServiceRegistryBuilder(); 
     registradorServiço.applySettings(cfg.getProperties()); 
     StandardServiceRegistry servico = registradorServiço.build(); 
     return cfg.buildSessionFactory(servico); 
    }catch(Throwable e){ 
     System.out.println("Criação do Objeto inicial do SesseionFactory falhou. Erro:" +e); 
     throw new ExceptionInInitializerError(e); 
    } 
} 
public static SessionFactory getSessionFactory(){ 
    return sessionFactory; 
} 

}

這裏是爲用戶類DAO工廠:

public class DAOFactory { 

public static UsuarioDAO criarUsarioDAO(){ 
UsuarioDAOHibernate usuarioDAO = new UsuarioDAOHibernate(); 
``usuarioDAO.setSession(HibernateUtil.getSessionFactory().getCurrentSession()); 

    return usuarioDAO; 
} 

}

任何線索?

+0

增加整個異常堆棧。 –

+0

@Janson你解決了你的問題了嗎? –

+0

@Càphen一個忘記sya,有一個名爲HibernateUtil的類,這是一個resposable創建會話,將編輯我的第一篇文章,所以你可以看到它。 – Janson

回答

0

你應該ApplicationContext添加使用LocalSessionFactoryBean註解類:

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 

    <!-- Refer to hbn cfg --> 
    <property name="configLocation">  
     <value> 
      classpath:location_of_config_file/hibernate.cfg.xml 
     </value> 
    </property> 

    <property name="dataSource" ref="dataSource"/> 
    <property name="annotatedClasses"> 
    <list> 
     <value>br.com.javaparaweb.financeiro.usuario.Usuario</value> 
    </list> 
    </property> 
</bean> 

或自動掃描與@Entity註解類:

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 

    <!-- Refer to hbn cfg --> 
    <property name="configLocation">  
     <value> 
      classpath:location_of_config_file/hibernate.cfg.xml 
     </value> 
    </property> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="packagesToScan" value="br.com.javaparaweb.financeiro.usuario"/> 
</bean> 

Reference

相關問題