Posted on 2006-07-19 18:39
Earth 阅读(350)
评论(0) 编辑 收藏 所属分类:
Life
去掉与Hibernate有关的HibernateUtil,HibernateFilter, 以及所有的Hibernate配置和映射文件~_~
IUserDAO, IMessageDAO等DAO接口基本不变,只需要在类前面加上@Remote~_~
DAO接口的实现基本不变,只需要在类前面加上@Stateless,并定义一个叫javax.persistence.EntityManager的万能类(和Spring中的
getHibernateTemplate()差不多但是比它还要简单~_~)
DAO接口的EJB3实现像下面这样
@Stateless
public
class
MessageDAO
implements
IMessageDAO {
@PersistenceContext(type
=
PersistenceContextType.TRANSACTION, unitName
=
"
message
"
)
private
EntityManager manager;
public
void
saveMessage(Message m) {
manager.persist(m);
}
public
void
updateMessage(Message m) {
if
(manager.find(Message.
class
, m.getId())
==
null
) {
throw
new
RuntimeException(
"
Unable to update
"
+
m
+
"
because there is no prior version
"
);
}
manager.merge(m);
}
public
List getMessages() {
String sql
=
"
select m from Message m
"
;
return
manager.createQuery(sql).getResultList();
}
public
void
deleteMessage(String id, String userId) {
Message msg
=
getMessage(id);
if
(msg
==
null
) {
throw
new
MessageDAOException(
"
Can't find the message u wanna del!
"
);
}
if
(
!
msg.getUser().getId().equals(userId)) {
throw
new
MessageDAOException(
"
U cannot del others' message!
"
);
}
if
(manager.contains(msg)) {
manager.remove(msg);
}
else
{
removeMessage(msg.getId());
}
}
public
void
removeMessage(String id) {
manager.createQuery(
"
DELETE FROM Message m WHERE m.id = :mid
"
)
.setParameter(
"
mid
"
, id).executeUpdate();
}
public
Message getMessage(String id) {
return
(Message) manager.find(Message.
class
, id);
}
}
(看看是不是比Hibernate3.0简单,好像Hibernate3.2和这个一样,偶还没试过3.2)
最后最关键的是实体定义和映射,到regsaga上面看了篇讲解Annotation的文章
(
http://wiki.redsaga.com/confluence/display/HART/Mapping+entity+bean+associations+relationships
)
原来Hibernate和EJB3的注解出自同门,我好像又明白了一点什么~_~
改完了,要开始测试了,真是激动人心。
确保D:\jboss-4.0.4.GA\server\default\lib\mysql-connector-java-3.1.12-bin.jar
然后拷贝D:\jboss-4.0.4.GA\docs\examples\jca\mysql-ds.xml
到D:\jboss-4.0.4.GA\server\default\deploy并做小部分修改
~_~。
好像还要加一个persistence.xml一起打在jar包里边。写一个Ant任务吧,大家都这么做(还没有在Eclipse下打包并发布的经验,只会
JBuilder~_~)
几番周折,给Ant加了打jar包打war包以及deploy的任务。
启动JBoss-4.0.4-GA,出了些错误,一一改正。。
JUnit测试通过了,但打开JSP页面注册时报错
18:31:28,859 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.reflect.UndeclaredThrowableException
at $Proxy177.saveUser(Unknown Source)
at org.apache.jsp.regist_jsp._jspService(regist_jsp.java:64)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at cn.hxex.message.util.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:127)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:595)
Caused by: java.io.NotSerializableException: cn.hxex.message.model.User
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1075)
at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1245)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1069)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:291)
at java.rmi.MarshalledObject.<init>(MarshalledObject.java:92)
at org.jboss.aop.joinpoint.MethodInvocation.writeExternal(MethodInvocation.java:318)
at org.jboss.serial.persister.ExternalizePersister.writeData(ExternalizePersister.java:58)
at org.jboss.serial.objectmetamodel.ObjectDescriptorFactory.describeObject(ObjectDescriptorFactory.java:275)
at org.jboss.serial.objectmetamodel.DataContainer$DataContainerOutput.writeObject(DataContainer.java:386)
at org.jboss.serial.io.MarshalledObjectForLocalCalls.<init>(MarshalledObjectForLocalCalls.java:38)
at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:57)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(StatelessRemoteProxy.java:102)
... 29 more
始终没法解决,非常伤心,也很郁闷。决定先放弃一段时间。。。。。。。。