标题: Hibernate自带文档中的例子无法运行?
wps2000
新手上路
Rank: 1



UID 34
精华 0
积分 0
帖子 73
翻译 0
原创 0
阅读权限 10
注册 2007-5-20
状态 离线
发表于 2007-8-16 11:29  资料  短消息  加为好友 
Hibernate自带文档中的例子无法运行?

我下了个Hibernate 3.2试了试,想看看Hibernate ORM和PHP现在的ORM方案之间的不同:

代码(就是doc文件夹下的那些):
Person.java

PHP代码如下:
/*
 * Person.java
 *
 * Created on 2007年8月15日, 下午10:50
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package event;
import java.util.Collection;
import java.util.Iterator;
/**
 *
 * @author wps20000
 */
public class Person {
    private 
Long id;
    private 
int age;
    private 
String name;
    private 
java.util.Set events = new java.util.HashSet();
    
/** Creates a new instance of Person */
    
public Person() {
    }
    public 
Person(String name)
    {
        
this.name name;
    }
    public 
Person(String nameint age)
    {
        
this.name name;
        
this.age age;
    }
    public 
Long getId()
    {
        return 
id;
    }
    private 
void setId(Long id)
    {
        
this.id id;
    }
    public 
int getAge()
    {
        return 
age;
    }
    public 
void setAge(int age)
    {
        
this.age age;
    }
    public 
String getName()
    {
        return 
name;
    }
    public 
void setName(String name)
    {
        
this.name name;
    }
    public 
void setEvents(java.util.Set events)
    {
        
this.events events;
    }
    public 
java.util.Set getEvents()
    {
        return 
events;
    }
}



Person.hbm.xml

PHP代码如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "[url=http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd]http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd[/url]">
<hibernate-mapping>
 <class name="event.Person" table="PERSONS">
  <id name="id" column="PERSON_ID">
   <generator class="native" />
  </id>
  <property name="age" type="int" />
  <property name="name" type="String"/>
  <set name="events" table="PERSON_EVENT">
   <key cloumn="PERSON_ID"/>
   <many-to-many column="EVENT_ID" class="event.Event" />
  </set>
 </class>
</hibernate-mapping>


测试的代码:

PHP代码如下:
package event;
import org.hibernate.Hibernate;
import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.HibernateException;
/**
 *
 * @author wps2000
 */
public class EventTest {
    
    
/** Creates a new instance of EventTest */
    
public EventTest() {
    }
    public static 
void main(String[] args)
    {
        
Configuration cfg = new Configuration().addClass(Person.class).addClass(Event.class);
        
        new 
SchemaExport(cfg).create(truetrue);
        
        
SessionFactory sessions cfg.buildSessionFactory();
        
Session session sessions.openSession();
        
        
java.util.Iterator<Objectit session.createQuery("from Event").list().iterator();
        for(;
it.hasNext();)
        {
            
System.out.println(((Event)it.next()).getTitle());
        }
    }
    
}



但是运行的结果是:
11:22:14,203 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(12) Attribute "cloumn" must be declared for element type "key".
Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource event/Person.hbm.xml

我就奇怪了,那个set的column的确是作为key的属性声明的,为什么会出错了?

PS:jsp调用bean的时候,bean放在WEB_INF/classes/counter/目录下,但是通过 <jsp:useBean id="cnt" scope="application" class="counter.Counter" />却找不到这个bean





顶部
Verdana
新手上路
Rank: 1



UID 39
精华 1
积分 10
帖子 18
翻译 0
原创 0
阅读权限 10
注册 2007-5-23
状态 离线
发表于 2007-8-16 12:15  资料  主页 短消息  加为好友 
<key cloumn="PERSON_ID"/>


是 column 不是 cloumn,拼写错了~





顶部
wps2000
新手上路
Rank: 1



UID 34
精华 0
积分 0
帖子 73
翻译 0
原创 0
阅读权限 10
注册 2007-5-20
状态 离线
发表于 2007-8-16 12:44  资料  短消息  加为好友 
3x Verdana !
改了下,过了
那个jsp调用bean的问题?

Counter.java

PHP代码如下:
package counter;
/**
 *
 * @author wps2000
 */
public class Counter {
    private 
int count;
    
/** Creates a new instance of Counter */
    
private Counter() {
        
count 0;
    }
    public 
void setCount(int count)
    {
        
this.count count;
    }
    public 
int getCount()
    {
        
count++;
        return 
count;
    }
}



然后我编译过了,放到了WEB_INF/classes/counter/ 下

jsp如下:

PHP代码如下:
<[email=%@page]%@page[/emailc%>
<[
email=%@page]%@page[/emailpageEncoding="UTF-8"%>
<
jsp:useBean id="cnt" scope="application" class="counter.Counter" />
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the projectThe Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library
.
--%>
<%--
<[
email=%@taglib]%@taglib[/emailuri="[url=http://java.sun.com/jsp/jstl/core]http://java.sun.com/jsp/jstl/core[/url]" prefix="c"%> 
--%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "[url=http://www.w3.org/TR/html4/loose.dtd]http://www.w3.org/TR/html4/loose.dtd[/url]"
>
<
html>
    <
head>
        <
meta http-equiv="Content-Type" c>
        <
title>JSP Page</title>
    </
head>
    <
body>
    <
h1>JSP Page</h1>
    <
br/>
    <
a href="./Welcome.do">Struts 欢迎页<br/>您是第 <%=cnt.getCount()%></a>
    
    <%--
    
This example uses JSTLuncomment the taglib directive above.
    
To testdisplay the page like thisindex.jsp?sayHello=true&name=Murphy
    
--%>
    <%--
    <
c:if test="${param.sayHello}">
        <!-- 
Let's welcome the user ${param.name} -->
        Hello ${param.name}!
    </c:if>
    --%>
    
    </body>
</html>



编译错误:
org.apache.jasper.JasperException:  The value for the useBean class attribute counter.Counter is invalid.
C:/Documents and Settings/user1/jsp/build/web/index.jsp(3,0)
C:\Documents and Settings\user1\jsp\nbproject\build-impl.xml:353: Java returned: 1
生成失败(总时间:1 秒)





顶部
Verdana
新手上路
Rank: 1



UID 39
精华 1
积分 10
帖子 18
翻译 0
原创 0
阅读权限 10
注册 2007-5-23
状态 离线
发表于 2007-8-17 12:04  资料  主页 短消息  加为好友 
WEB-INF 不是 WEB_INF 吧





顶部
wps2000
新手上路
Rank: 1



UID 34
精华 0
积分 0
帖子 73
翻译 0
原创 0
阅读权限 10
注册 2007-5-20
状态 离线
发表于 2007-8-17 14:40  资料  短消息  加为好友 
写了个错别字,的确是放在 WEB-INF 下面的(这个文件夹是开发工具自动建的)





顶部
 


PHPEye开源社区


当前时区 GMT+8, 现在时间是 2008-10-12 08:42

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.015117 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver