Eclipse + JBoss 5 + EJB3 Development Guide (13)

Source: Internet
Author: User
Tags xmlns jboss

Accessing application control Entitymanager objects in the servlet

The Entitymanager objects used in the previous articles are container-controlled. This type of Entitymanager object is created by the EJB container, in which case the lifecycle of the Entitymanager object is entirely managed by the EJB container. The following code can be used in Sessionbean to notify the EJB container to create the Entitymanager object:

@PersistenceContext (unitname= "myentity")
protected Entitymanager em;

The code above needs to be placed in any Sessionbean class, and the EJB container will automatically create the Entitymanager object. However, if you want to access the Entitymanager object directly in the servlet, you need to use the Createentitymanager method of the Entitymanagerfactory interface to create the Entitymanager object. After you use the Entitymanagerfactory and Entitymanager objects, you can close the two objects by using the appropriate closing side. Before accessing application-controlled entity beans in a servlet, you need to modify the contents of a Persistence.xml file, and the following results are modified:

<?xml version= "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 ">

<pers Istence-unit name= "myentity" transaction-type= "resource_local" >
<non-jta-data-source>java:/mypim </non-jta-data-source>
<properties>
<property name= "Hibernate.hbm2ddl.auto" V  Alue= "None"/>
<property name= "Hibernate.dialect" value= "

org.hibernate.dialect.MySQL5Dialect" />
<property name= "Hibernate.format_sql" value= "true"/> <property name=
"Hi Bernate.show_sql "value=" true "/>
</properties>
</persistence-unit>
</persist Ence>

The modifications are as follows: two

1. Set the Transaction-type property value of the <persistence-unit> element to resuorce_local.

2, change the <jta-data-source> element to <non-jta-data-source>, and the element value is unchanged.

Note that you cannot use the @persistencecontext annotation to tell the EJB container to automatically create the Entitymanager object, and if you still want to do so, you can define additional <persistence-unit> elements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.