TS 71 Entity Classes From Database

Test Specification of Entity Classes from Database Wizard

Author: Michal Vanek
Version: NetBeans 7.1
Last update: 29-th September 2011
Comments: MySQL server with Sakila database scheme needed for tests

Contents


Test suite: "Sakila database connection on localhost"

Purpose: "create Sakila database connection "

  1. "Creating jdbc/sakila connection"
    1. "Start mySQL server if not already running"
    2. "Download and create Sakila DB if needed - http://downloads.mysql.com/docs/sakila-db.zip"
    3. "Use File | New Project... to create new Web Application project located in your home directory."
    4. "Choose your own project name and click next"
    5. "If not chosen automatically, choose Glassfish v3 and Java EE5, click next"
    6. "Right click project name | New | Other | choose Persistence - Persistent Unit and click next"
    7. "Choose Data source | New Data source | Database connection | New Database connection "
    8. "Name: enroll for MySQL (Connector/Jdriver)"
    9. "Host:localhost"
    10. "Port: 3309"
    11. "Database: sakila"
    12. "User Name: usrname"
    13. "Password: password"

File:mysqlSettings_TS_65_Entity_Classes_From_Database.jpg

    1. "Click OK - connection is created, no error appears"
    2. "JNDI Name: jdbc/sakila and click OK"
    3. "Right click project name | New | Other | choose Persistence - Entity Class from Database and click next"
    4. "Select Data Source: jdbc/sakila"
    • EXPECTED RESULT: "Sakila tables are displayed in Available Tables area."


Test suite: "Not related database tables and not included relationship"

Purpose: "Verification single tables creation"
Setup: "Create Simple web application, create Persistent Unit with connection to MySQL database."

  1. "New Entity Class from Database - Single table, Include Related Tables checked, "
    1. "Right click project name | New | Other | choose Persistence - Entity Classes from Database and click next"
    2. "Select Data Source: from Drop down jdbc/sakila "
    3. "Make sure Include Related Tables button is checked"
    4. "From Available Tables: choose any table without other relations and Add to selected Tables. For example - Actor and click next"
    5. "Make sure Generate Query Anotations for Persistent Fields is checked"
    6. "Choose web package from drop down and click next "
    7. "Choose Association Fetch: default"
    8. "Collection type: java.util.collection"
    9. "Check boxes are unchecked and click finish"
    10. "New java file named by the table is created in web package."
    11. "Clean and Build a project"
    12. "Repeat all the steps for all Association Fetch types and all Collection Types"
    • EXPECTED RESULT: "For each case new java file with table is created and project Build is successful. Fetch type is not imported."
This is not imported in generated code - import javax.persistence.FetchType;

Code created by wizard in a java file is similar to this one

@Entity
@Table(name = "actor")
@NamedQueries({
    @NamedQuery(name = "Actor.findAll", query = "SELECT a FROM Actor a"),
    @NamedQuery(name = "Actor.findByActorId", query = "SELECT a FROM Actor a WHERE a.actorId = :actorId"),
    @NamedQuery(name = "Actor.findByFirstName", query = "SELECT a FROM Actor a WHERE a.firstName = :firstName"),
    @NamedQuery(name = "Actor.findByLastName", query = "SELECT a FROM Actor a WHERE a.lastName = :lastName"),
    @NamedQuery(name = "Actor.findByLastUpdate", query = "SELECT a FROM Actor a WHERE a.lastUpdate = :lastUpdate")
})
public class Actor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "actor_id")
    private Short actorId;
    @Basic(optional = false)
    @Column(name = "first_name")
    private String firstName;
    @Basic(optional = false)
    @Column(name = "last_name")
    private String lastName;
    @Basic(optional = false)
    @Column(name = "last_update")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastUpdate;

  1. "New Entity Class from Database - Relationship table, Include Related Tables unchecked "
    1. "If necessary create new simple web aplication with Persistent Unit and connection to MySQL database"
    2. "Right click project name | New | Other | choose Persistence - Entity Classes from Database and click next"
    3. "Select Data Source: from Drop down jdbc/sakila "
    4. "Make sure Include Related Tables button is unchecked"
    5. "From Available Tables: choose table which has any relationship to other tables and Add to selected Tables. For example - Actor and click next"
    6. "Only one table is added to Selected tables field"
    7. "Make sure Generate Query Anotations for Persistent Fields is checked"
    8. "Choose web package from drop down and click next "
    9. "Choose Association Fetch: default"
    10. "Collection type: java.util.collection"
    11. "Check boxes are unchecked and click finish"
    12. "New java file named by the table is created in web package."
    13. "Clean and Build a project"
    14. "Repeat all the steps for all Association Fetch types and Collection Types"
    • EXPECTED RESULT: "For each case new java file with table is created and project Build is successful"
