Hi
I use
Hibernate-Version: 3.2.0.cr5
apache-tomcat-5.5.17
In hibernate.cfg.xml I put
Code:
<property name="hibernate.session_factory_name">HibernateSessionFactory</property>
Also made listener with code:
Code:
sessionFactory = new Configuration().configure().buildSessionFactory();
After this I receive SessionFactory by:
Code:
initCtx = new InitialContext();
SessionFactory ses = (SessionFactory) envCtx.lookup("HibernateSessionFactory");
So, where is the problem ? :)
I want to map SessionFactory object in JNDI with name "java:comp/env" for example.
When I change this in properties for "java:comp/env/HibernateSessionFactory" , I can't receive obect by name "java:comp/env/HibernateSessionFactory".
I try to use "ResourceLink" tag in context.xml file, but this is also didn't work (in debug mode i can found this object in 'bindings' map and list() also returned it, but lookup throw
javax.naming.NameNotFoundException: Name HibernateSessionFactory is not bound in this Context)
Did anyone do something like this and it worked?
--------------------------------------------------
My files:
hibernate.cfg.xml
Code:
<?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">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/MunSol</property>
<property name="connection.username">postgres</property>
<property name="connection.password">bleble</property>
<property name="hibernate.session_factory_name">HibernateSessionFactory</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
---------------------------------
web.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Send Servlet</servlet-name>
<servlet-class>a.servlet.SendServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Send Servlet</servlet-name>
<url-pattern>/send</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
a.servlet.HibernateInitializerListener
</listener-class>
</listener>
</web-app>
---------------------------------------------
context.xml
Code:
<Context>
<ResourceLink name="HibernateSessionFactory"
global="HibernateSessionFactory"
type="org.hibernate.SessionFactory"/>
</Context>
-----------------------------------------------
SendServlet.java
public class SendServlet extends HttpServlet{
Code:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("bleble");
Context initCtx;
try {
initCtx = new InitialContext();
NamingContext envCtx = (NamingContext)initCtx.lookup("java:comp/env");
SessionFactory ses = (SessionFactory) envCtx.lookup("HibernateSessionFactory");
} catch (NamingException e) {
e.printStackTrace();
}
}
}