jueves, 27 de agosto de 2009

Anotaciones del tutorial: “Using Java Persistence in a Web Application”

http://www.netbeans.org/kb/docs/web/customer-book.html

Coding the Web Application

Development for the Java EE 5 platform is much easier than before because the container does more of the work. Because the container is managing persistence, you do not need to edit any deployment descriptors to configure the CMP mappings or specify the finder methods. In the Java EE 5 platform, persistence is no longer limited to an EJB component. Java EE 5 introduces the Java Persistence API, which can be used by non-EJB components, such as a web application.

Java EE 5 technology also eliminates the need for a lot of the boilerplate code needed in J2EE 1.4 development. EJB development has been streamlined so that fewer interfaces are needed, lookup is simpler, and annotations make component definition and resource injection simple and clear.

Managing Persistence

To manage persistence in the application you only need to create a persistence unit, specify which data source and entity manager to use, and then let the container do the work of managing entities and persistence. The container discovers the entity beans at runtime.

You create a persistence unit by defining it in persistence.xml. The project does not contain a persistence.xml yet so you need to create it. The persistence.xml file is created automatically when you add a persistence unit to the project with the New Persistence Unit wizard. The wizard will help you define the properties of the persistence unit.

TIP: You can also create a persistence unit in the New Entity Class wizard. When creating an entity class, the wizard will prompt you to create a persistence unit if one does not exist.

Creating the Entity Classes

When developing for the J2EE 1.4 platform, when you created entity beans you placed them in an EJB module even when the application was a simple web application. Each entity bean required several interfaces, and you had to configure the deployment descriptors in ejb-jar.xml to define the entity beans, the interfaces, persistence and finder queries for each entity bean.

In Java EE 5, instead of creating entity beans and putting them in the EJB module, you can use simple entity classes, and the entity classes can be placed anywhere in a Java EE 5 application. Writing classes for Java EE 5 applications is also easier than it was for the J2EE 1.4 platform because you can use annotations to define components and inject resources.

@Entity annotation is used to declare the class an entity class. Other annotations provide additional information such as the database table and the columns that the entity class and its properties are mapped to.

No hay comentarios: