Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
former_member182046
Contributor
0 Kudos

h3. Overview

In the first part  () of the blog series we installed the MySQL database server and established a connection to it from NetWeaver Developer Studio. Then we deployed the JDBC driver to the CE server and created a Custom DataSource connection for use by our Java Enterprise applications.

In the second part  () we set up the Development Components of our Java Enterprise application, connected the EJB DC with a Dictionary DC, and adjusted the deployment descriptors to our needs.

In this part we will create a simple JPA Entity, a database table, and a EJB 3.0 Stateless Session Bean to act as a façade for the entity.

Create a Simple JPA Entity

In the EJB DC, right-click on node ejbModul and create package de.thor.mysql.

Right-click on the package name and select “Create JPA Entity”. Name the class “Blogger” and click “Next”.

Change the default table name to “TMP_BLOGGER” and add the following fields:

Field name

Type

Key field</td>

  </tr>

<tr><td>id </td><td>int</td><td>true</td></tr>

<tr><td>firstName</td>

<td>java.lang.String</td><td>false</td></tr>

<tr><td>lastName</td><td>java.lang.String</td><td>false</td></tr>

</tbody></table>

<br /><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-5.JPG|height=484|alt=|width=593|src=https://...!</p><p>Click “Finish” to start generation of the class.</p><p>Here’s an excerpt of the generated code:</p><pre>@Entity<br />@Table(name="TMP_BLOGGER")<br />public class Blogger implements Serializable {   <br />   @ID   <br />   private int id;   <br />   private String firstName;   <br />   private String lastName;<br />   private static final long serialVersionUID = 1L;<br /><br />   public Blogger() {<br />     super();<br />   }   <br />   // Setter and getter methods<br />   // ...   <br />}</pre><p>It’s a very basic Entity because it doesn’t even have Id auto-generation and a version field. </p><p>To generate dictionary metadata out of the Entity, right-click on mysql_ejb in the Project Explorer View and select “JPA Tools – Generate DDL”. A summary dialog appears:</p><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-7.JPG|height=336|alt=|width=571|src=https://...!</p>h3. Adjust the public part

<p>Open the Component Properties view for the EJB DC, select tab “Public Parts” and right-click on public part “client”. Select “Manage Entities” and add Java class “Blogger” to the public part.</p>h3. Create a Stateless Session Bean

<p>A good way to expose the functionality of our JPA Entity is to create an EJB 3.0 Stateless Session Bean – also because it allows us to create a web service which we can use for simple tests.</p><p>So right-click on package de.thor.mysql and select “New – EJB Session Bean 3.0”. Name the class “BloggerFacade” and accept the other defaults in this and the following dialog.</p><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-8.JPG|height=427|alt=|width=400|src=https://...!</p><p>First we’re going to add two lines of code right at the beginning of the class. The name of the persistence unit is the same we defined in deployment descriptor persistence.xml.</p><pre>public class BloggerFacadeBean implements BloggerFacadeLocal {<br />    @PersistenceContext(unitName = "blogger_pu")<br />    EntityManager em;<br />    …</pre><p><br />Now we will add methods for saving (creating or modifying) a particular Blogger, retrieving a list of all the bloggers and retrieving a particular blogger by id.</p><pre>    public Blogger saveBlogger(Blogger blogger) {<br />        em.merge(blogger);<br />        em.flush();<br />        return blogger;<br />    }<br />    <br />    @SuppressWarnings("unchecked")<br />    public List<Blogger> getAllBloggers() {<br />        return em.createQuery("SELECT b FROM Blogger b").getResultList();<br />    }<br /><br />    public Blogger getBloggerById(int id) {<br />        return (Blogger) em.find(Blogger.class, id);<br />    }<br /></pre><p>In the Outline view, right-click on the three new methods and select “EJB methods – Add to local interface”. </p>To expose the EJB as a web service, add annotation @WebService right below the @Stateless annotation.h3. Deploy the application<br />

<p>Before deploying the Enterprise Application, you have to deploy Dictionary DC mysql_dict. This will cause the creation of table TMP_BLOGGER in the system database.</p><p>Now you can deploy DC mysql_ear. If all went well and your Deployment Console view reports a “Success”, our web service should be up and running. </p>h3. Test the functionality

<p>Open the Web Service Navigator (http://hostname:port/wsnavigator). Select search type “Provider System” and search for “BloggerFacadeBean”.</p><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-9.JPG|height=428|alt=|width=533|src=https://...!</p><p>Click “Next”, select operation “saveBlogger”, and click “Next” again. Fill in the data to create a blogger entry and click “Next”.</p><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-10.JPG|height=143|alt=|width=317|src=https:/...!</p><p>The next screen shows the result of the successful operation. </p><p>!https://weblogs.sdn.sap.com/weblogs/images/14831/mysql2-11.JPG|height=400|alt=|width=306|src=https:/...!</body>

1 Comment