TS 73 Persistence

Persistence Test Specification

Author: Jiri Skrivanek
Version: NetBeans 7.3.1
Last update: 14-th May 2013
Bugzilla: javaee/Persistence
Introduction: This test specification is focused on testing persistence support in NetBeans IDE. It is based on outdated TS_72_Entity_Classes_From_Database, TS_61_JSFPages_from_Entity_Classes and TS_72_JPA_Support test specifications.

Contents


Test suite: Entity Classes from Database

  1. Select Tables
    1. Create new web project with GlassFish server
    2. Open new file wizard and select Persistence|Entity Classes from Database
    3. Choose jdbc/sample in Data Source combo box
    4. Select CUSTOMER table and click Add >
    5. Uncheck Include Related Tables and tables DISCOUNT_CODE and MICRO_MARKET should be removed from selected tables
    6. Check Include Related Tables and mentioned tables are back in selected tables
    7. Click << Remove All to empty selected tables
    8. Click Add All >> to select all tables
  2. Fetch type EAGER, java.util.List and defaults
    1. Click Next
    2. In Entity Classes page provide package name and click Next
    3. In Mapping Options page change Association Fetch to eager, Collection Type to java.util.List and click Finish
    4. Open DiscountCode.java and check
      1. EAGER and List is used
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode", fetch = FetchType.EAGER)
        private List<Customer> customerList;
      2. Three named queries DiscountCode.findAll, DiscountCode.findByDiscountCode and DiscountCode.findByRate are generated
      3. JAXB annotations @XmlRootElement and @XmlTransient are present
      4. Table name is not fully qualified
        @Table(name = "DISCOUNT_CODE")
      5. Attributes for regenerating tables are not used
        @Column(name = "DISCOUNT_CODE")
        private Character discountCode;
        @Column(name = "RATE")
        private BigDecimal rate;
      6. Annotation name = "RATE" is present because it doesn't use defaults
    5. Open Customer.java and check
      1. Column name zip is used
        private MicroMarket zip;
    6. Remove all classes.
  3. Fetch type LAZY, java.util.Set and non defaults
    1. Right-click package node and choose New|Entity Classes from Database
    2. Click Add All >> to select all tables and click Next
    3. Uncheck Generate Named Query Annotations for Persistent Fields, Generate JAXB Annotations and click Next
    4. Change Association Fetch to lazy , Collection Type to java.util.Set
    5. Select check box Fully Qualified Database Table Names, Attributes for Regenerating Tables, Use Defaults if Possible and Generate Fields for Unresolved Relationships
    6. Uncheck Use Column Names in Relationships
    7. Click Finish
    8. Open DiscountCode.java and check
      1. LAZY and Set is used
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode", fetch = FetchType.LAZY)
        private Set<Customer> customerSet;
      2. Only DiscountCode.findAll named query is generated
      3. JAXB annotations @XmlRootElement and @XmlTransient are missing
      4. Table name is fully qualified
        @Table(name = "DISCOUNT_CODE", catalog = "", schema = "APP")
      5. Attributes for regenerating tables are used
        @Column(name = "DISCOUNT_CODE", nullable = false)
        private Character discountCode;
        @Column(precision = 4, scale = 2)
        private BigDecimal rate;
      6. Annotation name = "RATE" is missing because it uses defaults
    9. Open Customer.java and check
      1. Column name zip is not used
        private MicroMarket microMarket;
    10. Remove all classes.
  4. All defaults
    1. Right-click package node and choose New|Entity Classes from Database
    2. Click Add All >> to select all tables and click Next
    3. Keep all defaults and click Next
    4. Keep all defaults and click Finish
    5. Open DiscountCode.java and check
      1. Default/none fetch type and Collection is used
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "discountCode")
        private Collection<Customer> customerCollection;
      2. Three named queries DiscountCode.findAll, DiscountCode.findByDiscountCode and DiscountCode.findByRate are generated
      3. JAXB annotations @XmlRootElement and @XmlTransient are present
      4. Table name is not fully qualified
        @Table(name = "DISCOUNT_CODE")
      5. Attributes for regenerating tables are not used
        @Column(name = "DISCOUNT_CODE")
        private Character discountCode;
        @Column(name = "RATE")
        private BigDecimal rate;
      6. Annotation name = "RATE" is present because it doesn't use defaults
    6. Open Customer.java and check
      1. Column name zip is used
        private MicroMarket zip;
  5. DB Scripts from Entity Classes
    1. Open new file wizard and select Persistence|DB Scripts from Entity Classes
    2. Click Next
    3. Provide package name and click Next
    4. Click Finish
    5. SQL script should be opened in editor with CREATE TABLE and ALTER TABLE commands

Test suite: JSF Pages from Entity Classes

  1. Create JSF Pages
    1. Open new file wizard
    2. Select Persistence|JSF Pages from Entity Classes and click Next
    3. Click Add All >> to select all previously created entity classes and click Next
    4. Leave defaults unchanged in Generate JSF Pages and Classes panel and click Next
    5. Check that JavaServer faces configuration panel is displayed and click Finish
    6. Run project and wait until index page is shown in browser
    7. Verify that pages for all tables are available, it is possible to list items, delete item, edit item, create new item

