2015-10-18 64 views
0

閱讀關於PluralSight的「Introduction to Hibernate」教程。我有這個異常錯誤。完整的錯誤是:線程「main」中的異常org.hibernate.MappingException:未知實體

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.simpleprogrammer.User 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1451) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:678) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:670) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:665) 
    at com.simpleprogrammer.Program.main(Program.java:15) 

不知道什麼是錯的。我創建了User.java pojo。我創建了一個與pojo相匹配的表格。我創建了映射,然後將映射添加到hibernate.cfg.xml文件中。但是,仍然收到錯誤。任何人都可以幫助我通過這個?

的hibernate.cfg.xml

Program.java

public class Program { 
    public static void main(String[] args) { 
     System.out.println("Hello World!"); 
     Session session = HibernateUtilities.getSessionFactory().openSession(); 
     session.beginTransaction(); 

     User user = new User(); 
     user.setName("Joe"); 
     user.setGoal(250); 
     session.save(user); 

     session.getTransaction().commit(); 
     session.close(); 
     HibernateUtilities.getSessionFactory().close(); 
    } 
} 

User.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<!-- Generated Oct 17, 2015 11:44:47 AM by Hibernate Tools 3.5.0.Final --> 
<hibernate-mapping> 
    <class name="com.simpleprogrammer.User" table="USERS"> 
     <id name="id" type="int"> 
      <column name="ID" /> 
      <generator class="identity" /> 
     </id> 
     <property name="name" type="java.lang.String"> 
      <column name="NAME" /> 
     </property> 
     <property name="total" type="int"> 
      <column name="TOTAL" /> 
     </property> 
     <property name="goal" type="int"> 
      <column name="GOAL" /> 
     </property> 
    </class> 
</hibernate-mapping> 

User.java

package com.simpleprogrammer; 

public class User { 
    private int id; 
    private String name; 
    private int total; 
    private int goal; 

    public int getId() { 
     return id; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public int getTotal() { 
     return total; 
    } 
    public void setTotal(int total) { 
     this.total = total; 
    } 
    public int getGoal() { 
     return goal; 
    } 
    public void setGoal(int goal) { 
     this.goal = goal; 
    } 
} 

HibernateUtilities.java

package com.simpleprogrammer; 

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

public class HibernateUtilities { 

    private static SessionFactory sessionFactory; 
    private static ServiceRegistry serviceRegistry; 

    static { 
     try { 
      Configuration configuration = new Configuration().configure(); 

      serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     } 
     catch(HibernateException exception) { 
      System.out.println("Problem creating session factory!"); 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 
+0

你已經在該位置的映射XML?也只是在cfg文件中添加'' – Saravana

+0

是的,我確信XML在那個位置。我還添加了一個映射標籤只是爲了資源 – LinhSaysHi

+0

我沒有安裝:(你能嘗試的路徑恩'/ COM/simpleprogrammer/User.hbm.xml' – Saravana

回答

0

我用你的榜樣,並能夠得到它有一些小的調整工作。

驗證User.hbm.xml位於src/main/resources/com/simpleprogrammer中。改變在hibernate.cfg.xml爲小寫table="users"

然後:

  1. 除去從會話工廠name=""屬性。
  2. 增加了<property name="hibernate.connection.password">SomePassword</property>
  3. 添加<property name="hibernate.hbm2ddl.auto">create</property>
1

請檢查您在MySQL創建的架構名稱和一個在hibernate.cfg file.It specied是不匹配對我來說,我是越來越相同的問題。

還修改如下所示的連接url,並從hibernate.cfg中刪除默認模式字段。 jdbc:mysql:// localhost:3306/protein_tracker

這對我有用。

6

LinhSaysHi,我用Hibernate 5執行了你的代碼,並且我有完全相同的錯誤。 我用Hibernate 4執行了它,沒有問題。

上Pluralsight的教程已經對Hibernate 4. 這裏創建一個會話工廠生成器,使用Hibernate的5部作品:

public class HibernateUtilities_5 { 

    private static final SessionFactory sessionFactory = buildSessionFactory(); 

    private static SessionFactory buildSessionFactory() { 
     try { 
      StandardServiceRegistry standardRegistry = new StandardServiceRegistryBuilder() 
        .configure("hibernate.cfg.xml").build(); 
      Metadata metadata = new MetadataSources(standardRegistry).getMetadataBuilder().build(); 
      return metadata.getSessionFactoryBuilder().build(); 

     } catch (HibernateException he) { 
      System.out.println("Session Factory creation failure"); 
      throw he; 
     } 
    } 

    public static SessionFactory getSessionFactory() { 
     return sessionFactory; 
    } 
} 
+0

?感謝科裏你的解決方案像一個魅力工作。但我怎麼能檢查一樣?我的意思是我怎麼能降級我的冬眠版本4,檢查像你。如果我知道這個過程中,我可以解決我的錯誤是由於如果你使用Maven你可以指定你在pom.xml想要的版本版本中future.Thanks問題提前.. – srinivas

+1

嘛。 – Coli

+0

Ok @ Coli.Thank u .. – srinivas

6

而與Hibernate 5.1.1試圖出來我也面臨着同樣的錯誤。但是我解決了這個問題,將ServiceRegistry接口替換爲StandardServiceRegistryBuilder,並使用「metadatasources」創建會話工廠。請看我的代碼如下

static 
{ 
    try 
    { 
     //Configuration configuration = new Configuration().configure(); 
     standardServiceRegistry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml") 
            .build(); 
     Metadata metadata = new MetadataSources(standardServiceRegistry) 
           .getMetadataBuilder().build(); 
     sessionFactory = metadata.getSessionFactoryBuilder().build(); 

    }catch(HibernateException exception) 
    { 
     System.out.println("problem creating session factory!"); 
    } 
} 

public static SessionFactory getSessionFactory(){ 

    return sessionFactory; 
} 

我做了這些改變,它的工作就像一個魅力。 請在您的代碼中對您加載配置文件的方式進行更改並重試。讓我知道你是否仍然面臨障礙。

+2

您的解決方案對我無效 – Valerii

0

更改代碼,每休眠5

static 
    { 
     try { 
      //Configuration configuration = new Configuration().configure(); 
      serviceRegistry = new 
      StandardServiceRegistryBuilder().configure("hibernate.cfg.xml") 
             .build(); 
      Metadata metadata = new MetadataSources(serviceRegistry) 
           .getMetadataBuilder().build(); 
      sessionFactory = metadata.getSessionFactoryBuilder().build(); 
     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
     } 
    } 
    public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
    } 

給 充分的類路徑中user.hbm.xml

 <class name="com.simpleprogrammer.Users" table="USERS"> 

映射在hibernate.cfg.xml資源

 <mapping resource="com/simpleprogrammer/Users.hbm.xml"/> 

並執行下面的代碼

Session session = HibernateUtilities.getSessionFactory().openSession(); 

    session.beginTransaction(); 

    Users user = new Users(); 

    user.setuserName("Prince"); 
    user.setGoal(100); 
    user.setId(101); 
    user.setTotal(10); 

    session.save(user); 

    session.getTransaction().commit(); 
    session.close(); 
    HibernateUtilities.getSessionFactory().close(); 
相關問題