我把正确的导入但错误仍然发生在Hibernate 5中的org.hibernate.MappingException

时间:2016-05-26 13:20:34

标签: java hibernate hibernate-5.x

使用Hibernate 5.1

package br.com.condominio.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Apartamento {

@Id
@GeneratedValue
private Integer id;

private int numero;
private int quartos;
private String ocupacao;

public int getNumero() {
    return numero;
}

public void setNumero(int numero) {
    this.numero = numero;
}

public int getQuartos() {
    return quartos;
}

public void setQuartos(int quartos) {
    this.quartos = quartos;
}

public String getOcupacao() {
    return ocupacao;
}

public void setOcupacao(String ocupacao) {
    this.ocupacao = ocupacao;
}
}

配置代码

package br.com.condominio.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
    serviceRegistryBuilder.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    return configuration.buildSessionFactory(serviceRegistry);
}

public static SessionFactory getSessionfactory() {
    return sessionFactory;
}

public static void salvar(Object object){

    Session session = getSessionfactory().openSession();
    session.beginTransaction();
    session.save(object);
    session.getTransaction().commit();
    session.close();
}

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property      name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <!-- A linha abaixo pode ser um endereço ip -->
    <property name="hibernate.connection.url">jdbc:mysql://localhost/bancoapartamento</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password"></property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>

    <mapping class="br.com.condominio.model.Apartamento" />
</session-factory>
</hibernate-configuration>

错误:

org.hibernate.MappingException: Unknown entity: br.com.condominio.Model.Apartamento
    org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1533)
    org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:682)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:674)
    org.hibernate.internal.SessionImpl.save(SessionImpl.java:669)
    br.com.condominio.hibernate.HibernateUtil.salvar(HibernateUtil.java:29)
    br.com.condominio.controller.ApartamentoController.salvar(ApartamentoController.java:32)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

1 个答案:

答案 0 :(得分:0)

原因是您的配置代码

 Configuration configuration = new Configuration().configure();
    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder();
    serviceRegistryBuilder.applySettings(configuration.getProperties());
    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    return configuration.buildSessionFactory(serviceRegistry); 

只是

 return new Configuration().configure().buildSessionFactory();