I want to use Hibernate3.6.4 to realize many to one mapping with oracle,my code is follows:
hibernate.cfg.xml
Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="dialect">
org.hibernate.dialect.Oracle9iDialect
</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping resource="score/Score.hbm.xml"/>
<mapping resource="student/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Score.hbm.xml
Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="score.Score" table="score">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="native"/>
</id>
<many-to-one cascade="save-update" name="student" class="student.Student" fetch="select">
<column name="sid" not-null="true"/>
</many-to-one>
<property name="score" type="java.lang.Integer">
<column name="score" not-null="true"/>
</property>
<property name="type" type="java.lang.String">
<column name="type" length="20" not-null="true"/>
<property>
</class>
</hibernate-mapping>
Student.hbm.xml
Code:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="student.Student" table="student">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" not-null="true"/>
</property>
<property name="number" type="java.lang.String">
<column name="snumber" length="20" not-null="true"/>
</property>
<property name="classid" type="java.lang.Integer">
<column name="classid" not-null="true"/>
<property>
</class>
</hibernate-mapping>
Score.java
Code:
package score;
import student.Student;
public class Score implements java.io.Serializable{
private Integer id;
private Student student;
private Integer score;
private String type;
public Student getStudent(){
return student;
}
public void setStudent(Student student){
this.student=student;
}
public Integer getId(){
return id;
}
public void setId(Integer id){
this.id=id;
}
public Integer getScore(){
return score;
}
public void setScore(Integer score){
this.score=score;
}
public String getType(){
return type;
}
public void setType(String type){
this.type=type;
}
}
MemberDAO.java
Code:
package member;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.*;
import hibernatesession.HibernateSessionFactory;
public class MemberDAO{
public Session getSession(){
return HibernateSessionFactory.getSession(); //Line 9
}
public void save(Member transientInstance){
try{
Transaction tx=getSession().beginTransaction(); //Line 13
getSession().save(transientInstance);
tx.commit();
}
catch(RuntimeException re){
re.printStackTrace();
}
}
public List findByProperty(String propertyName,Object value){
try{
String queryString="from Member as model where model."+propertyName+"=?";
Query queryObject=getSession().createQuery(queryString);
queryObject.setParameter(0,value);
return queryObject.list();
}
catch(RuntimeException re){
throw re;
}
}
}
JSP page
Code:
<%@ page language="java" import="java.util.*,member.Member,member.MemberDAO" pageEncoding="UTF-8" %>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(username+" --> "+password);
MemberDAO md=new MemberDAO();
Member m=new Member();
m.setId(username);
m.setUsername(username);
m.setPassword(password);
md.save(m);
%>
When I run above JSP,it raise following error,how to correct it?
Code:
Fatal: Error parsing XML (20) : Attribute "name" is required and must be specified for element type "property".
2011-5-16 23:19:56 org.hibernate.util.xml.ErrorLogger error
Fatal: Error parsing XML (21) : The element type "property" must be terminated by the matching end-tag "</property>".
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
at org.hibernate.cfg.Configuration.add(Configuration.java:513)
at org.hibernate.cfg.Configuration.add(Configuration.java:509)
at org.hibernate.cfg.Configuration.add(Configuration.java:716)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:801)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at hibernatesession.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:13)
at member.MemberDAO.getSession(MemberDAO.java:9)
at member.MemberDAO.save(MemberDAO.java:13)
at org.apache.jsp.submit_jsp._jspService(submit_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.dom4j.DocumentException: Error on line 21 of document : The element type "property" must be terminated by the matching end-tag "</property>". Nested exception: The element type "property" must be terminated by the matching end-tag "</property>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:75)
... 35 more
2011-5-16 23:19:56 org.hibernate.cfg.Configuration configure
Info: configuring from resource: /hibernate.cfg.xml
2011-5-16 23:19:56 org.hibernate.cfg.Configuration getConfigurationInputStream
Info: Configuration resource: /hibernate.cfg.xml
2011-5-16 23:19:56 org.hibernate.cfg.Configuration addResource
Info: Reading mappings from resource : score/Score.hbm.xml
2011-5-16 23:19:56 org.hibernate.util.xml.ErrorLogger error
Fatal: Error parsing XML (20) : Attribute "name" is required and must be specified for element type "property".
2011-5-16 23:19:56 org.hibernate.util.xml.ErrorLogger error
Fatal: Error parsing XML (21) : The element type "property" must be terminated by the matching end-tag "</property>".
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
at org.hibernate.cfg.Configuration.add(Configuration.java:513)
at org.hibernate.cfg.Configuration.add(Configuration.java:509)
at org.hibernate.cfg.Configuration.add(Configuration.java:716)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:801)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at hibernatesession.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:33)
at hibernatesession.HibernateSessionFactory.getSession(HibernateSessionFactory.java:24)
at member.MemberDAO.getSession(MemberDAO.java:9)
at member.MemberDAO.save(MemberDAO.java:13)
at org.apache.jsp.submit_jsp._jspService(submit_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.dom4j.DocumentException: Error on line 21 of document : The element type "property" must be terminated by the matching end-tag "</property>". Nested exception: The element type "property" must be terminated by the matching end-tag "</property>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:75)
... 36 more
java.lang.NullPointerException
at member.MemberDAO.save(MemberDAO.java:13)
at org.apache.jsp.submit_jsp._jspService(submit_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
2011-5-16 23:19:56 org.hibernate.cfg.Configuration configure
Info: configuring from resource: /hibernate.cfg.xml
2011-5-16 23:19:56 org.hibernate.cfg.Configuration getConfigurationInputStream
Info: Configuration resource: /hibernate.cfg.xml
2011-5-16 23:19:56 org.hibernate.cfg.Configuration addResource
Info: Reading mappings from resource : score/Score.hbm.xml
2011-5-16 23:19:56 org.hibernate.util.xml.ErrorLogger error
Fatal: Error parsing XML (20) : Attribute "name" is required and must be specified for element type "property".
2011-5-16 23:19:56 org.hibernate.util.xml.ErrorLogger error
Fatal: Error parsing XML (21) : The element type "property" must be terminated by the matching end-tag "</property>".
org.hibernate.InvalidMappingException: Unable to read XML
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:101)
at org.hibernate.cfg.Configuration.add(Configuration.java:513)
at org.hibernate.cfg.Configuration.add(Configuration.java:509)
at org.hibernate.cfg.Configuration.add(Configuration.java:716)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:801)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at hibernatesession.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:33)
at hibernatesession.HibernateSessionFactory.getSession(HibernateSessionFactory.java:24)
at member.MemberDAO.getSession(MemberDAO.java:9)
at member.MemberDAO.findByProperty(MemberDAO.java:24)
at org.apache.jsp.submit_jsp._jspService(submit_jsp.java:71)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:3)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.dom4j.DocumentException: Error on line 21 of document : The element type "property" must be terminated by the matching end-tag "</property>". Nested exception: The element type "property" must be terminated by the matching end-tag "</property>".
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.util.xml.MappingReader.readMappingDocument(MappingReader.java:75)
... 36 more