初学hibernate错误请教..

来源:互联网 发布:杨振宁 霍金 知乎 编辑:程序博客网 时间:2024/06/05 20:00

bean文件如下:

--------------------------------------------------------------------------------------------------------

package com.bjsxt.hibernate.model;

public class Student {
    private String id;
    private String name;
    private int age;
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 
}

 

 

 

Student.hbm.xml文件如下:

-----------------------------------------------------------------------------------------------------
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     

<hibernate-mapping>
 <class name="com.bjsxt.hibernate.model.Student">
  <id name="id" >
      <generator class="uuid"></generator>
  </id>
  <property name="name" />
  <property name="age" />
    </class>
</hibernate-mapping>

 

 

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">123456</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.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.model.Teacher"/>-->
    </session-factory>

</hibernate-configuration>

 

 

单元测试类如下:

----------------------------------------------------------------------------------------------------------------

package com.bjsxt.hibernate.model;
import com.bjsxt.hibernate.model.*;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class HibernateIDTest {
 private static SessionFactory sf = null;
 @BeforeClass
 public static void BeforClass(){
  try {
   Configuration cfg = new Configuration().configure();
   SessionFactory sf = cfg.configure().buildSessionFactory();
  } catch (HibernateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 @AfterClass
 public static void AfterClass(){
  sf.close();
 }
 @Test
 public void testStudent(){
  Student s = new Student();
  //s.setId(1);
  s.setName("zhangsan");
  s.setAge(8);
  
  Session session = sf.openSession();
  session.beginTransaction();
  session.save(s);
  Transaction tr = session.getTransaction();
  tr.commit();
  session.close();
  
 }
}

 

 

 错误信息如下:

------------------------------------------------------------------------------------------------------------------------
org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/bjsxt/hibernate/model/Student.hbm.xml
 at org.hibernate.cfg.Configuration.addResource(Configuration.java:616)
 at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1635)
 at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
 at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
 at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
 at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
 at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
 at com.bjsxt.hibernate.model.HibernateIDTest.BeforClass(HibernateIDTest.java:20)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
 at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.DuplicateMappingException: Duplicate class/entity mapping com.bjsxt.hibernate.model.Student
 at org.hibernate.cfg.Mappings.addClass(Mappings.java:141)
 at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:167)
 at org.hibernate.cfg.Configuration.add(Configuration.java:716)
 at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:551)
 at org.hibernate.cfg.Configuration.addResource(Configuration.java:613)
 ... 23 more

 

在下初学hibernate,用的是尚学堂的视频。遇到这个这个问题很是郁闷。怎么也找不到错误的地儿。在网上也查了相关的错误原因。但是无法独自解决。希望各位前辈给予指点。谢谢!!

原创粉丝点击