2016-02-05 162 views
-1

測試:異常線程 「main」 org.hibernate.MappingException

import java.util.GregorianCalendar; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 

import it.s.monitoringDoor.model.Door_Open; 
import it.s.monitoringDoor.model.HibernateUtilities; 

public class TestHibernate { 
    public static void main(String[] args){ 
     SessionFactory factory = HibernateUtilities.getSessionFactory(); 

     Door_Open d = new Door_Open(); 
     d.setId(1); 
     d.setId_door(1);; 
     d.setTimestamp(getTime()); 

     Session s = factory.openSession(); 

     Transaction t = s.getTransaction(); 
     t.begin(); 
     s.save(d); 
     t.commit(); 


    } 
    private static Date getTime(){ 
     GregorianCalendar gc = new GregorianCalendar(); 
     return gc.getTime(); 
    } 

} 

HibernateUtilities:

package it.s.monitoringDoor.model; 

import java.util.Properties; 

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

public class HibernateUtilities { 

    //XML based configuration 
    private static SessionFactory sessionFactory; 


    private static SessionFactory buildSessionFactory() { 
     try { 
      // Create the SessionFactory from hibernate.cfg.xml 
      Configuration configuration = new Configuration(); 
      configuration.configure("hibernate.cfg.xml"); 
      System.out.println("Hibernate Configuration loaded"); 

      ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
      System.out.println("Hibernate serviceRegistry created"); 

      SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

      return sessionFactory; 
     } 
     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() { 
     if(sessionFactory == null) 
      sessionFactory = buildSessionFactory(); 
     return sessionFactory; 
    } 

} 

POJO類Door_Open.java

package it.selco.monitoringDoor.model; 

import java.util.Date; 

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

    @Entity 
    @Table(name = "door_open") 
    public class Door_Open { 
     //---------------- Instance variables ---------------- 
     @Id 
     @GeneratedValue 
     @Column(name = "id") 
     private int id; 
     @Column(name = "id_door", nullable = false) 
     private int id_door; 
     @Column(name = "id_door", nullable = false) 
     private Date timestamp; 

     //-------------------- Constructor -------------------- 
     public Door_Open(){ 
      ; 
     } 

     public Door_Open(int id_door, Date timestamp) { 
      setId_door(id_door); 
      setTimestamp(timestamp); 
     } 


     //--------------------- Get & Set --------------------- 

     public int getId() { 
      return id; 
     } 


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

     @SuppressWarnings("unused") 
     private int getId_door() { 
      return id_door; 
     } 

     public void setId_door(int id_door) { 
      this.id_door = id_door; 
     } 

     @SuppressWarnings("unused") 
     private Date getTimestamp() { 
      return timestamp; 
     } 

     public void setTimestamp(Date timestamp) { 
      this.timestamp = timestamp; 
     } 

     //--------------------- Methods ---------------------- 
     //TODO Auto-generated methods variables block 

    } 

的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
             "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory name=""> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost/monitor_door</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password"/> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <!-- ############################################ 
# mapping files with external dependencies # 
############################################ --> 
    <mapping resource="it/s/monitoringDoor/model/Door_Open.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

Door_Open.hbm.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="Door_Open" table="door_open" schema="monitor_door"> 
     <id name="id" type="int" column="id"> 
     <generator class="native"/> 
     </id> 
     <property name="id_door" column="id_door" type="int" not-null="true"/> 
     <property name="timestamp" column="timestamp" type="timestamp" not-null="true"/> 
    </class> 
</hibernate-mapping> 

當我運行測試返回我下面的錯誤。

異常線程 「main」 org.hibernate.MappingException:未知實體:it.s.monitoringDoor.model.Door_Open [...]

一些建議嗎?

+0

您是否試圖通過該程序?例外發生在哪裏?你是否能夠進一步找到錯誤?你應該提供一個「最小的完整工作示例」,而不是你的完整程序,只是問「錯誤在哪裏?」 – Anedar

回答

0

看包你Door_Open類的,那就是:it.selco.monitoringDoor.model而在hibernate.cfg.xmlDoor_Open.hbm.xml文件是:<mapping resource="it/s/monitoringDoor/model/Door_Open.hbm.xml"/>。包和映射資源路徑不匹配,它們應該是相同的,例如, it/selco/monitoringDoor/model/Door_Open.hbm.xml 並且當然將Door_Open.hbm.xml移動到與其類相同的路徑。

希望這會有所幫助。

PS,你爲什麼要爲你的課程使用兩個註解(例如@Entity)和hbm映射?我認爲你應該選擇一個不是兩個。

相關問題