自己写了一个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();
}
}
希望哪位大虾可以帮帮忙! |