我用的 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>
求大神帮忙 ,详解下hibernatesession
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>
求大神帮忙 ,详解下hibernatesession
解决方案 »
- @Transactional在我的配置当中不起作用
- struts2报 HTTP Status 404 - 为什么?
- 求解答Hibernate关于单表中父子关系的映射文件怎么写?
- (讨论)关于pojo、dao、service的困惑
- Spring框架调用Web Service冒错什么原因?
- 请问朋友们:以前TOMCAT端口80 现在把STRUTS解压后
- JBoss4.0.0+eclipse3.0.1,在线更新安装Jboss IDE,出现错误,在线等啊
- 求教weblogic8.1中应用运行时找寻一个class的顺序
- jbuilder中的enterprise server5.1解破
- SpringMVC Controller中怎样获取label等静态表单数据?
- 关于SPRING3.0整合STRUTS2
- 关于hibernate一个很简单的manytoone的问题 看不出错在哪 能帮个忙么
<mapping class="com.bjsxt.hibernate.Teacher"/>
换成(只是示例、具体的是你Teacher.hbm.xml文件的路径)
<mapping resource="com/bjsxt/hibernate/Teacher.hbm.xml"/>在不行就贴出你Teacher.hbm.xml的配置看看!
<mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/>试试
在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"/>我就不知道你写这个啥意思?????
将后面的“/”去掉,并且在后面加上</mapping>
3.6的文档里正确的在下面:<mapping class="com.bjsxt.hibernate.Teacher"></mapping>
<mapping class=""></mapping>
是用的hibernate-annotations-3.4.0.GA