有关hibernate的问题
出错问题:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1376)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1310)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1296)
at hibernate.ch01.HibernateTest.main(HibernateTest.java:11)
Caused by: org.dom4j.DocumentException: Error on line 7 of document : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1366)
... 3 more
UserInfo.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>
<!-- created by afuer-->
<!-- 类与表之间的关联-->
<class name="hibernate.ch01.UserInfo" table="login">
<id name="id" type="java.lang.Integer">
<column name="id" />
<!-- 指明主键的自增长类型-->
<generator class="identity" />
</id>
<!-- 以下为普通字段的关联-->
<property name="userName" type="java.lang.String">
<column name="name" length="100" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="100" />
</property>
</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">
<!-- Generated by MyEclipse Hibernate Tools.-->
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:nysql://localhost/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123</property>
<property name="hibernate.connection.pool.size">20</property>
<property name="hibernate.show.sql">true</property>
<property name="jdbc.fetch_size">50</property>
<property name="jdbc.batch_size">25</property>
<property name="jdbc.user_scrollable_resultset">false</property>
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">gb2312</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Mapping files-->
<mapping resource="hibernate/ch01/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernatTest.java文件内容
package hibernate.ch01;
import hibernate.ch01.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args) {
SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session=sessions.openSession();
Transaction tx=null;
try{
tx=session.beginTransaction();
UserInfo u=new UserInfo();
u.setUserName("zhaoweiwei");
u.setPassword("123");
System.out.println("开始插入数据到数据库.....");
session.save(u);//保存数据到数据库
UserInfo u1=(UserInfo)session.load(UserInfo.class,new Integer(1));
System.out.println("从数据库中提取的信息为"+u1.getUserName());
tx.commit();
tx=null;
System.out.println("数据库操作成功!");
}
catch(HibernateException e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}
finally{
session.close();
}
}
}
UserInfo.java文件的内容
package hibernate.ch01;
public class UserInfo {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}