Vw jpa binding

Visual Web Database Binding Using JPA

Modification History:

  • Nov 14, 2007 - Winston Prakash



Currently the primary database binding support in visual web is via CachedRowset. For Netbeans 6.0 JPA binding support is indirectly supported via manual steps. See Using Java Persistence API Within a Visual Web Application for details. The Woodstock Table Component and its design time has been enhanced to support this functionality. However, most of the work need to be manually done by the user. This proposal aims at providing an automated drag and drop JPA binding support for NB 6.1.

JPA or CachedRowset binding

Supporting JPA doesn't mean support for CachedRowset disappears. We should continue to support binding via CachedRowset as well as JPA. It is up to the user to select one of the methodology. User should be able to set the prefered methodology via Netbeans Options dialog as shown below. The default should be JPA binding.


Drag and Drop CachedRowset binding

In case of CachedRowset binding when the database table is dragged and dropped over the design page, Visual Web Designtime creates two objects.

  • A CachedRowset (Usually in the SessionBean Context)
  • A CachedRowsetDataProvider (In the same context where the drop occurs, usually the Page)

Corresponding code is injected in to the two contexts (Ex. Page1.java and SessionBean1.java).

If the database table is dropped over a component, Visual Web Design Time System gives an opportunity to the Component Designtime (DesignInfo) to bind the CachedRowsetDataProvider to that component. Necessary component modification to bind the DataProvider takes place and the corresponding code is injected in to the JSP or Java.

You can read about how and when the above two objects are created at Visual Web Datasource Consumer

In short,

  • Designer receives the database table Drag and Drop
  • Designer delegates the process of consuming the database drag and drop to DatasourceTransferManager defined in the Visual Web DataConnectivity module.
  • DatasourceTransferManager creates a BeanCreateInfoSet Object (stuffing the data connection information to it) and pass it back to the designer.
  • Designer gets the "beanClass" (which is CachedRowetdataProvider) from BeanCreateInfoSet and creates an instance of it in the page context. This adds the source corresponding to CachedRowetdataProvider in the page
  • Designer calls the method BeanCreateInfoSet.beanCreatedSetup(). This method creates the CachedRowset in the SessionBean context and set the connection properties to it and then sets the CachedRowset to the dataprovider. These actions creates the corresponding code in the Session Bean and Page Bean.

Note: BeanCreateInfoSet is a designtime API interface.

Manual JPA binding

In netbeans 6.0 for manual binding the following steps are needed.

  • Create the corresponding entity class representing the Users table. This is done via the action New -> Entity Classes from Database.
  • Create the persistence Unit. This is also done as part of creating the entity class step. The "Entity Classes from Database", provides the option to create the persistence Unit.
  • Create the Entity Controller Class. The controller class provides the interface to fetch the collection of entity beans corresponding to the number of rows in the database. The Query String using JPQL (Java Persistence Query Language) is added to the Controller class to fetch the required data from the database.
  • Create a property in the Session Bean that returns either a List or Array of Entity Bean Objects obtained via Entity Controller class.
  • Finally via Table Layout bind the List or Array of Entity objects to the Table Component.

Note: Currently, in visual Web only Table Component allows direct binding to a List or Array. All other components requires a ListObjectDataprovider or ArrayObjectDataProvider for binding.

Drag and Drop JPA binding

For drag and drop JPA binding all the above steps should be done automatically. To achieve this, we may have to do the following modification to the steps explained above for consuming the database table drag and drop using CahcedRowset methodology.

After DatasourceTransferManager receives the request from designer to consume database DnD, it should do the following

  • Get the option setting by the user for database binding type.
  • If binding type is "Use CachedRowet", then proceed as before and consume using CachedRowset methodology.
  • If binding type is "Use JPA", display the dialog "New Entity Class from database". Required API:
  • Set Database Connection
  • Set Selected Table
  • Get generated Source Path
  • Create a BeanCreateInfoSet Object and pass in the Class generated for the Entity Bean.

Designer gets the BeanCreateInfoset and invoke the following call back methods.

  • GetClassName(). The class returned should be of type List<EnityBeanClass>
  • beanCreatedSetup(). During this call the following steps should be performed
  • Add context method to create controller (Entity manager)
  • Add initialization code to initialize the list with the collection of entity beans obtained from the controller.
  • Formalize the JPQL Query and set to the controller in the initialization code.

Things to explore

  • When the entity bean is generated the Java source need to be compiled and available in the Class Path for insync to operate on it. Possibly we could invoke "Compile Single" ant task to compile the generated entity class.
  • Components do not yet can bind directly to collection of POJO's. This need to be resaerched and the Component designtime need to be modified accordingly.
  • It appears to easy to bind to components of type Input and Output as JSF 1.2 directly support binding to List of POJO object (Ex: #Page1.myList 0.name)
  • Components of type Selector (Ex. List and DropDown) needs data of type javax.faces.model.SelectItem
  • Even though Table Layout customizer supports binding to Object List and Object Array, it still does not support drag and drop these object on to it.

Ideas from Sandip:

  • Add context menu item "Create JPA Context" to the database connection. This creates a Java Library with all Entity beans corresponding to the tables in the connection and corresponding controller and some metadata defining the entities (similar to Web Service)
  • Display a subnode "JPA" in the Database Connection which displays the list of Entity Bean.
  • User drag and drops the Entity Bean on to the component. This gesture does the following
  :* Adds the JPA library to the project
  :* Does the necessary binding
  :* Adds the Persistenace Unit from the meta-data information.
Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo