我在包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)
那麼,什麼是錯的,我什麼寫道,我認爲所有設置都是正確的。
編輯:
這是我的項目文件夾結構:
編輯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)
包含'Student_Info'實體的導入語句。 – Rembo