Hibernate菜鸟求助
我用的 Hibernate3.6.10版本 mysql版本5.5
文件:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.bjsxt.hibernate.model.Student;
import com.bjsxt.hibernate.model.Teacher;
public class TeacherTest {
public static void main (String[] args){
Teacher t=new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("中级");
Configuration cfg= new Configuration();
SessionFactory sf= cfg.configure().buildSessionFactory();
Session session =sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
}
}
出现问题提示:
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ com.bjsxt.hibernate.Teacher] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2369)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
at TeacherTest.main(TeacherTest.java:17)
Caused by: java.lang.ClassNotFoundException: com.bjsxt.hibernate.Teacher
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2366)
... 6 more
我的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/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</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.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">update</property>
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>
<mapping class="com.bjsxt.hibernate.Teacher"/>
</session-factory>
</hibernate-configuration>
求大神帮忙 ,详解下 hibernate session
[解决办法]
Caused by: java.lang.ClassNotFoundException: com.bjsxt.hibernate.Teacher
找不到Teacher类啊
[解决办法]
Hibernate.cfg.xml
<mapping class="com.bjsxt.hibernate.Teacher"/>
换成(只是示例、具体的是你Teacher.hbm.xml文件的路径)
<mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/>
在不行就贴出你Teacher.hbm.xml的配置看看!
[解决办法]
没有读到teacher的xml,你把teacher不用mapping class形式,也改成
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>试试
[解决办法]
Unable to load class [ com.bjsxt.hibernate.Teacher] declared in Hibernate configuration <mapping/>
entry
在hibernate<mapping>中不能加载com.bjsxt.hibernate.Teacher这个类
java.lang.ClassNotFoundException: com.bjsxt.hibernate.Teacher
找不到com.bjsxt.hibernate.Teacher这个类
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>这个是指定hibernate映射文件的路径
<mapping class="com.bjsxt.hibernate.Teacher"/>我就不知道你写这个啥意思?????