1. Create Project > enterprise app (check option create ejb and web too)
2. @ejb project, create Persistence > Database Schema
Then create Entity Class from DB using this database schema
3. @ejb project, create Persistence Unit (use default setting, Persistence Provider:Hibernate),
specify the Datasource
4. @ejb project, create SessionBean > check both Local and Remote option, Stateless
5. in eBookBean.java, add
@PersistenceContext
private EntityManager em;then create a method and implement it. after that add the method to eBookBeanLocal.java as well
6. @war project, create servlet, to access the EJB, use this
@EJB
private eBookBeanLocal ebookBean;
then, initialize the EJB by Initial Context Lookup using EJB's JNDI
Default JNDI name:
ear-name/ejb-name/remote & ear-name/ejb-name/locale.g eBook.ear contains eBookBean, the JNDI name will be "eBook/eBookBean/local"
7. @main project, create new Application Deployment Descriptor for deployment to JBoss:
Right click on Main project > New Standard Deployment Descriptor
File Name: application.xml
***IMPORTANT***
otherwise, will throw DeploymentException during deployment and thus
will get 404 file not found error when access to the jsp/servlet8. Edit jboss-ds.xml on JBoss deploy folder to include the data source (if newly created in step 2)
9. clean and build > deploy
Troubleshoot:
Problem:
ERROR [hk.edu.ouhk.ebook.bookstore.getBookList] Fail to lookup
eBook/eBookBean/local:javax.naming.NameNotFoundException: eBook not boundPossible root cause:
eBook.ear depends on Data Source which is not yet configured in JBoss.
Ensure Data Source is correctly configured and Bean class is working properly. For example, incorrect NamedQuery will lead to such error.
If database schema has been changed, make sure to re-generate new Entity class from database.
Problem:
ERROR org.hibernate.MappingException: named query not knownPossible root cause:
persistence.xml is incorrectly configured with provider or properties.
<provider>org.hibernate.ejb.HibernatePersistence</provider>
minimal setting:
(Remove <exclude-unlisted-classes>false</exclude-unlisted-classes> as well)
<?xml version="1.0" encoding="UTF-8"?>Problem:
<persistence version="1.0" 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">
<persistence-unit name="HAVideo-ejbPU" transaction-type="JTA">
<jta-data-source>java:/HAVideo</jta-data-source>
<properties/>
</persistence-unit>
</persistence>
Lookup EJB local bean Fail
Possible root cause:
Some entity class created has problem. try recreate all entity class.
And try add back the file: MAS-ejb\Configuration Files\ejb-jar.xml
ejb-jar.xml look like this:
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar version="2.1" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<enterprise-beans>
<session>
<display-name>MAS</display-name>
<ejb-name>MAS</ejb-name>
<local>hk.edu.ouhk.cfi.mas.ejb.MasLocal</local>
<ejb-class>hk.edu.ouhk.cfi.mas.ejb.MasBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>MasBean</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Problem:
If show
jboss.jca:service=Download jboss-local-jdbc.rar and put it in default/deploy folder. It is used for DataSource failover.In case Datasource has problem, this rar is required.RARDeployment,name='jboss-local-jdbc.rar' State: NOTYETINSTALLED
沒有留言:
張貼留言