Hi,
I am created a simple Hibernate application to insert two values in Oracle table - EVENT, column name are - id number(5) primary key, title varchar2(30). But i am getting following error:
Session factory cannot be initialized
org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1087)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1111)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1098)
at mypack.util.SessionManager.<clinit>(SessionManager.java:11)
at org.apache.jsp.test_jsp._jspService(test_jsp.java:89)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:793)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:702)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:571)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:644)
at java.lang.Thread.run(Thread.java:595)
What i have created is :
myhiber(context directory)
----|---test.jsp
----|---WEB-INF
-----------|---web.xml
-----------|---lib
-----------------|---Hibernate3.jar
-----------------|---classes12.jar
------------------and all .jar files of Hibernate.
-----------|---classes
-----------------|---hibernate.cfg.xml
-----------------|---mypack(package)
-----------------------------|---hibernate
-----------------------------------|---Event.class
-----------------------------------|---Event.hbm.xml
------------------------|--util(package)
-----------------------------|---SessionManager.class
I have checked by running as follows:
http://localhost:8080/myhiber/test.jsp
.jsp file contains two text boxes and submit button.
I will write what these files contains:
Event.java
package mypack.hibernate;
public class Event {
private Long id;
private String title;
public Event(){}
public void setId(Long id){
this.id=id;
}
public void setTitle(String title){
this.title=title;
}
public Long getId(){
return id;
}
public String getTitle(){
return title;
}
}
Event.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="mypack.hibernate">
<class name="Event" table="EVENT">
<id name="id" type="long" column="ID">
<generator class="native" />
</id>
<property name="title">
<column name="TITLE" />
</property>
</class>
</hibernate-mapping>
SessionManager.java
package mypack.util;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class SessionManager{
private static SessionFactory sf;
static{
try{
sf = new Configuration().configure().buildSessionFactory();
}catch(Exception e){
System.err.println("Session factory cannot be initialized");
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sf;
}
}
test.jsp
<%@ page language="java" import="java.util.*, org.hibernate.*, mypack.util.*, mypack.hibernate.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<form method="post">
ID: <input type="text" name="id"><br>
TITLE: <input type="text" name="title"><br>
<input type="submit">
</form>
<%
String id = request.getParameter("id");
String title=request.getParameter("title");
//out.println(id);
//out.println(title);
if((id!=null)&&(title!=null)){
Session ss = SessionManager.getSessionFactory().getCurrentSession();
ss.beginTransaction();
Event ev = new Event();
ev.setId(new Long(id));
ev.setTitle(title);
ss.save(ev);
ss.beginTransaction().commit();
}
%>
</body>
</html>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">scott</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="myeclipse.connection.profile">MyOracle</property>
<property name="connection.password">tiger</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.pool_size">5</property>
<property name="hbm2dll.auto">update</property>
<mapping resource="mypack/hibernate/Event.hbm.xml" />
</session-factory>
</hibernate-configuration>
Table - event
create table event
(
id number(5) constraint ID_Pri primary key,
title varchar2(30)
);
please can any body solve my problem and tell me where i am wrong.
Thanking you,
Rajasekhar.