this code is not present in a java file
@ManyToOne(optional=false), @OneToMany(Cascade...


Test suite: "Related database tables "

Purpose: "Verification of creation correct relationship between tables"
Setup: "Create Simple web application, create Persistent Unit with connection to MySQL database."

  1. "Related tables - java.util.Collection"
    1. "If necessary create new simple web aplication with Persistent Unit and connection to MySQL database"
    2. "Right click project name | New | Other | choose Persistence - Entity Classes from Database and click next"
    3. "Select Data Source: from Drop down jdbc/sakila "
    4. "Make sure Include Related Tables button is checked"
    5. "Add any table which is related to other tables(customer) and click next"
    6. "Choose web package and click next"
    7. "Choose Association Fetch: Default"
    8. "Java files corresponding to table names are created"
    9. "Build project is successful"
    10. "Repeat for all Association Fetch types"
    • {{result|EXPECTED RESULT: "Build project is successful. Collection is used to create related tables. Java files contains this code "
Java file with main table contains in class definition:   
@ManyToOne(optional = false)

Other java files with related tables contains in class definition:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "someID")
private Collection<Customer> customerCollection; 

If fetch type is different from default, it is inserted in @ManyToOne and @OneToOne option
for example:
 @OneToMany(cascade = CascadeType.ALL, mappedBy = "film", fetch = FetchType.EAGER)
 @ManyToOne(optional = false, fetch = FetchType.LAZY)
  1. "Related tables - java.util.List"
    1. "If necessary create new simple web aplication with Persistent Unit and connection to MySQL database"
    2. "Right click project name | New | Other | choose Persistence - Entity Classes from Database and click next"
    3. "Select Data Source: from Drop down jdbc/sakila "
    4. "Make sure Include Related Tables button is checked"
    5. "Add any table which is related to other tables(customer) and click next"
    6. "Choose web package and click next"
    7. "Choose Association Fetch: Default"
    8. "Java files corresponding to table names are created"
    9. "Build project is successful"
    10. "Repeat for all Association Fetch types"
    • EXPECTED RESULT: "Build project is successful. List is used to create related tables.
Java files contains this code "
Java file with main table contains in class definition:   
@ManyToOne(optional = false)

Other java files with related tables contains in class definition:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "someID")
private List<Customer> customerCollection;

If fetch type is different from default, it is inserted in @ManyToOne and @OneToOne option
for example:
 @OneToMany(cascade = CascadeType.ALL, mappedBy = "film", fetch = FetchType.EAGER)
 @ManyToOne(optional = false, fetch = FetchType.LAZY)
  1. "Related tables - java.util.Set"
    1. "If necessary create new simple web aplication with Persistent Unit and connection to MySQL database"
    2. "Right click project name | New | Other | choose Persistence - Entity Classes from Database and click next"
    3. "Select Data Source: from Drop down jdbc/sakila "
    4. "Make sure Include Related Tables button is checked"
    5. "Add any table which is related to other tables(customer) and click next"
    6. "Choose web package and click next"
    7. "Choose Association Fetch: Default"
    8. "Java files corresponding to table names are created"
    9. "Build project is successful"
    10. "Repeat for all Association Fetch types"
    • EXPECTED RESULT: "Build project is successful. Set is used to create related tables.
Java file with main table contains in class definition:   
@ManyToOne(optional = false)

Other java files with related tables contains in class definition:
@OneToMany(cascade = CascadeType.ALL, mappedBy = "someID")
private Set<Customer> customerCollection;

If fetch type is different from default, it is inserted in @ManyToOne and @OneToOne option
for example:
 @OneToMany(cascade = CascadeType.ALL, mappedBy = "film", fetch = FetchType.EAGER)
 @ManyToOne(optional = false, fetch = FetchType.LAZY)


Test suite: "New Entity Class from Database - Database table wizard window "

Purpose: "Verification of adding removing related tables "
Setup: "Create Simple web application, create Persistent Unit with connection to MySQL database. Add New Entity Class from Database and navigate to Database tables step in wizard"

  1. "Remove not related table"
    1. "Add any table which is related to other tables"
    2. "Other related tables are added to Selected Tables"
    3. "Select previously selected table and click remove"
    • EXPECTED RESULT: "All tables are removed from Selected Tables"
  2. "Removing related table"
    1. "Add any table which is related to other tables"
    2. "Other related tables are added to Selected Tables"
    3. "Select any related table and try click remove button"
    • EXPECTED RESULT: "Removed button is disabled. No tables are removed"
  3. "Add all button"
    1. "Click Add All button"
    • EXPECTED RESULT: "All tables are added to Selected Tables"
  4. "Remove all button"
    1. "Click Add All button"
    2. "Click Remove All button"
    • EXPECTED RESULT: "All tables are removed from Selected Tables to Available tables"
  5. "Checked box Generate Named query Annotations for Persistent Fields"
    1. "Add any not related table into Selected tables and click next "
    2. "Make sure - Generated Named Query Annotations for Persistent Fields check box is checked"
    3. "Click Finish"
    4. "Open new generated file"
    • EXPECTED RESULT: "NamedQueries are generated in a java file"
Source file contains similar code:
@NamedQueries({@NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a");
  1. "Unchecked box Generate Named query Annotations for Persistent Fields"
    1. "Add any not related table into Selected tables and click next "
    2. "Make sure - Generated Named Query Annotations for Persistent Fields check box is unchecked"
    3. "Click Finish"
    4. "Open new generated file"
    • EXPECTED RESULT: "No NamedQueries are generated in a java file"
Source file does not contains similar code:
@NamedQueries({@NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a");
  1. "Fully Qualified Database Table Names - checked"
    1. "Add any table which is related to other tables"
    2. "Navigate to step - Mapping Options"
    3. "Check Fully Qualified Database Table Names and click Finish"
    • EXPECTED RESULT: "Database Table name is generated in a source file"
Example of generated Table name and other attributes in the header of the file:

@Entity
@Table(name = "actor", catalog = "sakila", schema = "") 
  1. "Attributes for Regenerating Tables - checked"
    1. "Add any table which is related to other tables"
    2. "Navigate to step - Mapping Options"
    3. "Check Attributes for Regenerating Tables check box"
    4. "Uncheck Fully Qualified Database Table Names check box and click Finish"
    • EXPECTED RESULT: "Attributes for Regenerating Table are generated in a source file"
Example of generated attributes in the header of the file:

@Entity
@Table(name = "country")

Test suite: Datasource Code Completion

  1. Table and column code completion
    1. Open entity class in editor
    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
    9. Go to services tab, disconnect from database and remove the connection
    10. Check that project is badged with broken datasource icon (bug 202741, bug 202742)
    11. Click Resolve Data Source Problem... in the project's popup menu
    12. Resolve the datasource by adding a connection and check project status is refreshed
  2. Persistence unit code completion
    1. Create second persistence unit in this project
    2. Open new file wizard and choose "Enterprise JavaBeans|Session Beans for Entity Classes"
    3. Finish the wizard
    4. In opened Facade file put cursor at @PersistenceContext(unitName = "|, invoke code completion and check both persistence unit names are offered
    5. Select second persistence unit and check it is inserted into the code
    • EXPECTED RESULT: All steps finished successfully.


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