A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 史小兵 中级黑马   /  2012-12-7 19:53  /  2207 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

自己写了一个hibernate小程序但是老是报 org.xml.sax.SAXParseException: Attribute "xmlns" must be declared for element type "hibernate-configuration".异常!
找了好多地方就是找不到原因,希望哪个高手可以帮忙指点一下!
这是Student:package com.bjsxt.hibernate.model;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="_Teacher")
public class Student {
        private String name;
        private  int id;
        private int age;
        private Date birthday;
       
        private String title;
                @Transient
        public String getTitle() {
                return title;
        }
        public void setTitle(String title) {
                this.title = title;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        @Id
        public int getId() {
                return id;
        }
        public void setId(int id) {
                this.id = id;
        }
        public int getAge() {
                return age;
        }
                @Temporal(TemporalType.DATE)
        public Date getBirthday() {
                return birthday;
        }
        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
                public void setAge(int age) {
                this.age = age;
        }

}


下边是hibernate.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
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <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">5556643</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.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout 显示出来执行时的sql语句-->
    <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.Student" />
  </session-factory>
</hibernate-configuration>


下边是Student.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-mapping package="com.bjsxt.hibernate.model">
<class name="Student" >
        <id name="id" >
        <generator class="native"></generator>
        </id>
        <property name="name"></property>
        <property name="age"></property>
</class>
</hibernate-mapping>

下边是测试的主程序

package com.bjsxt.hibernate.model;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class StudentTest {
        public static void main(String args[]){
        Student s=new Student();
        s.setId(1);
        s.setName("s1");
        s.setAge(1);
       
       
        Configuration cfg=new Configuration();
        ServiceRegistry serviceRegistry =
        new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();  
       
        SessionFactory sf=cfg.configure().buildSessionFactory(serviceRegistry);
        Session ss=sf.openSession();
        ss.beginTransaction();
        ss.save(s);
        ss.getTransaction().commit();
        ss.close();
        sf.close();
        }
}
希望哪位大虾可以帮帮忙!

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

5 个回复

倒序浏览
  1. package com.bjsxt.hibernate.model;
  2. import java.util.Date;

  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5. import javax.persistence.Table;
  6. import javax.persistence.Temporal;
  7. import javax.persistence.TemporalType;
  8. import javax.persistence.Transient;


  9. @Entity
  10. @Table(name="_Teacher")
  11. public class Student {
  12.         private String name;
  13.         private  int id;
  14.         private int age;
  15.         private Date birthday;
  16.         
  17.         private String title;
  18.                 @Transient
  19.         public String getTitle() {
  20.                 return title;
  21.         }
  22.         public void setTitle(String title) {
  23.                 this.title = title;
  24.         }
  25.         public String getName() {
  26.                 return name;
  27.         }
  28.         public void setName(String name) {
  29.                 this.name = name;
  30.         }
  31.         @Id
  32.         public int getId() {
  33.                 return id;
  34.         }
  35.         public void setId(int id) {
  36.                 this.id = id;
  37.         }
  38.         public int getAge() {
  39.                 return age;
  40.         }
  41.                 @Temporal(TemporalType.DATE)
  42.         public Date getBirthday() {
  43.                 return birthday;
  44.         }
  45.         public void setBirthday(Date birthday) {
  46.                 this.birthday = birthday;
  47.         }
  48.                 public void setAge(int age) {
  49.                 this.age = age;
  50.         }

  51. }
复制代码
hibernate.cfg.xml 配置
  1. <?xml version='1.0' encoding='utf-8'?>
  2. <!DOCTYPE hibernate-configuration PUBLIC
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  4.         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

  5. <hibernate-configuration>

  6.     <session-factory>

  7.         <!-- Database connection settings -->
  8.         <property name="connection.driver_class"
  9. >com.mysql.jdbc.Driver</property>
  10.         <property name="connection.url"
  11. >jdbc:mysql://localhost/student?useUnicode=true&amp;characterEncoding=utf-8</property>
  12.         <property name="connection.username"
  13. >root</property>
  14.         <property name="connection.password"
  15. >root</property>

  16.         <!-- JDBC connection pool (use the built-in) -->
  17.         <!--  <property name="connection.pool_size"
  18. >1</property>
  19. -->
  20.         <!-- SQL dialect -->
  21.         <property name="dialect"
  22. >org.hibernate.dialect.MySQLDialect</property>

  23.         <!-- Enable Hibernate's automatic session context management -->
  24.       <!--   <property name="current_session_context_class"
  25. >thread</property>-->

  26.         <!-- Disable the second-level cache  -->
  27.         <property name="cache.provider_class"
  28. >org.hibernate.cache.NoCacheProvider</property>

  29.         <!-- Echo all executed SQL to stdout -->
  30.         <property name="show_sql"
  31. >true</property>

  32.         <!-- Drop and re-create the database schema on startup -->
  33.         <property name="hbm2ddl.auto"
  34. >update</property>

  35.         <!--  <mapping resource="text/Hibernate/mode/Student.hbm.xml"/>-->
  36.                 <!-- 学生信息表 -->
  37.                 <mapping class="com.bjsxt.hibernate.model.Student"/>
  38.     </session-factory>

  39. </hibernate-configuration>
复制代码
测试类
  1. package com.bjsxt.hibernate.model;

  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.AnnotationConfiguration;
  5. import org.hibernate.cfg.Configuration;
  6. import org.hibernate.service.ServiceRegistry;
  7. import org.hibernate.service.ServiceRegistryBuilder;

  8. public class StudentTest {
  9.         public static void main(String args[]){
  10.         Student s=new Student();
  11.         s.setId(1);
  12.         s.setName("s1");
  13.         s.setAge(1);
  14.         
  15.         Configuration cfg=new AnnotationConfiguration();
  16.                 SessionFactory sf=cfg.configure().buildSessionFactory();
  17.                 Session session=sf.openSession();
  18.                 session.beginTransaction();  
  19.                 session.save(s);
  20.                 session.getTransaction().commit();
  21.                 session.close();
  22.         sf.close();
  23.         }
  24. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
记得把数据库的密码改下。这种是直接注解的。看马士兵的视频后面会讲到的
回复 使用道具 举报
卍解 发表于 2012-12-7 20:46
hibernate.cfg.xml 配置测试类

注解不是很懂
回复 使用道具 举报
卍解 发表于 2012-12-7 20:46
hibernate.cfg.xml 配置测试类

不对啊,我改过之后还是会报Connection cannot be null when 'hibernate.dialect' not set异常!
回复 使用道具 举报
方言设置不能为空,连接失败
<!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySqlDialect</property>
把我的配置好好的看下。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马