2016-05-26 8 views
0

Hibernate 5.1 verwendethabe ich den richtigen Import aber der Fehler immer noch org.hibernate.MappingException in Hibernate 5

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

Der Konfigurationscode

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> 

passieren Der Fehler:

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) 
+0

Bitte fügen Sie eine Hibernate-Version und den Code, in dem Sie eine Sitzung Fabrik erstellen. –

+0

'br.com.condominio.Model.Apartamento' ist nicht' br.com.condominio.model.Apartamento' ... Beachten Sie die Änderung des Gehäuses in 'model' ... –

+0

Ich habe den Fehler geändert – LorD

Antwort

0

Der Grund dafür ist Ihr Konfigurationscode

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

Gerade

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

Vielen Dank! Es klappt! – LorD

+0

@DenysWylliam Gern geschehen –