-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Hibernate.cfg.xml Issues
PostPosted: Fri Jan 09, 2004 7:32 pm 
Newbie

Joined: Tue Jan 06, 2004 12:57 am
Posts: 14
I seem to be having a problem when switching from the properties file to hibernate.cfg.xml. I am getting warnings from Tomcat that indicate that the properties in the xml file are not being read. The hibernate.cfg.xml file is in the right directory (WEB-INF/classes).

I am now using the code from the reference guide in the HibernatePlugin code:

sf = new Configuration().configure().buildSessionFactory();

I created a Datasource in Tomcat and put it into <GlobalNamingResources>. Is that ok?

Jeff.


<Context path="/web" docBase="web">
<Resource name="jdbc/web" scope="Shareable" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/web">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!-- DBCP database connection settings -->
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://localhost:1433;databaseName=test</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>pwd</value>
</parameter>

<!-- DBCP connection pooling options -->
<parameter>
<name>maxWait</name>
<value>3000</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>100</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>10</value>
</parameter>
</ResourceParams>
</Context>


<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="connection.datasource">java:comp/env/jdbc/web</property>
<property name="show_sql">false</property>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

<mapping resource="Contact.hbm.xml"/>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>


WARNING: No dialect set - using GenericDialect: The dialect was not set. Set the
property hibernate.dialect.
Jan 10, 2004 9:35:13 AM net.sf.hibernate.dialect.Dialect <init>
INFO: Using dialect: net.sf.hibernate.dialect.GenericDialect
Jan 10, 2004 9:35:13 AM net.sf.hibernate.cfg.SettingsFactory buildSettings
INFO: Use outer join fetching: false
Jan 10, 2004 9:35:13 AM net.sf.hibernate.connection.UserSuppliedConnectionProvid
er configure
WARNING: No connection properties specified - the user must supply JDBC connecti
ons
Jan 10, 2004 9:35:13 AM net.sf.hibernate.transaction.TransactionManagerLookupFac
tory getTransactionManagerLookup
INFO: No TransactionManagerLookup configured (in JTA environment, use of process




import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.MappingException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

/**
* Initialize the Hibernate SessionFactory for this project
* as an application scope object.
*
* @author Ted Husted
* @version $Revision: 1.3 $ $Date: 2003/03/14 21:59:41 $
*/
public class HibernatePlugIn implements PlugIn {

/**
* A field to store the reference to our SessionFactory.
* Can close and dispose if not null.
*/
private SessionFactory sf;

/**
* A public identifer for the persistence session,
* kept in servlet session ("client") scope
* ["HIBERNATE_SESSION"].
*/
public static String SESSION = "HIBERNATE_SESSION";

/**
* A public identifer for the session factory,
* kept in application ("global") scope
* ["HIBERNATE_SESSION_FACTORY"].
*/
public static String SESSION_FACTORY = "HIBERNATE_SESSION_FACTORY";

/**
* Fetch the SessionFactory from application scope.
* @param request The requeste we are servicing
* @return The SessionFactory for this application session
* @throws HibernateException
*/
public static SessionFactory sessionFactory(HttpServletRequest request)
throws HibernateException {
Object sf = request.getSession().getServletContext().getAttribute(SESSION_FACTORY);
if (null == sf) {
throw new HibernateException(SESSION_FACTORY);
}
return (SessionFactory) sf;
}


/**
* Open a new session with the application-scope SessionFactory.
* Session is not retained, only returned.
*
* @param request The requeset we are servicing
* @return An open session
* @throws HibernateException
*/
public static Session open(HttpServletRequest request) throws HibernateException {
return sessionFactory(request).openSession();
}

/**
* Open a new Session and cache it in the HttpSession or
* fetch the existing Session.
*
* @param request The requeset we are servicing
* @return An open session
* @throws net.sf.hibernate.HibernateException if session cannot be instantiated
*/
public static Session reconnect(HttpServletRequest request)
throws HibernateException {

Session s = (Session) request.getSession(true).getAttribute(SESSION);
if (null != s) {
s.reconnect();
} else {
s = open(request);
request.getSession().setAttribute(SESSION, s);
}
return s;
}

/**
* Expire the Session, to ensure fresh data or to switch approaches.
*
* @param request The requeset we are servicing
* @return An open session
* @throws net.sf.hibernate.HibernateException if session cannot be instantiated
*/
public static void expire(HttpServletRequest request)
throws HibernateException {

HttpSession httpSession = request.getSession();
if (null!=httpSession) {
Session s = (Session) httpSession.getAttribute(SESSION);
if (null != s) {
s.close();
httpSession.removeAttribute(SESSION);
}
}
}

/**
* The classes with mappings to add to the Configuration are enumerated here.
* There should be a "${class}.hbm.xml" mapping file for each class
* stored with each compiled class file.
* <p>
* To complete the Hibernate setup, there must be a valid "hiberate.properties"
* file under the "classes" folder (root of the classpath),
* which specifies the details of the database hookup.
* <p>
* The mapping documents and properties file is all that Hibernate requires.
* <p>
* A JDBC Driver is not included in this distribution and *must* be
* available on your server's or container's classpath
* (e.g., the Tomcat common/lib directory).
*
* @return A Configuration object
* @throws net.sf.hibernate.MappingException if any the mapping documents can be rendered.
*/
private static final Configuration createConfiguration()
throws MappingException {
return new Configuration()
.addClass(common.User.class)
.addClass(common.Contact.class);
}

public void init(ActionServlet servlet, ModuleConfig config)
throws ServletException {

SessionFactory exists = (SessionFactory)
servlet.getServletContext().getAttribute(SESSION_FACTORY);
if (null != exists) return; // already got one

try {
//sf = createConfiguration().buildSessionFactory();
sf = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
servlet.log(e.toString());
throw new ServletException(e);
}

servlet.getServletContext().setAttribute(SESSION_FACTORY, sf);
}

public void destroy() {
if (null != sf) {
try {
sf.close();
} catch (HibernateException e) {
// too late now
}
}
sf = null;
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 10, 2004 10:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Try using cfg.configure(this.getClass().getClassLoader().getResource("/hibernate.cfg.xml")) or something like this


Top
 Profile  
 
 Post subject: Hibernate.cfg.xml Issues
PostPosted: Sat Jan 10, 2004 7:36 pm 
Newbie

Joined: Tue Jan 06, 2004 12:57 am
Posts: 14
Nope that didnt work, I am still getting the same warnings.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 8:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Give a try to the latest version of tomcat

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 8:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Crappy tomcat classloaders grmbl ...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 2:07 pm 
Newbie

Joined: Tue Feb 10, 2004 2:11 pm
Posts: 15
I've tried this:
<plug-in className="foo:/hibernate/SessionFactory" />

and I tried this:
<plug-in className="foo.hibernate.SessionFactory" />

As expected, it is not working.

I am getting a "Servlet action is not available" ... I know this is happening because 'className' I specified up there is not correct

I've been on this for days now. Surely there is someone out there who knows how this thing is supposed to work .... What did every one else do in this case?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 2:10 pm 
Newbie

Joined: Tue Feb 10, 2004 2:11 pm
Posts: 15
Please disregard this post.
IT was meant for something else. My apologies..


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2004 6:51 pm 
Newbie

Joined: Tue Feb 10, 2004 12:03 am
Posts: 16
use this one


Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");



this worked for me[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 29, 2004 12:42 am 
Newbie

Joined: Sat Feb 28, 2004 4:42 pm
Posts: 7
Location: Oakland, California USA
Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");


I had the problem:

java.lang.UnsupportedOperationException: The user must supply a JDBC connection

and adding the above worked for me, too. THANKS!!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.