Test suite: Java Persistence Query Language Hints and Code Completion

  1. Hints
    1. Open Customer.java
    2. Go to Customer.findByName named query
    3. Change SELECT to ELECT
    4. Check that hint "The query does not start with a valid identifier, has to be either SELECT..." appears
    5. Revert changes and verify that hint disappears
    6. Change Customer to ustomer
    7. Check that hint "The abstract schema ustumer is unknown." appears
    8. Revert changes and verify that hint disappears
    9. Change c.name to c.nam
    10. Check that hint "The state field path 'c.nam' cannot be resolved to a valid type." appears
    11. Revert changes and verify that hint disappears
  2. Named Query Code Completion
    1. Open Customer.java
    2. Insert new line behind @NamedQueries({ and paste empty named query definition there
      @NamedQuery(name = "Customer.find1", query = ""),
    3. Place cursor at empty query and invoke code completion (Ctrl+Space)
    4. Type S and Enter to choose SELECT between DELETE FROM, SELECT and UPDATE
    5. Type Space, c, Space and invoke code completion
    6. Choose FROM, type Space and invoke code completion
    7. Select Customer, type Space, c, Space and invoke code completion
    8. Choose WHERE, type Space, c. and invoke code completion
    9. Select city, type Space and invoke code completion
    10. Choose IS NOT NULL
    11. Resulting named query looks like this
      SELECT c FROM Customer c WHERE c.city IS NOT NULL
    12. Save file (Ctrl+S)
  3. Code Completion in Persistence Annotations
    1. Open Customer.java
    2. Put cursor behind @Table(name = ", invoke code completion and check all tables from database are offered in code completion
    3. Select a table, press Enter and check it is inserted into the code
    4. Put cursor behind @Column(name = ", invoke code completion and check all columns from particular table are suggested in code completion
    5. Select a column, pres Enter and check it is inserted into the code
    6. Go to Services tab and disconnect from database
    7. Try code completion and there should be only "Connect To Database"
    8. Choose it and check it is connected to database and next attempt to use code completion shows tables or columns from database
  4. Persistence Unit Code Completion
    1. Open Configuration Files|persistence.xml
    2. Click Add button at top-right corner
    3. Confirm New Persistence Unit dialog and save the file
    4. Open CustomerFacade.java
    5. Put cursor behind @PersistenceContext(unitName = " and invoke code completion
    6. Check that two persistence units are available
  5. Code Completion in createQuery
    1. Open CustomerFacade.java
    2. Insert empty query definition
      public void q() {
      em.createQuery("");
      }
    3. Place cursor at empty query and invoke code completion (Ctrl+Space)
    4. Type S and Enter to choose SELECT between DELETE FROM, SELECT and UPDATE
    5. Type Space and invoke code completion
    6. Choose OBJECT, type (o), Space and invoke code completion
    7. Choose FROM, type Space and invoke code completion
    8. Select Customer, type Space and invoke code completion
    9. AS should be automatically added as the only option
    10. Type Space, o and invoke code completion
    11. Choose WHERE, type Space, o. and invoke code completion
    12. Select city, type Space and invoke code completion
    13. Choose IS NOT NULL
    14. Resulting named query looks like this
      SELECT OBJECT(o) FROM Customer AS o WHERE o.city IS NOT NULL
    15. Save file (Ctrl+S)
  6. Code Completion in createNamedQuery
    1. Insert new line in previously created method
    2. Paste empty named query definition
      em.createNamedQuery("");
    3. Place cursor at empty query and invoke code completion (Ctrl+Space)
    4. List of all named queries is offered
    5. Pick one and confirm by hitting Enter
    6. While pressing Ctrl move cursor over the inserted named query and click on it
    7. Cursor is moved to class with declaration of selected named query

Test suite: Java Persistence Query Language Editor

  1. JPQL Editor
    1. Build project
    2. Go to Configuration Files
    3. Right-click persistence.xml and choose Run JPQL Query
    4. Place cursor inside editor and invoke code completion (Ctrl+Space)
    5. Type S and Enter to choose SELECT between DELETE FROM, SELECT and UPDATE
    6. Check a hint about invalid query in bottom part of the editor (SQL button pressed)
    7. Type Space, c, Space and invoke code completion
    8. Choose FROM, type Space and invoke code completion
    9. Select Customer, type Space, c, Space and invoke code completion
    10. Choose WHERE, type Space, c. and invoke code completion
    11. Select city, type Space and invoke code completion
    12. Choose IS NOT NULL
    13. Resulting named query looks like this
      SELECT c FROM Customer c WHERE c.city IS NOT NULL
    14. Click run button in toolbar
    15. Results should be listed in the bottom and the Result button should be pressed
    16. Click the SQL button and check SQL query
      SELECT CUSTOMER_ID, ADDRESSLINE1, ADDRESSLINE2, CITY, CREDIT_LIMIT, EMAIL, FAX, NAME, PHONE, STATE, DISCOUNT_CODE, ZIP FROM CUSTOMER WHERE NOT ((CITY IS NULL))
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