Notice that in this code we do not use transactions. The reason for this is that this code only performs a read operation and not a write operation. So even if the operation failed, none of the data in the DB would be effected. So there is no need to protect the operation using a transaction.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/* 1. Now retrieve the new product line,
using the ID we created */
|
| Figure 13. updateProductline implementation |
Notice that we wrap the update call with a transaction, this is because it has to write something to the database and we need to protect it from failure.
In section #3 above we are loading the productline from the database right after we updated it, and printing out the values we get back from the database to confirm that it's updated.
Deleting an entity is almost identical to how we saved and
updated the entity. We wrap our work in a transaction and then
tell the DAO to do the work for us:
/* 1. Now retrieve the new product line,
using the ID we created */
|
| Figure 14. deleteProductline implementation |
Similar to our updateProductline implementation above, you'll notice that we use the transaction to wrap the delete call and then attempt to load the entity from the DB and confirm that the operation should fail.
NOTE: The reason the transaction has to wrap both the findById and delete method calls is because objects managed by JPA must be part of the same transaction. So in order for us to erase the object we load, it has to be in the same transaction that we loaded it and tried to erase it.
The output from running this looks like this:
|
|
Figure 15. Output
|
The red text are default log messages that can be ignored (You
can setup a custom
log4j.properties file if you wish to control logging).
Then below the log warnings we see 2 messages from TopLink (our JPA implementation libraries) and then we see 3 more messages, all from our implementation.
Our first message prints out the new productline information that was added, the second one updates it and prints the new information and the last one deletes it from the DB and prints the confirmation message.
In addition to the user managed transactions described in the previous section, Spring also supports container-managed transactions via the @Transactional attribute. For container-managed transaction support, you must enable it during capability addition:
|
|
Figure 16. Turning on support for the @Transactional annotation
|
Enabling this adds the following element to your bean configuration file. We also add a JPAServiceBean which we will use to delete an entity using container-managed transactions.
|
|
Figure 17. Annotation driven configuration element
|
The JPAServiceBean implementation is shown below, note the @Transactional annotation on the deleteProductLine method and the absence of any user-managed transaction statements.
public class JPAServiceBean {
private IProductlineDAO dao;
@Transactional
public void deleteProductLine(String productlineID) {
/* 1. Now retrieve the new product line, using the ID we created */
Productline loadedProductline = dao.findById(productlineID);
/* 2. Now let's delete the product line from the DB */
dao.delete(loadedProductline);
/*
* 3. To confirm the deletion, try and load it again and make sure it
* fails
*/
Productline deletedProductline = dao.findById(productlineID);
/*
* 4. We use a simple inline IF clause to test for null and print
* SUCCESSFUL/FAILED
*/
System.out.println("Productline deletion: "
+ (deletedProductline == null ? "SUCCESSFUL" : "FAILED"));
}
public void setProductLineDAO(IProductlineDAO dao) {
this.dao = dao;
}
}
|
| Figure 18. @Transactional deleteProductline implementation |
Obtain an instance of JPAServiceBean from the application context and use it as follows:
JPAServiceBean bean = (JPAServiceBean) ctx.getBean("JPAServiceBean");
bean.deleteProductLine(productlineID);
|
| Figure 19. Using JPAServiceBean |
We have reached the end of our MyEclipse JPA Spring tutorial. In
this tutorial we tried to show not only the strengths of JPA as a
persistence technology, but how powerful the combination of JPA
and Spring together can be for developing large, robust
applications. It is also important to note that we wrote very
little code while MyEclipse generated the vast majority of it for
us.
If you have any suggestions for us to help make it more informative, please let us know.
Below we would like to provide you with some more information pertaining to the topic covered in this tutorial. We offer the FAQ section for quick references to common questions and the Resources section with links to other helpful resources online that you may want to become familiar. We realize we can't cover every question you may have in one tutorial, but between this tutorial contents and our additional learning resources we hope you are far on your way to feeling comfortable with the technology.
In this section we want to provide you with additional links to resources that supplement the topics covered in this tutorial. While this is not an exhaustive list, we do make an effort to point to the more popular links that should provide you with diverse, high-quality information.
We would like to hear from you! If you liked this tutorial, has some suggestions or even some corrections for us please let us know. We track all user feedback about our learning material in our Documentation Forum. Please be sure to let us know which piece of MyEclipse material you are commenting on so we can quickly pinpoint any issues that arise.