本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:
eclipse + JBoss 5 + EJB3开发指南(12):使用命名查询执行JPQL
在前面的文章中使用的都是容器管制的EntityManager对象。这类EntityManager对象是由EJB容器创建的,在这种情况下,EntityManager对象的生命周期完全由EJB容器来管理。在SessionBean中可以使用如下的代码来通知EJB容器创建EntityManager对象:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
@PersistenceContext(unitName
=
"
myentity
"
)
protected
EntityManagerem;
上面的代码需要放在任意一个SessionBean的类中,EJB容器会自动创建EntityManager对象。但如果要在Servlet中直接访问EntityManager对象,需要使用EntityManagerFactory接口的createEntityManager方法来创建EntityManager对象,在使用完EntityManagerFactory和EntityManager对象后,可以使用相应的close方方关闭这两个对象。在编写Servlet中访问应用程序管制实体Bean之前,需要修改一个persistence.xml文件的内容,修改后的结果如下:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
<?
xmlversion="1.0"encoding="UTF-8"
?>
<
persistence
xmlns
="http://java.sun.com/xml/ns/persistence"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version
="1.0"
>
<
persistence-unit
name
="myentity"
transaction-type
="RESOURCE_LOCAL"
>
<
non-jta-data-source
>
java:/MyPIM
</
non-jta-data-source
>
<
properties
>
<
property
name
="hibernate.hbm2ddl.auto"
value
="none"
/>
<
property
name
="hibernate.dialect"
value
="org.hibernate.dialect.MySQL5Dialect"
/>
<
property
name
="hibernate.format_sql"
value
="true"
/>
<
property
name
="hibernate.show_sql"
value
="true"
/>
</
properties
>
</
persistence-unit
>
</
persistence
>
修改的地方有如下两个:
1. 将<persistence-unit>元素的transaction-type属性值设为RESUORCE_LOCAL。
2, 将<jta-data-source>元素改成<non-jta-data-source>,元素值不变。
要注意,这时不能使用
@PersistenceContext注释通知EJB容器自动创建EntityManager对象了,如果仍然想这样做,可以定义其他的<persistence-unit>元素。
下面是访问EntityManager对象的Servlet代码:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->
package
servlet;
import
java.io.IOException;
import
javax.persistence.EntityManager;
import
javax.persistence.EntityManagerFactory;
import
javax.persistence.Persistence;
import
javax.persistence.PersistenceContext;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
entity.DateBean;
public
class
MyServlet
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
protected
void
service(HttpServletRequestrequest,
HttpServletResponseresponse)
throws
ServletException,IOException
{
EntityManagerFactoryemf
=
Persistence.createEntityManagerFactory(
"
myentity
"
);
EntityManagernewEM
=
emf.createEntityManager();
DateBeandb
=
newEM.find(DateBean.
class
,
1
);
response.getWriter().println(db.getMyDate());
newEM.close();
emf.close();
}
}
下一篇:
eclipse + JBoss 5 + EJB3开发指南(14):消息驱动Bean
国内最棒的Google Android技术社区(eoeandroid),欢迎访问!
《银河系列原创教程》发布
《Java Web开发速学宝典》出版,欢迎定购