2012-04-02 9 views
12

が、私は本当に単純なクラスを持っている(@Entityまたはインポートjavax.persistence.Entityを欠落していません)このMappingExceptionを得続ける:Hibernateは、未知のエンティティは

Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.assessme.com.entity.User 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1172) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1316) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:670) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:662) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:658) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) 
    at $Proxy4.save(Unknown Source) 
    at Main.main(Main.java:20) 

私HibernateUtilのは、次のとおりです。

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 

public class HibernateUtil { 

    private static SessionFactory sessionFactory; 
    private static ServiceRegistry serviceRegistry; 

    public static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      Configuration configuration = new Configuration(); 
      configuration.configure(); 
      serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();  
      return new Configuration().configure().buildSessionFactory(serviceRegistry); 
     } catch (Throwable ex) { 
      // Make sure you log the exception, as it might be swallowed 
      System.err.println("Initial SessionFactory creation failed." + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); 
     return sessionFactory; 
    } 

} 

私は非常に多くの重複を見てきましたが、決議は私のために働くように見えないので、誰も任意のアイデアを持っていますか?これは私の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> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql://localhost/ssme</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">mypassword</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">update</property> 

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

答えて

10

のように追加することができますhibernate.cfg.xmlのを持っている場合は、代わりにConfigurationAnnotationConfigurationを使用して、マップされたクラスのリストにあなたのクラスを追加する

configuration.addAnnotatedClass(User.class); 

を呼び出す必要があります。

+0

ああに線の下に追加し、すべて一つのクラスにあなたが言うことを行うためのベストプラクティスですか? – david99world

+0

プロジェクトのサイズによって異なります。マッピングされたクラスが2つしかない場合は、これを 'HibernateUtil'で行うことができます。大きなプロジェクトの場合は、リフレクションを使用して、特定のパッケージのすべてのクラスを休止状態に追加するか、マッピング設定ファイルを使用できます。 – phlogratos

2

persistence.xmlまたはhibernate.cfg.xmlはありますか?あなたがマッピングクラスを追加しなければならない場合は、クラスがデータベースにマッピングされていることをhibernateが理解するようにしてください。あなたはこの <mapping class="com.entity.Users"/>

10

</session-factory>この前hibernate.cfg.xmlに入れる:

<mapping class="org.assessme.com.entity.User" /> 
3

AnnotationConfigurationhereを説明を使用することを検討してください。

addClass(User.class)メソッドを使用するか、複数のエンティティを永続化する場合はaddPackage("org.assessme.com.entity")メソッドを使用します。

あなたはこのHibernateUtilクラスでホイールを再発明しています。 Springを使用すると、AnnotationSessionFactoryBeanのようなものを利用できます。

1

私のcfg.xmlファイルにhbm.xmlマッピングを追加するのを忘れました。私がそれを追加するとすぐに、この例外は消えました。

<mapping resource="com/mycompany/backend/hibernate/CServiceCenters.hbm.xml" /> 
1

私はHibernateUtilの中でこれを行う必要があり、あなたはhibernate.cfg.xml

<mapping class="fully qualified Entity class name"/> 
関連する問題