Table of Content :
- Introduction
- Create the JPA Diagram
- Create the Stored Procedure
- Generate the entity classes
- Existing Database
- Create tester to call Named Stored Procedure
- Deploy the EJB Module
- Output
- Source Code
Introduction :
In this tutorial we will go through the basic execution of stored procedure using JPA Modeler .JPA 2.1 supports :
- Named stored procedures calls defined in @NamedStoredProcedureQuery annotation and created through EntityManager.createNamedStoredProcedureQuery() .
- Dynamic stored procedure calls created through EntityManager.createStoredProcedureQuery().
Create the JPA Diagram :
- Goto File menu > New File > Persistence category
- From the Persistence , select JPA Diagram from Database ( or create new JPA Diagram ) and click Next.
Select JPA Diagram - Select table and add to import entity from DB .
Select Tables - Type SmapleERD for the diagram name.
- Type com.jpamodeler.nsp for the Package.
- Click Finish.
Create JPA Diagram
When we click Finish, the IDE creates the JPA Diagram and opens the diagram in the designer window.
Create the Stored Procedure :
- Click on the entity > Properties .
- From the properties , select Named Stored Procedure Query Property.
Open Stored Procedure Query Panel - Click on the Add button to create new named stored procedure .
Create Stored Procedure - In this panel , first select the Database connection.
- It will fetch the existing stored procedure list from the DB.
- Now select the Stored Procedure and it will fetch existing stored procedure parameter list from the DB.
Select Database Connection to fetch SP - Click on the Result Set Tab to add result classes or resultset mappings.
Select Result Classes - Click on the save button to save named stored procedure.
Created Stored Procedure
Generate the entity classes :
Existing Database :
Click on the Services window > DatabasesDatabase existing table and data |
I have used MySQL as backend database, however PostgreSQL , MS SQL Server or Oracle can also be used. As the schema I have used a simple table to store student’s data.
Student Table SQL Query :
Stored Procedure (MySQL) :
Create tester to call Named Stored Procedure :
In this exercise we will create a simple startup singleton bean to esecute the store procedure and fetch the student by batch :- Right-click the EJB module and choose File > New File wizard .
- In the New File wizard, expand the Enterprises JavaBeans and select Session Beans as shown in the figure below.
Create Tester - Now , we have to specify the EJB Name and the Package Location in the appropriate text fields and then click Finish.
- When we click Finish, the class Tester.java opens in the Source Editor.
Deploy the EJB Module :
We can now build and deploy the EJB module. Right-click the StoredProceduresModule module and choose Deploy. When we click Deploy, the IDE builds the ejb module and deploys the JAR archive to the server. In the Services window, if we expand the Applications node of GlassFish Server, we can see that StoredProceduresModule was deployed.After we have defined the @NamedStoredProcedureQuery, we can use the createNamedStoredProcedureQuery(String name) method of the EntityManager to create a StoredProcedureQuery object. This provides the required methods to set the input parameter, call the stored procedure and get the result.
1 comments:
Ufrinulwolf_no_1981 Lisa Williams https://wakelet.com/wake/XDNoiXzTiSBqIjPc_31Zn
galestiogold
Post a Comment