New Features
Enhanced Features
O/R Mapping
Tips and Tricks
UML Diagrams
VP Suite

Attribute Default Value

In the Entity Relationship Diagram, user can specify the default value for each column. The database set default value to the column when a subsequent INSERT statement omits the value for the column.

Column Default Value

Before create any entity in Entity Relationship Diagram (ERD), you may configure the MySQL database as default database.

  1. Select Tools > Object-Relational Mapping (ORM) > Database Configuration... on the menu.
    config_db_menu.jpg

  2. Select MySQL and enter the database information includes Connection URL, User, etc… and then click OK.
    db_config.jpg

Setting Column Default Value

  1. Create an Entity Relationship Diagram (ERD) and add an Entity called student in the ERD. The student entity contains the following columns with default value and constraint:
    ColumnTypeDefault ValueConstraint
    idintPrimary Key
    namevarchar(255)none
    ageint18
    gendervarchar(1)m

name_col_specification.jpg

student_table.jpg

Reversing Column Default Value

You can reverse the table with default value from database. You will use the following SQL statement to create the staff table in MySQL database.

  1. Execute the “create table” SQL statement on MySQL database.
    CREATE TABLE staff(id int, name varchar(25), mobile varchar(15), drivers_licence varchar(20) DEFAULT 'No', 
    PRIMARY KEY(id))type=InnoDB;


    create_table_mysql.jpg

    the staff table only the drivers_licence column has default value ‘No’.

  2. Open new project in DBVA. Select Tools > Object-Relational Mapping (ORM) > Reverse Database… on the menu.
    reverse_db_menu.jpg

  3. Select Java language and click Next>.
    wizard_reverse_1.jpg

  4. Configure the MySQL database and click Next>.
    wizard_reverse_2.jpg

  5. Select the staff table and click Finish to reverse.

    wizard_reverse_3.jpg

  6. Check the default value in drivers_licence column is reversed from the database.
    view_reverse_dv1.jpg

    view_reverse_dv2.jpg

Synchronizing Default Value

The DBVA can help user to reverse the default value from database to the ERD column. The default value on the column also can synchronize to be an initial value for the corresponding attribute in Class Diagram.

  1. Reopen the project create on the Setting Column Default Value section(contain student entity).
    erd_student.jpg

  2. Select Tools > Object-Relational Mapping (ORM) > Synchronize to Class Diagram on menu.
    sync_class_diagram_menu.jpg

  3. The Class Diagram is created and contains the class call Student. Each attributes have initial value. The initial value will be assign for the attribute in generate source code.
    class_diagram_student.jpg

    When you modify the default value on the ERD, you can synchronize to Class Diagram again then the initial value will follow the default value to modify but you have modified the initial value and synchronize from ERD to Class Diagram, DBVA detect the conflict between the initial value and default value and show the Change initial value dialog to request user make decision to update the initial value or not.
    change_initial_value_dialog.jpg

Generating Code and DDL

The default database is configured at the beginning of the document. After Synchronize to Class Diagram, you can generate the persistable code and DDL file.

  1. Select Tools > Object-Relational Mapping (ORM) > Generate Code… on the menu.
    generate_dialog_menu.jpg

  2. Select Code and Database for Generate option and Java for Language option. Select the output path for the generate persistable Java code and DDL file.
    generate_dialog_code_config.jpg

  3. Click Database tab, select Create Database for Generate Database option and select Export to database and Generate DDL options. Finally, click OK button to start generate Java code and DDL file to output path.
    generate_dialog_db_config.jpg

  4. From attributedefault.ddl file. All the default value is created for column in the create table statement.
    DROP TABLE IF EXISTS `student`;
    CREATE TABLE `student` (`id` int NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT 'none', 
    `age` int DEFAULT 18, `gender` varchar(1) DEFAULT 'm', PRIMARY KEY (`id`)) type=InnoDB;


  5. From generated Student Class. You can observer that the initial value is set for corresponding attribute.
public class Student { 
...
   private int id;
 
   private String name = "none";
	
   private Integer age = new Integer(18);
	
   private String gender = "m";
...
}

Downloads


Resources

 
 
Last modified: 2006/06/19 14:52
 
 
Home | Recent Topics | Highlights | UML Diagrams | Tips and Tricks | Object-Relational Mapping
visual-paradigm.com Home | Training Center | UML Center | VP Gallery | Discussion Forum | UML Open Directory