10. More options for the Model




10.1 Understand the roles of JDBC and JPA

JDBC

See JDBC from OCPJP

JPA

JPA provides an object oriented way of persisting entities. If your application is already modeled with entities (as opposed to relational tables), it is preferable to go with JPA. By using JPA annotations on your entities, appropriate relational view can be generated automatically and all raw database access code can be eliminated.


10.2 Understand the many elements that make up the model

@Resource annotation

The @Resource annotation is used to declare a reference to a resource such as a data source, Java Messaging Service (JMS) destination, or environment entry. This annotation is equivalent to declaring a resource-ref, message-destination-ref or env-ref, or resource-env-ref element in the deployment descriptor.

@Resource can be used with JNDI. These resources can be used by:

  • A method annotated with @PostConstruct
  • A method annotated with @Resource
  • The servlet's init() method
@WebServlet(urlPatterns={"/wftestservlet1"}, name="WFTestServlet1")
public class WFTestServlet1 extends HttpServlet{
    @Resource(name="RESOURCE1")
    private String r1;
 
    @Resource(name="RESOURCE2")
    private String r2;
 
    public void service(HttpServletRequest req, HttpServletResponse res){
        System.out.println(r1+" : "+r2);
    }
}

The associated web-fragment.xml contains the following:

<web-fragment>
    ...
    <env-entry>
        <env-entry-name>RESOURCE1</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>AAA-WF</env-entry-value>
    </env-entry>
    <env-entry>
        <env-entry-name>RESOURCE2</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>BBB-WF</env-entry-value>
    </env-entry>
    ...
</web-fragment>

EJB

As of JEE 6, a war can include EJB classes as well.

Local bean

<ejb-local-ref>
    <ejb-ref-name>ejb/Customer</ejb-ref-name>
    <ejb-ref-type>Entity</ejb-ref-type>
    <local-home>com.wickedlysmart.CustomerHome</local-home>
    <local>com.wickedlysmart.Customer</local>
</ejb-local-ref>

Remote bean

<ejb-ref>
    <ejb-ref-name>ejb/LocalCustomer</ejb-ref-name>
    <ejb-ref-type>Entity</ejb-ref-type>
    <home>com.wickedlysmart.CustomerHome</home>
    <remote>com.wickedlysmart.Customer</remote>
</ejb-ref>

Other

  • resource-ref is used to specify datasource and Topic and Queue connection factories.
  • resource-env-ref is used to specify Topics and Queues
    <resource-ref>
        <res-ref-name>MyDatasource</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-ref>
        <res-ref-name>MyTopicConnectionFactory</res-ref-name>
        <res-type>javax.jms.ConnectionFactory</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
    <resource-env-ref>
        <resource-env-ref-name>MyTopic</resource-env-ref-name>
        <resource-env-ref-type>javax.jms.Topic</resource-env-ref-type>
    </resource-env-ref>



10.3 Understand fundamentals of connecting to a database using JDBC or JPA

JDBC

See JDBC from OCPJP

JPA

@PersistenceContext

This annotation injects an entity manager for referenced persistence units. Example:  

 @PersistenceContext (type=EXTENDED)
   EntityManager em;

@PersistenceUnit

The @PersistenceUnit annotation injects an entity manager factory. Example:

   @PersistenceUnit
   EntityManagerFactory emf;






Comments

If you have questions, corrections, information or examples to add, please comment below.

Add a New Comment
or Sign in as Wikidot user
(will not be published)
- +
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License