| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1322 人关注过本帖
标题:Hibernate求助!
只看楼主 加入收藏
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:5 
Hibernate求助!
运行出现如下错误 :
0 [main] INFO net.sf.hibernate.cfg.Environment - Hibernate 2.1.8
16 [main] INFO net.sf.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.autocommit=true , hibernate.connection.password=root, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:MySQL://localhost:3306/SAMPLEDB, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
16 [main] INFO net.sf.hibernate.cfg.Environment - using CGLIB reflection optimizer
16 [main] INFO net.sf.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
31 [main] INFO net.sf.hibernate.cfg.Configuration - Mapping resource: mypack/Customer.hbm.xml

这里应该是主要问题,不知道什么原因!
391 [main] ERROR net.sf.hibernate.cfg.Configuration - Could not configure datastore from input stream
org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at org.dom4j.io.SAXReader.read(SAXReader.java:358)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
at mypack.BusinessService.<clinit>(BusinessService.java:18) 

Nested exception:
java.net.MalformedURLException: unknown protocol: htpp
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at java.net.URL.<init>(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.startDTDEntity(Unknown Source)
at org.apache.xerces.impl.XMLDTDScannerImpl.setInputSource(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$DTDDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:339)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
at mypack.BusinessService.<clinit>(BusinessService.java:18)
net.sf.hibernate.MappingException: Error reading resource: mypack/Customer.hbm.xml
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:358)
at mypack.BusinessService.<clinit>(BusinessService.java:18)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:297)
at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
... 1 more
Caused by: org.dom4j.DocumentException: unknown protocol: htpp Nested exception: unknown protocol: htpp
at org.dom4j.io.SAXReader.read(SAXReader.java:358)
at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
... 2 more


BusinessService.java代码如下:
package mypack;
import javax.servlet.*;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.io.*;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.*;
public class BusinessService {
public static SessionFactory sessionFactory;

/** 初始化Hibernate,创建SessionFactory实例 */
static{
try {
//根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例
Configuration config = new Configuration();
config.addClass(Customer.class);  这里为第18行

//创建SessionFactory实例
sessionFactory = config.buildSessionFactory();

} catch(Exception e) {
e.printStackTrace();
}
}

/** 查询所有的Customer对象,然后调用printCustomer()方法打印Customer对象信息 */
public void findAllCustomers(ServletContext context, OutputStream out) throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

try {
List customer = session.find("from Customer as c order by c.name asc");
for(Iterator it = customer.iterator(); it.hasNext();) {
printCustomer(context, out, (Customer)it.next());
}
tx.commit();

} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;

} finally {
session.close();
}
}

/** 持久化一个Customer对象*/
public void saveCustomer(Customer customer) throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

try {
session.save(customer);
tx.commit();

} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;

} finally {
session.close();
}
}

/** 按照OID加载一个Customer对象,然后修改它的属性*/
public void loadAndUpdateCustomer(Long customer_id, String address) throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

try {
Customer c = (Customer)session.load(Customer.class, customer_id);
c.setAddress(address);
tx.commit();

} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;

} finally {
session.close();
}
}

/** 删除所有的Customer对象 */
public void deleteAllCustomers() throws Exception {

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

try {
session.delete("From Customer as c");
tx.commit();

} catch(Exception e) {
if(tx != null) {
tx.rollback();
}
throw e;

} finally {
session.close();
}
}

/** 选择向控制台还是动态网页输出Customer对象的信息 */
private void printCustomer(ServletContext context, OutputStream out, Customer customer) throws Exception {
if(out instanceof ServletOutputStream)
printCustomer(context, (ServletOutputStream)out, customer);
else
printCustomer((PrintStream)out, customer);
}

/** 把Customer对象的信息输出到控制台,如DOS控制台 */
private void printCustomer(PrintStream out, Customer customer) throws Exception {

}

/** 把Customer对象的信息输出到动态网页 */
private void printCustomer(ServletContext context, ServletOutputStream out, Customer customer) throws Exception {
//把Customer对象的image属性包含的二进制图片数据保存到1231_copy.jpg文件中
byte[] buffer = customer.getImage();
String path = context.getRealPath("/");
FileOutputStream fout = new FileOutputStream(path + "1231_copy.jpg");
fout.write(buffer);
fout.close();

out.println("-------以下是" + customer.getName() + "的个人信息-------" + "<br>");
out.println("ID: " + customer.getId() + "<br>");
out.println("口令: " + customer.getPassword() + "<br>");
out.println("E-MAIL: " + customer.getEmail() + "<br>");
out.println("电话: " + customer.getPhone() + "<br>");
out.println("地址: " + customer.getAddress() + "<br>");
String sex = customer.getSex() == 'M' ? "男" : "女";
out.println("性别: " + sex + "<br>");
String marriedStatus = customer.isMarried() ? "已婚" : "末婚";
out.println("婚姻状况: " + marriedStatus + "<br>");
out.println("生日: " + customer.getBirthday() + "<br>");
out.println("注册时间: " + customer.getRegisteredTime() + "<br>");
out.println("自我介绍: " + customer.getDescription().substring(0, 25) + "<br>");
out.println("<img src = '1231_copy.jpg' border = 0><p>"); //显示1231_copy.jpg图片
}

