Exception in thread "main" org.hibernate.MappingException: Unable to load class [ cn.lcc.hibernaet.model.MyTeacher] declared in Hibernate configuration <mapping/> entryCaused by: java.lang.ClassNotFoundException: cn.lcc.hibernaet.model.MyTeacher刚刚学习hibernate 真心不懂,我直接贴代码吧
先上异常吧
七月 24, 2012 10:36:03 上午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
七月 24, 2012 10:36:03 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: cn/lcc/hibernate/model/User.hbm.xml
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ cn.lcc.hibernaet.model.MyTeacher] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2133)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at cn.lcc.hibernate.test.HBTest.testMyTeacher(HBTest.java:54)
at cn.lcc.hibernate.test.HBTest.main(HBTest.java:107)
Caused by: java.lang.ClassNotFoundException: cn.lcc.hibernaet.model.MyTeacher
at java.net.URLClassLoader$1.run(Unknown Source)
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.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2130)
... 7 more这是我的配置文件,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> <!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://10.97.144.117:1433;DatabaseName=MY_FIRST_DB</property>
<property name="connection.username">JDBC</property>
<property name="connection.password">123456</property>
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="cn/lcc/hibernate/model/User.hbm.xml"/> <!-- 注释掉原来的配置文件的映射方式-->
<!-- 这儿我使用Annotatinon的方式进行数据库表的映射 -->
<mapping class="cn.lcc.hibernaet.model.MyTeacher" />
</session-factory></hibernate-configuration>
然后是,实体类,不知道对不对,Teacher.javapackage cn.lcc.hibernate.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "HSCN_USER", catalog = "MY_FIRST_DB")
public class MyTeacher {
private int id;
private String name;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}然后是测试类package cn.lcc.hibernate.test;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;import cn.lcc.hibernate.model.MyTeacher;
import cn.lcc.hibernate.model.User;public class HBTest { @Test
public void testMyTeacher() {
MyTeacher t= new MyTeacher();
//user.setUserId(13);
t.setName("zhangtao");
t.setPassword("123456");
Configuration cfg = new Configuration();
cfg.configure();
org.hibernate.service.ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session s = sf.getCurrentSession();
s.beginTransaction();
s.save(t);
s.getTransaction().commit();
s.close();
sf.close();
System.out.println(t.getId());
System.out.println(t.getName());
System.out.println(t.getName());
System.out.println("The end of processing");
}
}
先上异常吧
七月 24, 2012 10:36:03 上午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
七月 24, 2012 10:36:03 上午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.5.Final}
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: cn/lcc/hibernate/model/User.hbm.xml
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ cn.lcc.hibernaet.model.MyTeacher] declared in Hibernate configuration <mapping/> entry
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2133)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
at cn.lcc.hibernate.test.HBTest.testMyTeacher(HBTest.java:54)
at cn.lcc.hibernate.test.HBTest.main(HBTest.java:107)
Caused by: java.lang.ClassNotFoundException: cn.lcc.hibernaet.model.MyTeacher
at java.net.URLClassLoader$1.run(Unknown Source)
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.internal.util.ReflectHelper.classForName(ReflectHelper.java:192)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2130)
... 7 more这是我的配置文件,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> <!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://10.97.144.117:1433;DatabaseName=MY_FIRST_DB</property>
<property name="connection.username">JDBC</property>
<property name="connection.password">123456</property>
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property> <mapping resource="cn/lcc/hibernate/model/User.hbm.xml"/> <!-- 注释掉原来的配置文件的映射方式-->
<!-- 这儿我使用Annotatinon的方式进行数据库表的映射 -->
<mapping class="cn.lcc.hibernaet.model.MyTeacher" />
</session-factory></hibernate-configuration>
然后是,实体类,不知道对不对,Teacher.javapackage cn.lcc.hibernate.model;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "HSCN_USER", catalog = "MY_FIRST_DB")
public class MyTeacher {
private int id;
private String name;
private String password;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}然后是测试类package cn.lcc.hibernate.test;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.Test;import cn.lcc.hibernate.model.MyTeacher;
import cn.lcc.hibernate.model.User;public class HBTest { @Test
public void testMyTeacher() {
MyTeacher t= new MyTeacher();
//user.setUserId(13);
t.setName("zhangtao");
t.setPassword("123456");
Configuration cfg = new Configuration();
cfg.configure();
org.hibernate.service.ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
Session s = sf.getCurrentSession();
s.beginTransaction();
s.save(t);
s.getTransaction().commit();
s.close();
sf.close();
System.out.println(t.getId());
System.out.println(t.getName());
System.out.println(t.getName());
System.out.println("The end of processing");
}
}
解决方案 »
- 类implements ResultSet出错
- jsp页面如何处理inputstream,求助啊~~愁死我了~~~~
- 在线等待,JNDI绑定问题
- richfaces 的include问题
- 请教xsl,非常急
- java 正则表达式的问题
- 谁做过LINXU+APACHE+JSP或LINUX+TOMCAT+MYSOL+JSP方面的,给我点建议,或谈谈你们的经验或是你们的系统架构之类的。
- 跪求weblogic8方面的中文教程(电子版)
- 求解啊,急急急急急急急急,MYEclipse的一个问题
- ScheduledExecutor定时调度的疑问
- html跳转到servlet时如何带参数传递!!(没有表单)
- jboss7 部署 spring3.0.5+struts2.3.1+hiberbate 出错,求解决!
应该是
<mapping class="cn.lcc.hibernate.model.MyTeacher" />