2014-04-22 59 views
1

我找不出爲什麼一個對象不會通過註釋映射。在完全喪失:通過休眠映射一個簡單的實體

org.hibernate.MappingException: Unknown entity: com.hibernate.practice.Car 

我鬆散以下教程here,但似乎無法得到任何工作:無論我怎麼努力,我總是收到此錯誤。我試圖將對象剝離到它的裸露骨頭(認爲我在代碼中發生錯誤),但是即使只有一個ID和一個名稱Column,我仍然無法實現這個工作。我的hibernate.cfg來自Netbeans Hibernate教程。

<?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> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/practicedb?zeroDateTimeBehavior=convertToNull</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">pass</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.hbm2ddl.auto">create</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    </session-factory> 
</hibernate-configuration> 

此外,根據說明,我添加了HibernateUtil.java類。

public class HibernateUtil { 

    private static final SessionFactory sessionFactory; 

    static { 
    try { 
      // Create the SessionFactory from standard (hibernate.cfg.xml) 
     // config file. 
     sessionFactory = new AnnotationConfiguration().configure().addPackage("com.hibernate.practice").buildSessionFactory(); 
    } catch (Throwable ex) { 
     // Log the exception. 
     System.err.println("Initial SessionFactory creation failed." + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
    } 

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

我設置了一個簡單的類。這是一個帶有ID和名字的Car

@Entity 
public class Car { 
    @Id 
    @GeneratedValue 
    @GenericGenerator(name="increment", strategy="incrememnt") 
    private long id; 

    @Column(name="car_name") 
    private String name; 

    public Car() { 
    } 

    public long getId() { 
    return id; 
    } 

    public void setId(long id) { 
    this.id = id; 
    } 

    public String getName() { 
    return name; 
    } 

    public void setName(String name) { 
    this.name = name; 
    } 





} 

最後,我想測試這個東西出來是這樣的:

@Test 
    public void testOutHibernate() { 
    SessionFactory factory = HibernateUtil.getSessionFactory(); 
    Session session = factory.openSession(); 

    Car car = new Car(); 
    car.setName("red one"); 
    session.save(car); 


    try { 
     Transaction t = session.beginTransaction(); 
     session.save(car); 
     t.commit(); 
    } 
    finally { 
     session.close(); 
    } 
    session.close(); 
    } 

沒有失敗,我得到的Unknown Entity Exception。有什麼明顯的我失蹤了嗎?

+0

你有一個錯字'strategy =「incrememnt」'< - 拼寫錯誤 –

回答

0

您需要添加註解類,如下 -

sessionFactory = new AnnotationConfiguration() 
         .addAnnotatedClass(Car.class) 
         .addPackage("com.hibernate.practice") 
         .configure() 
         .buildSessionFactory(); 

你可以找到關於addPackage()方法在這裏討論 - https://forum.hibernate.org/viewtopic.php?f=1&t=980723

+0

啊,非常感謝你! – user3308774

+0

不客氣。 –