
2007-8-16 11:29
wps2000
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 name, int 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;
}
}
[/php]
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]
测试的代码:
[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(true, true);
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
java.util.Iterator<Object> it = session.createQuery("from Event").list().iterator();
for(;it.hasNext();)
{
System.out.println(((Event)it.next()).getTitle());
}
}
}[/php]
但是运行的结果是:
11:22:14,203 ERROR XMLHelper:61 - Error parsing XML: XML InputStream(12) [color=red]Attribute "cloumn" must be declared for element type "key".[/color]
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
2007-8-16 12:15
Verdana
<key cloumn="PERSON_ID"/>
是 column 不是 cloumn,拼写错了~
2007-8-16 12:44
wps2000
3x Verdana !
改了下,过了:lol
那个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;
}
}
[/php]
然后我编译过了,放到了WEB_INF/classes/counter/ 下
jsp如下:
[php]
<[email=%@page]%@page[/email] c%>
<[email=%@page]%@page[/email] pageEncoding="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 project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<[email=%@taglib]%@taglib[/email] uri="[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 JSTL, uncomment the taglib directive above.
To test, display the page like this: index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>
</body>
</html>
[/php]
编译错误:
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 秒)
2007-8-17 12:04
Verdana
WEB-INF 不是 WEB_INF 吧 :)
2007-8-17 14:40
wps2000
写了个错别字,的确是放在 WEB-INF 下面的(这个文件夹是开发工具自动建的)
页:
[1]
Powered by Discuz! Archiver 5.5.0
© 2001-2006 Comsenz Inc.