即使定义了类,我也会得到错误:org.hibernate.MappingException:未知实体

时间:2016-12-01 02:55:30

标签: java spring hibernate

project

那是我要创建的课程。

我已经更改了导入并重新编辑了注释,但错误仍在继续。我也改变了更新来创建,但没有任何改变,老实说我不知道​​发生了什么

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

}

任何线索?

1 个答案:

答案 0 :(得分:0)

您应该使用LocalSessionFactoryBean中的ApplicationContext添加带注释的课程:

<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