这是错误代码:
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 test.hibernate.InsertProduct.main(InsertProduct.java:16)
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1366)
... 3 more
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 test.hibernate.InsertProduct.main(InsertProduct.java:16)
Caused by: org.dom4j.DocumentException: hibernate.sourceforge.net Nested exception: hibernate.sourceforge.net
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1366)
... 3 more
解决方案 »
- 用js实现图片链接提示的问题
- 帮我看看,怎么弄成竖直对齐button
- hibernate插入数据,表不存在异常
- rmi输出helloworld问题,急,调了很久还是有问题,望高手帮帮!在线等
- Date类型插入数据库后,丢失时分秒
- 谁在weblogic9.0上成功的配置JMS 在线等
- 关于hibernate的问题
- 特殊字符再java中如何处理,例如输出System.out.println("~");
- 围绕Jbiulder的问题,请详细解答,节贴给分!
- websphere 中如何在命令行配置JMS和创建ConnectionFactory及Queue
- 关于tiles的问题
- 倾我所有:紧急求救SOAP 消息拦截问题,MessageContext的问题
log4j:WARN Please initialize the log4j system properly.这个是你没配置log4j而引起的警告,可以不管。Exception是由于你的hibernate.cfg.xml文件的内容不正确,无法解析造成的。到网上找个正确的例子比对一下吧
package test.hibernate;
import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.*;import org.hibernate.classic.Session;public class InsertProduct {
public static void main (String args[]){
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Product p = new Product();
p.setAotoid("1");
p.setUsername("zhang");
p.setPrice(new Integer(10));
p.setAmount(new Integer(10));
System.out.println(p+"_______________1");
try{
session.save(p);
tx.commit();
session.close();
System.out.println(p+"_______________2");
}catch(Exception ex){
ex.printStackTrace();
} }}
package test.hibernate;
import java.io.Serializable;public class Product implements Serializable{
private static final long serialVersionUID = 3858043967100350732L;
private String aotoid;
private String username;
private Integer price;
private Integer amount;
public Integer getAmount(){
return amount;
}
public void setAmount(Integer amount){
this.amount = amount;
}
public String getAotoid(){
return aotoid;
}
public void setAotoid(String aotoid){
this.aotoid = aotoid;
}
public String getUsername(){
return username;
}
public void setUsername(String username){
this.username = username;
}
public Integer getPrice(){
return price;
}
public void setPrice(Integer price){
this.price = price;
}
}
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.1//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.1.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="connection.username">cd</property>
<property name="connection.url">
jdbc:oracle:thin:@10.102.72.139:1521:cd
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.password">123</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource = "hibernate.cfg.xml"/>
<mapping resource = "test/hibernate/Product.hbm.xml"/></session-factory></hibernate-configuration>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.1.dtd" >
<hibernate-mapping package="test.hibernate">
<class table="test_products" name="Product">
<property name="aotoid" column="aotoid" type="string" length="50"/>
<property name="username" column="username" type="string" length="50"/>
<property name="price" column="price" type="int"/>
<property name="amount" column="amount" type="int"/>
</class></hibernate-mapping>
<?xml version='1.0' encoding='GBK'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://192.168.158.31:3306/testmxb?useUnicode=true&characterEncoding=GBK</property>
<property name="connection.username">test</property>
<property name="connection.password">1234</property> <!-- mapping files -->
<!--<mapping resource="testhibernate/Employer.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
package test.hibernate;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory { /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION; private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}}