public void test(ServletContext context, OutputStream out) throws Exception {
Customer customer = new Customer();
customer.setName("Tom");
customer.setEmail("tom@yahoo.com");
customer.setPassword("1234");
customer.setPhone(55556666);
customer.setAddress("Shanghai");
customer.setSex('M');
customer.setDescription("I am very honest.");

//设置Customer对象的image属性,它是字节数组,存放photo.gif文件中的二进制数据
//photo.gif文件和BusinessService.class文件位于同一个目录下
InputStream in = this.getClass().getResourceAsStream("1231.jpg");
byte[] buffer = new byte[in.available()];
in.read(buffer);
customer.setImage(buffer);

//设置Customer对象的birthday属性,它是java.sql.Date类型
customer.setBirthday(Date.valueOf("1980-05-06"));

saveCustomer(customer);
findAllCustomers(context, out);

loadAndUpdateCustomer(customer.getId(), "Shanghai");
findAllCustomers(context, out);

deleteAllCustomers();
}

public static void main(String[] args) throws Exception {
//new BusinessService().test(null, System.out);
//sessionFactory.close();
}
}

[此贴子已经被作者于2007-6-11 12:55:21编辑过]

搜索更多相关主题的帖子: hibernate Hibernate connection dialect properties 
2007-06-11 12:53
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
我只在3.0中有过这样的问题,报错的位置和你一样,我当时是改成Configuration config = new Configuration().config;就行了,不知道对你个有帮助!


2007-06-11 15:10
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 
谢谢,我去试试看

开开心心的过&玩每一天!!!!
2007-06-11 18:59
しΟν∈→鱈
Rank: 1
等 级:新手上路
威 望:2
帖 子:369
专家分:0
注 册:2006-10-25
收藏
得分:0 

改了,不过报另一个错。。。。
[CODE]0 [main] INFO net.sf.hibernate.cfg.Environment - Hibernate 2.1.8
63 [main] INFO net.sf.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=root, hibernate.connection.password=root, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:MySQL://localhost:3306/SAMPLEDB, hibernate.connection.driver_class=com.mysql.jdbc.Driver}
110 [main] INFO net.sf.hibernate.cfg.Environment - using CGLIB reflection optimizer
141 [main] INFO net.sf.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
141 [main] INFO net.sf.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
141 [main] INFO net.sf.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
141 [main] WARN net.sf.hibernate.cfg.Configuration - /hibernate.cfg.xml not found
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:886)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:910)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:897)
at mypack.BusinessService.<clinit>(BusinessService.java:17)[/CODE]

另外我用的是2.0的,我看的是孙老师的精通HIBERNATE,她书上讲的是2.0的。。


开开心心的过&玩每一天!!!!
2007-06-11 19:06
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
报错是没有找到hibernate.cfg.xml  再看一下你的路径个有设置好!还有是程序跑到BusinessService类的第17行有错误!能不能把你改了之后的BusinessService发上来啊!

2007-06-12 15:24
hardes
Rank: 1
等 级:新手上路
帖 子:322
专家分:0
注 册:2006-6-3
收藏
得分:0 

Hibernate中Hibernate配置文件有三种形式:
1.使用Eclipse IDE自动生成的Hibernate-cfg.xml文件,目录在工程下的classes目录下
调用方式为:
Configuration config = new Configuration().config();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessoinFactory.openSession();



2.如果配置文件后缀名为.XML格式的文件而名字不是Hibernate-cfg.xml,是自己定义的一个名字.比如:aaa.xml,目录在工程下的classes目录下,调用方式为:
Configuration config = new Configuration().config("aaa.xml");
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();



3.如果配置文件为Hibernate.properties,文件后缀名是*.properties的话,同样是工程下的classes目录下
调用方式为:
Configuration config = new Configuration();//跟上面不一样的是,这后面什么方法都不调用

而是例如这里有一个Customer的javaBean需要被持久化到数据库里,而对应的也有他的customer.hbm.xml配置文件的话
config.addClass(Customer.class);

如果还有其他的javaBean你可以
config.addClass(Customer.class),addClass(A.class).addClass(B.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
反正都要被加载到缓存中,Hibernate在运行的时候会根据你的Customer.class查找对应的Customer.hbm.xml文件进行加载元数据信息,总之一句话你把所有需要持久化的对象都要用addClass()进行加载就行了但是必须要有对应的*.hbm.xml配置文件相对应




运行上面代码时候已经启动Hibernate了.前提条件是你在数据库中已经建好了相应的表了.
如果还没有在数据库中建表,而希望Hibernate帮你自动生成表的话还需要在初始化 Configuration 对象代码下面加上以下代码
new SchemaExport(config).create(true,true);
这样的话Hibernate会自动帮你生成数据库Schema的.

以上是我整理出的所有Hibernate启动配置的时候需要注意的一些情况,如果还有什么不明白的话,可以继续发帖子问我.希望对你有所帮助.



当神已无能为力,就让爷来普度众生吧!
2007-06-14 15:16
快速回复:Hibernate求助!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015449 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved