有关hibernate的问题

weiandjuan 2007-12-13 12:07:11
出错问题:
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;
}

}

...全文
113 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiandjuan 2007-12-16
  • 打赏
  • 举报
回复
行,我再看看,谢谢了
tianhaoleng 2007-12-15
  • 打赏
  • 举报
回复
我晕哦!

你看这两个地方!

1、<?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">

2、<?xml version="1.0" encoding="gb2312"?>

他们怎么同时出现在你的配置文件里?
tianhaoleng 2007-12-15
  • 打赏
  • 举报
回复

1、先检查你在configuration后有没有调用configure()方法,因为如果使用xml配置hibernate的话,必须使用configure()方法。

2、检查你的配置文件放的位置对不对。

楼主好运!
weiandjuan 2007-12-13
  • 打赏
  • 举报
回复
谢谢,不过我把中文都删了还是不行啊
zhuxr2003 2007-12-13
  • 打赏
  • 举报
回复
UserInfo.hbm.xml文件不要有中文。
weiandjuan 2007-12-13
  • 打赏
  • 举报
回复
什么意思啊
不会就别捣乱行吗
weiandjuan 2007-12-13
  • 打赏
  • 举报
回复
什么意思啊
不会就别捣乱行吗
mendman 2007-12-13
  • 打赏
  • 举报
回复
<?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">

58,441

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