In the last article, it was shown how to create Java Bean Wrapper classes that can be called from a Spring Data Access Object (DAO) using the Java Bean Wrapper Wizard.
This article will demonstrate how to tie Java Bean Wrapper classes together using the Spring DAO Wizard to create a reusable data access layer for a business service. Also it will be shown how to create an automated unit test for your DAO.
The Spring DAO Wizard is interface driven. This wizard allows Java Beans, generated by the Java Bean Wrapper Wizard to be selected to implement the interface methods of a DAO class.
If you have not worked through the example shown in the last article Using the Spring Framework with the Teradata Plug-in for Eclipse, do so now before you continue.
In the last article, you created several Java Bean Wrapper classes. In this article, you will need an interface class to tie all of these classes together in a DAO. Please go to the Package Explorer in Eclipse and select the package “employee.repository” from the "Employee_Project" and right click. Select the menu item New->Interface. The New Java Interface Wizard will pop up. Enter “IEmployeeDAO” for the interface class name.
Once this is done, select the “Finish” button.
The Java interface class "IEmployeeDAO" will now be in the Eclipse Java editor. Enter the code that is in the image below in your Java editor.
Once this is done, right click in the editor and select the “Save” menu item.
Next, select the interface you have just created in the Eclipse Package explorer and right click. Select the menu option “Implement Spring DAO”. This launches the Spring DAO Wizard.
The Specify Spring DAO Wizard page is the first page of the Wizard. This page allows you to select the source folder, package and class name of the new class to be generated. This screen also allows you to select the option to create a unit test for the DAO that will be implemented. Enter “employee.repository” for the package name and “EmployeeDAO” for the class name. Also select the check box “Create Unit Test for DAO” and select the database profile in which you wish to run your unit test.
Once this is done, select the “Next” button.
The next page of the wizard is the Implement Spring DAO Methods Wizard page. This page allows you to implement the methods of the Spring DAO interface. The screen lists all of the Spring DAO methods in a table. The table has combo boxes of options of implementation types for each Spring DAO method. The default option selected for each method is ”None”. You will not be allowed to continue if the “None” option is selected for any of the interface methods.
The implementation option of "Stub" creates a skeleton stub method for each DAO method.
The "Bean" implementation option allows you to map a Bean created by the Java Bean Wrapper Wizard. The Bean must implement the Spring CallableStatementCallBack interface to be used. Select the “Bean” implementation type for all of the methods for your DAO.
You must map each method to a Java Wrapper Bean by selecting the "Map Bean" toggle and then selecting a Java Wrapper Bean class from the “Select Java Bean” pop up dialog.
Select the "Map Bean" toggle for the method "updateEmployee" in the DAO implementation table. This will pop up the "Select Java Bean" dialog. Select the Java Bean Wrapper class “UpdateEmployee” in the “Select Java Bean” popup dialog.
Once this is done, select the “OK” button.
Once the Bean has been selected, the "Map Parameters" toggle becomes available to you. You are not required to map the parameters of the interface method but if the parameters are not mapped, a “TODO” comment will be inserted in the generated code stating that the methods' parameters need to be used.
You can map parameters by selecting the map parameters toggle. This will cause the Dialog Map DAO parameters to come up. The dialog consists of a list of parameters and a table of mappings of the selected parameters from the list. A parameter can be a class object. If the parameter is a class object all getter methods will be displayed in the mapping table. Only one mapping is required in the mapping table for the user to continue. A mapping consists of a parameter being used with a Java Bean Wrapper setter method which matches the parameter type.
Select the "Map Parameter" toggle in the DAO implementation table for the method “updateEmployee”. This will cause the “Map DAO Parameters” dialog to pop up. Select “emp” in the parameter list. Go to the "Map Selected Parameters" table in the pop up dialog and map the “emp” object parameter in the following way:
• emp.getEmpDept() -> UpdateEmployee.setEmpDept(emp.getEmpDept())
• emp.getEmpID() -> UpdateEmployee.setEmpId(emp.getEmpID())
• emp.getEmpName() -> UpdateEmployee.setEmpName(emp.getName())
• emp.getEmpJob() -> UpdateEmployee.setEmpJib(emp.getEmpJob())
Once this is done, select the “OK” button.
Now repeat the process of mapping Beans to your DAO methods in the following way:
• getAllEmployees -> GetAllEmployees.java
• getEmployeeByID -> GetEmployeeByID.java
• getEmployeeByDept -> GetEmployeeByDept.java
• size-> GetNumberEmployees.java
Now repeat the process of mapping the parameters to your DAO methods.
• getEmployee(int id) -> GetEmployeeByID(id)
• getEmployeeByDept(String dept) -> GetEmployeeByDept(dept)
Once this is done, select the “Finish” button.
Implemented Spring DAO Class
Once the “Finish” button is selected in the Spring DAO wizard, your implemented class will appear in the Eclipse Java editor.
The implemented methods for the new Spring DAO class can be skeleton stubs or methods that have been mapped to a Java Bean Wrapper.
A Stub method is just a place holder. The method has a “TODO” comment prompting you to implement the method. If the method is called with out any changes, an exception is thrown.
A mapped Bean method is a DAO method that been implemented to call a Java Bean Wrapper. The Java Bean Wrapper class now has a static method named castReturnValue. This method casts the object returned by the execute method from the JDBC template in the DAO class. The castReturnValue in the Java Bean Wrapper can be customized so it can be mapped to any type returned by the DAO method. If the castReturnValue does not exist or does not return the correct value, the DAO method will return null and a “TODO” comment will be in place to prompt the user to cast the return value object to the correct type. In this case, all of your methods have been mapped to Java Bean Wrapper classess.
The parameters of a DAO method can be mapped to setter methods of the Java Wrapper class. If the parameters are not mapped, a “TODO” comment will be inserted in the generated code stating that the methods' parameters need to be used.
If a DAO method parameter is a class object, the getter methods of the class being passed in as a parameter can be mapped to the setter methods of the Java Bean Wrapper class used in the DAO method.
The Spring DAO Wizard also generates a TestNG unit test if the "Create Unit Test for DAO" option is selected in the Specify Spring DAO Wizard page.
The DAO test class you created is in a separate source folder "test/java". The unit test DAO Package is the same package name as the DAO class package name. The DAO unit test name is the DAO class name with the prefix of "Test". In this case, the test name is “TestEmployeeDAO”.
The following files were created to support the DAO unit test when the “Finish” button is selected in the Spring DAO Wizard:
• src/config.jdbc.properties - Properties file with JDBC-related settings including connection information
• test/config/test-config.xml - TestNG Test Suite configuration file
• test/config/(DAO Class Name)-context.xml - Spring context file for the DAO unit test
• build.xml - Build file for the DAO and DAO unit test
The generated DAO unit test is a very simple implementation. If the DAO class method being tested has a parameter that is a numeric primitive, the value is always set to zero in the test class. If the parameter is a class object, it is always set to null such as the method below.
The parameter values for the unit test should be changed to values that make sense for the DAO method being tested or the unit test may fail.
Change your test method “testGetEmployeeByDept” in the Java editor by setting the variable “dept” to the value “Product Development”. Also check if one value was returned and that the record returned was the employee “Mike Smith”.
The other methods in your test class will also need to be changed.
Edit the method "testGetAllEmployees" by verifying three results were returned.
Change the method “testGetEmployeeById' by setting the variable “id' to 100002 and verify that “Steven Brown” was the employee returned.
Edit the method "testSize" to verify that the number of employees is three.
Finally, change the method “testUpdateEmployee” by giving the employee “James Parker” a promotion to “Vice President” and verifying the change. See the code below in the image for the specific changes for this test case.
Once you are done editing your Spring DAO unit test, you can run it by using the following steps:
• Right click on the build.xml file and select Run As -> External Tools Configurations ...
• Right Click Ant Build in the External Tools Configurations Dialog and select new
• Enter “run-tests” in the Arguments text box and select the Apply button in the External Tools Configurations Dialog
Select “Run” button in the External Tools Configurations dialog . The results of the test will be in your Eclipse console.
You can also review the test results in the index.html file in the main project directory.
This article has shown you how to tie Java Bean Wrapper classes together using Spring. You implemented an interface to create a DAO using the Spring DAO Wizard. Also you automated a test for your Spring DAO class. The Spring DAO Wizard gives you a quick and easy way to create a reusable data access layer for your business process.
Is TeraData support spring batch job transactions? Any tech knowhow on that!
This job will read very specific data from application database and write to TeraData for further use by different application.
The Teradata Java Wizards for the Teradata IDE Plug-in for Eclipse does not support batch insert for MyBatis and Spring. You can do this with Teradata but there are no code generation tools for this type of application.
Thanks John for your reply.
I got curious because org.springframework.batch.support.DatabaseType support only DB2, DB2ZOS, DERBY, H2, HSQL, MYSQL, ORACLE, POSTGRES, SQLSERVER, SYBASE.
Is there any tech spec on spring batch job transactions supported by teradata?
You can use an annotation driven transaction manager using the class "org.springframework.jdbc.datasource.DataSourceTransactionManager" and it should work fine.