Java 1.6+
Eclipse 3.4.2
TOMCAT 6.0
JBoss Hibernate Tools 3
MySQL 5.0
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 name="sqlServer">
<property name="hibernate.session_factory_name">SessionFactory</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password">01201972</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/super</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
</session-factory>
</hibernate-configuration>
User.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2009-6-1 6:16:18 by Hibernate Tools 3.2.4.GA -->
<hibernate-mapping>
<class name="com.User" table="user" catalog="super">
<id name="id" type="java.lang.Integer">
<column name="Id" />
<generator class="identity" />
</id>
<property name="username" type="string">
<column name="username" />
</property>
<property name="password" type="string">
<column name="password" />
</property>
<property name="content" type="string">
<column name="content" length="65535" />
</property>
</class>
</hibernate-mapping>
User.javaCode:
package com;
// Generated 2009-6-1 6:16:17 by Hibernate Tools 3.2.4.GA
/**
* User generated by hbm2java
*/
public class User implements java.io.Serializable {
private Integer id;
private String username;
private String password;
private String content;
public User() {
}
public User(String username, String password, String content) {
this.username = username;
this.password = password;
this.content = content;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
}
UserHome.java:Code:
package com;
// Generated 2009-6-1 6:16:18 by Hibernate Tools 3.2.4.GA
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import static org.hibernate.criterion.Example.create;
/**
* Home object for domain model class User.
* @see com.User
* @author Hibernate Tools
*/
public class UserHome {
private static final Log log = LogFactory.getLog(UserHome.class);
private final SessionFactory sessionFactory = getSessionFactory();
protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
public void persist(User transientInstance) {
log.debug("persisting User instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
public void attachDirty(User instance) {
log.debug("attaching dirty User instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(User instance) {
log.debug("attaching clean User instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void delete(User persistentInstance) {
log.debug("deleting User instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public User merge(User detachedInstance) {
log.debug("merging User instance");
try {
User result = (User) sessionFactory.getCurrentSession().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public User findById(java.lang.Integer id) {
log.debug("getting User instance with id: " + id);
try {
User instance = (User) sessionFactory.getCurrentSession().get(
"com.User", id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List<User> findByExample(User instance) {
log.debug("finding User instance by example");
try {
List<User> results = (List<User>) sessionFactory
.getCurrentSession().createCriteria("com.User").add(
create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
}
hibernateTest.jspCode:
<%@page pageEncoding="GBK" contentType="text/html; charset=GBK" %>
<%@page import="com.User,com.UserHome" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title>Hibernate Test</title>
</head>
<body>
<%
User myuser = new User();
myuser.setId(5);
myuser.setUsername("samon");
myuser.setPassword("01201972");
myuser.setContent("Hello,Hibernate!");
out.print(myuser.getId()+"<br/>"+myuser.getUsername()+"<br/>"+myuser.getPassword()+"<br/>"+myuser.getContent());
UserHome myUserHome = new UserHome();
%>
</body>
</html>
Run as Tomcat Server 6.0 Browser IE
Code:
out.print(myuser.getId()+"<br/>"+myuser.getUsername()+"<br/>"+myuser.getPassword()+"<br/>"+myuser.getContent());
is OK!
Result:Code:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
com.UserHome.getSessionFactory(UserHome.java:30)
com.UserHome.<init>(UserHome.java:22)
org.apache.jsp.hibernateTest_jsp._jspService(hibernateTest_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
What's problem?and i copy all the JBoss Hibernate Tools 3 JAR into TOMCAT Lib & also this error message!
who can help me! thanks!