3.servlet
package dt2mis.servlet;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import javax.rmi.*;
import javax.naming.*;
import dt2misejb.*;
import com.hibernate.C_employee;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;public class Servlet_hibernate_test extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=Big5"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try{
Context ctx=new InitialContext();
Object obj=ctx.lookup("HibernateSession");
HibernateSessionHome home=(HibernateSessionHome)PortableRemoteObject.narrow(obj,HibernateSessionHome.class);
HibernateSession remote=home.create();
C_employee c_employee = new C_employee();
c_employee.setDept_id("001");
c_employee.setEmp_id("P0047009");
c_employee.setEmp_name("tommy");
c_employee.setE_mail("tommy p. wang");
c_employee.setEmp_sex("1");
c_employee.setPass_word("11111111");
c_employee.setRole("009");
c_employee.setTelephone("74814");
Session session1 = remote.currentSession();
System.out.println("111111");
Transaction tx= session1.beginTransaction();
System.out.println("2222222");
session1.save(c_employee);
tx.commit();
System.out.println("555555555");
remote.closeSession();
System.out.println("66666666");
}catch(Exception ex){ex.printStackTrace();} out.println("<html>");
out.println("<head><title>Servlet_hibernate_test</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a POST. This is the reply.</p>");
out.println("</body></html>");
} //Clean up resources
public void destroy() {
}
}
===================
4.sessionbean:
package dt2misejb;import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import net.sf.hibernate.*;
import javax.naming.*;
import javax.ejb.*;
public class HibernateSessionBean
implements SessionBean {
SessionContext sessionContext;
public static final ThreadLocal tlsession = new ThreadLocal();
private static SessionFactory sf; public void ejbCreate() throws CreateException {
try {
Context ctx = new InitialContext();
sf = (SessionFactory) ctx.lookup("hibernate/session_factory");
}
catch (Exception e) {
e.printStackTrace();
}
} public void ejbRemove() {
} public void ejbActivate() {
} public void ejbPassivate() {
} public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} public Session currentSession() throws HibernateException {
Session s = (Session) tlsession.get();
try{
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sf.openSession();
tlsession.set(s);
}
}catch(HibernateException he){he.printStackTrace();}
return s;
} public Session closeSession() throws HibernateException {
Session s = (Session) tlsession.get();
try{
tlsession.set(null);
if (s != null) {
s.close();
}
}catch(HibernateException he){he.printStackTrace();}
return null;
}
}
=============================
运行servlet后出现如下错误:
java.lang.IllegalStateException: Cannot serialize a Session while connected
at net.sf.hibernate.impl.SessionImpl.writeObject(SessionImpl.java:512)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:79
5)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:12
94)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav
a:1245)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at weblogic.rmi.internal.CBVOutputStream.writeObject(CBVOutputStream.jav
a:78)
at weblogic.rmi.internal.ServerRequest.unmarshalReturn(ServerRequest.jav
a:94)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:285)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:244)
at dt2misejb.HibernateSession_n1rmjc_EOImpl_810_WLStub.currentSession(Un
known Source)
=============
如果不用ejb,都写到servlet里的话则出现插入值过大的错误,请问大家怎么解决这个问题啊?谢谢了!!!!!!!!!
package dt2mis.servlet;import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import javax.rmi.*;
import javax.naming.*;
import dt2misejb.*;
import com.hibernate.C_employee;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;public class Servlet_hibernate_test extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=Big5"; //Initialize global variables
public void init() throws ServletException {
} //Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
try{
Context ctx=new InitialContext();
Object obj=ctx.lookup("HibernateSession");
HibernateSessionHome home=(HibernateSessionHome)PortableRemoteObject.narrow(obj,HibernateSessionHome.class);
HibernateSession remote=home.create();
C_employee c_employee = new C_employee();
c_employee.setDept_id("001");
c_employee.setEmp_id("P0047009");
c_employee.setEmp_name("tommy");
c_employee.setE_mail("tommy p. wang");
c_employee.setEmp_sex("1");
c_employee.setPass_word("11111111");
c_employee.setRole("009");
c_employee.setTelephone("74814");
Session session1 = remote.currentSession();
System.out.println("111111");
Transaction tx= session1.beginTransaction();
System.out.println("2222222");
session1.save(c_employee);
tx.commit();
System.out.println("555555555");
remote.closeSession();
System.out.println("66666666");
}catch(Exception ex){ex.printStackTrace();} out.println("<html>");
out.println("<head><title>Servlet_hibernate_test</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a POST. This is the reply.</p>");
out.println("</body></html>");
} //Clean up resources
public void destroy() {
}
}
===================
4.sessionbean:
package dt2misejb;import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.CreateException;
import net.sf.hibernate.*;
import javax.naming.*;
import javax.ejb.*;
public class HibernateSessionBean
implements SessionBean {
SessionContext sessionContext;
public static final ThreadLocal tlsession = new ThreadLocal();
private static SessionFactory sf; public void ejbCreate() throws CreateException {
try {
Context ctx = new InitialContext();
sf = (SessionFactory) ctx.lookup("hibernate/session_factory");
}
catch (Exception e) {
e.printStackTrace();
}
} public void ejbRemove() {
} public void ejbActivate() {
} public void ejbPassivate() {
} public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
} public Session currentSession() throws HibernateException {
Session s = (Session) tlsession.get();
try{
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sf.openSession();
tlsession.set(s);
}
}catch(HibernateException he){he.printStackTrace();}
return s;
} public Session closeSession() throws HibernateException {
Session s = (Session) tlsession.get();
try{
tlsession.set(null);
if (s != null) {
s.close();
}
}catch(HibernateException he){he.printStackTrace();}
return null;
}
}
=============================
运行servlet后出现如下错误:
java.lang.IllegalStateException: Cannot serialize a Session while connected
at net.sf.hibernate.impl.SessionImpl.writeObject(SessionImpl.java:512)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:79
5)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:12
94)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.jav
a:1245)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at weblogic.rmi.internal.CBVOutputStream.writeObject(CBVOutputStream.jav
a:78)
at weblogic.rmi.internal.ServerRequest.unmarshalReturn(ServerRequest.jav
a:94)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:285)
at weblogic.rmi.cluster.ReplicaAwareRemoteRef.invoke(ReplicaAwareRemoteR
ef.java:244)
at dt2misejb.HibernateSession_n1rmjc_EOImpl_810_WLStub.currentSession(Un
known Source)
=============
如果不用ejb,都写到servlet里的话则出现插入值过大的错误,请问大家怎么解决这个问题啊?谢谢了!!!!!!!!!
解决方案 »
- hibernate3.0执行 select now() from dual语句报错!
- 各位大狭帮我看看这个问题,谢谢
- 求webservice 资料 或者书 谢谢了
- 哪位兄弟姐妹能提供一下"java数据库应用程序编程指南"的源代码
- 一个项目快结束了,却发现汉字大问题......
- 高手请进(20)
- shark怎样读取xpdl文件
- 一个hibernate很奇怪的问题????
- ~~~~!◎◎#请教各位大哥:怎样存储邮件内容content为Object类型到SQL Server 2000 ???
- Lliux中文字转图片变成框框
- 请教tomcat的配置
- 100分求解:linux9下weblogic8.1.2console左侧树形菜单无法显示!!
CREATE TABLE C_EMPLOYEE
(
EMP_ID VARCHAR2(8) NOT NULL,
EMP_NAME VARCHAR2(20) NOT NULL,
EMP_SEX CHAR(1),
DEPT_ID CHAR(3) NOT NULL,
HEAD_SHIP VARCHAR2(10),
TELEPHONE VARCHAR2(20),
E_MAIL VARCHAR2(30),
PASS_WORD VARCHAR2(15),
ROLE CHAR(5)
)
如果不用ejb的话,应该要注意如下事实:
你的generator class="uuid.hex" 产生出来的id的长度是32位,
但是你的emp_id的长度为8位,这个是不是原因?
EMP_ID VARCHAR2(8) NOT NULL,
EMP_NAME VARCHAR2(20) NOT NULL,
EMP_SEX CHAR(1),
DEPT_ID CHAR(3) NOT NULL,
HEAD_SHIP VARCHAR2(10),
TELEPHONE VARCHAR2(20),
E_MAIL VARCHAR2(30),
PASS_WORD VARCHAR2(15),
ROLE CHAR(5)都改大点ejb中初始化和servlet中不同。ejb可以用hibernate.