|
Advertisement |
JSF CDI AND JPA IN NETBEANS - Part 3
Posted On September 11, 2012 by Shruthi S filed under Java
In this third part of the article author continues to explain how to implement JSF and JPA in Netbeans.
Sun might have failed as a business entity but by allowing a finger in the pie to many stake holders it has succeeded in making Java the number one technology especially in the enterprise sphere and there appears to no end to this position. Google has used this in Android and Microsoft has created a similar platform and a language in Dot Net and C#. They were so greatly influenced by Java platform and Java language that some call C# a clone of Java. Now great open source technologies of Java platform such as Spring, Hibernate are ported to dot net platform. This is known to everybody but the fact that JPA is a great influence on ADO.Net Entity Frame Work may not be so obvious. One’s knowledge of Java will come in handy if one wants to learn C#. Likewise knowledge of JPA will be useful if one wants to experiment with Entity Framework of dot net platform. It is hoped that we can throw more light on this at some day in future and for the time being we will concentrate on the topic in hand. In the previous month articles on JPA, it was mentioned that to use Application- managed persistence, we have to write the boiler- plate code for obtaining EntityManagerFactory(EMF). From EMF, EntityManager(EM) has to be got and we have to close it once a database operation is over and our code has to provide for the same and we also have to code for getting transaction support. EntityManager can be obtained from the method createEntiityManager() of the EntityManagerFactory object. In Java SE, EntityManagerFactory, in turn can be got from the factory method createEntityManagerFactory() of the class javax.persistence.Persistence. But in J2EE environment we can tell the server to inject EntityManagerFactory object through @PersistentUnit annotation in a EntityManagerFactory field as shown below:
@PersistenceUnit(unitName=" ")
private EntityManagerFactory emf;
The application can obtain an entity manager by calling:
EntityManager em = emf.createEntityManager();
When the entity manager is no longer required, the method close() of EntityManager object can be called as follows:
em.close();
Any work with the entity manager should be wrapped into a transaction. A transaction manager can be obtained with the injection:
@Resource private UserTransaction utx;
We can understand things better by creating and running a project using the above code.
Create a project: Create a web project in Netbeans6.8 or 6.9.The database and the table created for the part-II can be used. But as mentioned GlassFish Server 3 should be chosen in Server and Java EE 6 Web will automatically appear in Java EE version but you have to click Enable Context and Dependency Injection and click Next and choose JSF among frameworks and click Finish. Necessary folders and files will be created. Under Source Packages create a package say cdi and right click on it and choose NewàEntity Class from Database. In the wizard that appears in the Data Source choose the data source you created for part-II and in the wizard that appears fill up the JNDI name as shown earlier. When connection is established the available table will appear and you can click Add All and the table will appear in Selected Tables and click Next and finish. Necessary entity class and JPA configuration file will be generated
CDI bean. Under cdi create a new java class UserBean and it may be completed as under:
UserBean.java |
|
package com.cdi; public String login() { public String logout() { if (result.isEmpty()) |
1) EntityManagerFactory emf field is annotated with @PertsistenceUnit .The server injects the thread-safe emf object. Unlike EntityManager object, the EntityManagerFactory object is not obtained and closed.
2) UserTransaction field is annotated with @Resource annotation to obtain the transaction manager.
3) EntityManger object em is obtained from createEntityManager() method of EntityManagerFactory object.
4) & 5) & 6) begin() in UserTransaction object is invoked before the joinTransaction() of EntityManager object is called and object to be persisted is created and persisted.
7) After persisting the object the transaction has to be committed or in case of failure to commit the transaction has to be rolled back.-See the transaction is begun, joined and committed within a try block and rolled back in a finally block.
8) The EntityManager object is closed when the operation for which it was obtained was over in another finally block.
9) For one operation , a new EntityManager object Is obtained and is closed when the operation is over.
View Files The view files and faces-config.xml can be used from part-II application. When you Run the project you will find no difference between part-II and this application. When you click New User button the register page will be displayed. When you fill up the registration form, if the password and passwordv do not match you will get Figure 1.When you correctly submit the form you will get Figure 2.When you click Continue you will get the login page with username and password filled as shown in Figure 3.
Note: CDI does not provide a security module of its own but provides an integration point for Seam3 Security module which is only in the alpha stage. In the mean time experts point out that CDI interceptors can be used to secure the CDI application.






