首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

hibernate4 在用annotation投射是老是出错,为什么呀

2012-08-03 
hibernate4 在用annotation映射是老是出错,为什么呀Exception in thread main org.hibernate.MappingExc

hibernate4 在用annotation映射是老是出错,为什么呀
Exception in thread "main" org.hibernate.MappingException: Unable to load class [ cn.lcc.hibernaet.model.MyTeacher] declared in Hibernate configuration <mapping/> entry

Caused by: java.lang.ClassNotFoundException: cn.lcc.hibernaet.model.MyTeacher

刚刚学习hibernate 真心不懂,我直接贴代码吧
先上异常吧

Java code
七月 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 logVersionINFO: 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 buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml七月 24, 2012 10:36:03 上午 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: cn/lcc/hibernate/model/User.hbm.xmlException 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 code
<?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.java
Java code
package 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;    }    }

然后是测试类
Java code
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");    }}



[解决办法]
<mapping class="cn.lcc.hibernaet.model.MyTeacher" /> 
应该是
<mapping class="cn.lcc.hibernate.model.MyTeacher" /> 

热点排行