2014-03-13 155 views
1

我在包com.hibernate中有一個名爲Student_Info的模型對象。使用休眠功能將對象保存到數據庫中

在同一個包我有在那裏我創建的Student_Info實例的主類:

public class Main { 

    public static void main(String[] args) { 
     Student_Info student = new Student_Info(); 
     student.setRollNo(1); 
     student.setName("Ichigo"); 

     SessionFactory sessionFactory = 
       new AnnotationConfiguration().configure().buildSessionFactory(); 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 

     session.save(student); 

     session.getTransaction().commit(); 
     session.close(); 
     sessionFactory.close(); 

    } 

} 

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:3306/hibernatedb</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password">toor</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.internal.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">create</property> 

     <mapping resource="com.hibernate.Student_Info" /> 
    </session-factory> 
</hibernate-configuration> 

但是當我運行的主類我得到這個錯誤:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info not found 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:767) 
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123) 
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177) 
    at com.hibernate.Main.main(Main.java:16) 

那麼,什麼是錯的,我什麼寫道,我認爲所有設置都是正確的。

編輯:

這是我的項目文件夾結構:

enter image description here

編輯2:

這是Student_Info類:

@Entity 
@Table(name="STUDENT_INFORMATION") 
public class Student_Info { 

    @Id 
    private int rollNo; 
    private String name; 

    public int getRollNo() { 
     return rollNo; 
    } 

    public void setRollNo(int rollNo) { 
     this.rollNo = rollNo; 
    } 

    public String getName() { 
     return name; 
    } 

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



} 

編輯3:

我創建了一個在包com.hibernate命名Student_Info.hbm.xml文件:

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

<hibernate-mapping> 
    <class name="com.hibernate.Student_Info" table="STUDENT_INFORMATION" catalog="hibernatedb"> 
     <id name="rollNo" type="int"> 
      <column name="rollNo" /> 
      <generator class="assigned" /> 
     </id> 
     <property name="name" type="string"> 
      <column name="name" /> 
     </property> 
    </class> 
</hibernate-mapping> 

而在hibernate.cfg.xml我改變了這一行:

<mapping resource="com.hibernate.Student_Info" /> 

有了:

<mapping resource="com.hibernate.Student_Info.hbm.xml" /> 

但是當我運行主要班級我得到這個錯誤:

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found 
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:767) 
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:123) 
    at org.hibernate.cfg.AnnotationConfiguration.addResource(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2255) 
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2227) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2207) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:213) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2160) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:201) 
    at org.hibernate.cfg.AnnotationConfiguration.doConfigure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:183) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:46) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2054) 
    at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:177) 
    at com.hibernate.Main.main(Main.java:16) 
+0

包含'Student_Info'實體的導入語句。 – Rembo

回答

0

加入後文件Student_Info.hbm.xml我不得不將它移動到文件夾src,和它的工作,但我不得不改變這一行:

<mapping resource="com.hibernate.Student_Info.hbm.xml" /> 

到:

<mapping resource="Student_Info.hbm.xml" /> 
1

試試這個

@Entity 

@Table(name="STUDENT_INFORMATION") 

public class Student_Info { 

    @Id 
    @column(name="your id column name") 
    private int rollNo; 

    @column(name="your name column name") 
    private String name; 

    public int getRollNo() { 
     return rollNo; 
    } 

    public void setRollNo(int rollNo) { 
     this.rollNo = rollNo; 
    } 

    public String getName() { 
     return name; 
    } 

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



} 

您沒有指定任何映射到表,如果你不提供任何映射然後ORM引擎會在數據庫中默認檢查相同的列名選項,如果沒有找到任何東西,它會拋出映射異常。啓用你的日誌和查找更多信息Hibernate是如何工作的...

希望這有幫助!

+0

在'hibernate.cfg.xml'中,我使用了這個' create',因此它應該創建一個名爲STUDENT_INFORMATION的新表,如果數據庫中沒有表的屬性爲'Student_Info'類作爲列。 –

1

,我認爲這是不正確>

<mapping resource="com.hibernate.Student_Info" /> 

這告訴Hibernate在哪裏位於不是實體HBM文件,然後在你可以添加實體hbm.xml文件。

<class name="Student_Info" table="Student_Info "> 
    ... 
</class> 

事實上

com.hibernate.Student_Info not found 

意味着有沒有com.hibernate.Student_Info文件,考慮讀this

UPDATE

爲了解決下一個異常,你應該考慮重命名文件如Java說。

Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com.hibernate.Student_Info.hbm.xml not found 

這意味着你應該使用

<mapping resource="Student_Info.hbm.xml" /> 

和文件名必須是名* Student_Info.hbm.xml *應在hibernate.cfg.xml中的水平

+0

請參閱我對我的帖子所做的修改 –

1

的問題是在你的hibernate.cfg.xml文件的映射標籤。

解決方案對於基於註解映射

如果你使用註解 - 然後在hibernate.cfg.xml文件中映射標籤使用屬性,而不是資源。 應該

<mapping class="com.hibernate.Student_Info"/> 

解決方案基於XML映射

如果要使用單獨的HBM文件映射然後在hibernate.cfg.xml文件,你應該在使用資源屬性映射文件。但是,在這種情況下,你必須指定,就像下面(使用/代替。

<mapping resource="com/hibernate/Student_Info.hbm.xml"/> 

希望這將解決問題的HBM文件的位置。

相關問題