Tips and Tricks
DB Visual ARCHITECT Topics

Create a Partial Table

DB-VA provides the Split Table feature. It allows the user to optimize the size of the database, and minimizes the redundant persistent classes for handling one-to-one identifying relationship.

The following example reverses the phpbb_users table schema to Entity Relationship Diagram, and and splits the phpbb_users table so that user preference and user information are stored in different tables.

The Schema of the phpbb_users table:

# --------------------------------------------------------
#
# Table structure for table 'phpbb_users'
#
CREATE TABLE phpbb_users (
   user_id mediumint(8) NOT NULL,
   user_active tinyint(1) DEFAULT '1',
   username varchar(25) NOT NULL,
   user_password varchar(32) NOT NULL,
   user_session_time int(11) DEFAULT '0' NOT NULL,
   user_session_page smallint(5) DEFAULT '0' NOT NULL,
   user_lastvisit int(11) DEFAULT '0' NOT NULL,
   user_regdate int(11) DEFAULT '0' NOT NULL,
   user_level tinyint(4) DEFAULT '0',
   user_posts mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
   user_timezone decimal(5,2) DEFAULT '0' NOT NULL,
   user_style tinyint(4),
   user_lang varchar(255),
   user_dateformat varchar(14) DEFAULT 'd M Y H:i' NOT NULL,
   user_new_privmsg smallint(5) UNSIGNED DEFAULT '0' NOT NULL,
   user_unread_privmsg smallint(5) UNSIGNED DEFAULT '0' NOT NULL,
   user_last_privmsg int(11) DEFAULT '0' NOT NULL,
   user_emailtime int(11),
   user_viewemail tinyint(1),
   user_attachsig tinyint(1),
   user_allowhtml tinyint(1) DEFAULT '1',
   user_allowbbcode tinyint(1) DEFAULT '1',
   user_allowsmile tinyint(1) DEFAULT '1',
   user_allowavatar tinyint(1) DEFAULT '1' NOT NULL,
   user_allow_pm tinyint(1) DEFAULT '1' NOT NULL,
   user_allow_viewonline tinyint(1) DEFAULT '1' NOT NULL,
   user_notify tinyint(1) DEFAULT '1' NOT NULL,
   user_notify_pm tinyint(1) DEFAULT '0' NOT NULL,
   user_popup_pm tinyint(1) DEFAULT '0' NOT NULL,
   user_rank int(11) DEFAULT '0',
   user_avatar varchar(100),
   user_avatar_type tinyint(4) DEFAULT '0' NOT NULL,
   user_email varchar(255),
   user_icq varchar(15),
   user_website varchar(100),
   user_from varchar(100),
   user_sig text,
   user_sig_bbcode_uid char(10),
   user_aim varchar(255),
   user_yim varchar(255),
   user_msnm varchar(255),
   user_occ varchar(100),
   user_interests varchar(255),
   user_actkey varchar(32),
   user_newpasswd varchar(32),
   PRIMARY KEY (user_id),
   KEY user_session_time (user_session_time)
);

Reverse phpbb_users Table in DBVA-EC

Assume you have been installed DBVA-EC, the DBVA-EC can help you to reverse the database schema to Entity Relationship Diagram.

  1. Create Project PHPbbReverseUser.
    image002.jpg

  2. Select Modeling > ORM > Reverse Database... to open the Database to Data Model dialog box.
    image003.jpg

  3. Select Java language and click Next.
    image004.jpg

  4. Select the database to convert the table schema. Assume that our phpbb is installed in MySQL database or you can select and DBVA-EC support database to connect. Do not set this database setting as default because you will export that phpbb User table to other database.
    image005.jpg

    1. If it is the first time you use DBVA-EC, it may not have the Adapter File for your selected database. You can use the drop down button to select *Download or Browse the most update driver.
      image006.jpg

      MySQL configuration
      Driver file : <<MySQL Connector/J 3.1.10>> (Download using DB-VA EC)
      Driver : MySQL(Connector/J Driver)
      Connection URL : jdbc:mysql://localhost/phpbb
                       (You can change this connection URL to connect to the
                        database with phpbb schema in another machine)


  5. Select the phpbb_users table anc click Next.
    image007.jpg

  6. The Entity Relationship Diagram is created which includes the phpbb_users table.
    image008.jpg

Split the phpbb_user Table

From the ER Diagram, you can observe that the phpbb_users table contains too much information. You can use the split table feature to reduce the size of the phpbb_users table then it can make the developer easier to read the structure of phpbb_uers.

  1. Select the phpbb_users table and right click to select Split Table from the popup menu.
    image009.jpg

  2. Select the readable information to be a new split table members.
    New Partial Table Name : phpbb_users_Info
    Selected Columns       : user_from
                             user_email
                             user_icq
                             username
                             user_password
                             user_lang
                             user_aim
                             user_yim
                             user_msnm
    


    image010.jpg

    The remaining columns in the original table are mainly the preferences of the user in phpbb_users table.

  3. The split table is created in the ER Diagram.
    image011.jpg

  4. Select Modeling > ORM > Sync to Class Diagram to Synchronize the Class Diagram from Entity Relationship Diagram.
    image012.jpg

  5. Select the ORM persistence type.
    image013.jpg

  6. The Class Diagram is created.
    image014.jpg

  7. From the class diagram all the attributes are inside the Phpbb_users class. It means any update to the attributes of the Phpbb_users class will be automatically updated to the corresponding table(master or split).

Generate Classes and Export to Database

  1. Select Modeling > ORM > Generate Code... to open the Database Code Generation dialog box.
    image015.jpg

  2. Select generate Code and Database, Java language and generate Sampe.
    image016.jpg

  3. Click Database tab, select Export to database and click Database Options button to configure the database.
    image017.jpg

  4. Select the database to test the split table feature.
    For example, create testphpbbuser database in MySQL, select the MySQL database and enter the required information.
    image018.jpg

  5. Finally click OK to generate the Phpbb_users Java class.
    image019.jpg

  6. You can see the generated Java classes in DBVA-EC’s Package Explorer.
    image020.jpg

Test Split Table

The generate Java classes include the sample class. You can modify the sample class to test the split table feature of the Phpbb_users.

  1. Insert the new Phpbb_users instance in CreatePHPbbReverseUserData.java.
    public void createTestData() throws PersistentException {
      PersistentTransaction t = PHPbbReverseUserPersistentManager.instance()
        .getSession().beginTransaction();
      try {
        Phpbb_users lUser = Phpbb_usersFactory.createPhpbb_users();
        lUser.setUsername("Edmond");
        lUser.setUser_lang("English");
        lUser.setUser_icq("12345678");
        lUser.setUser_msnm("12345678");
        lUser.setUser_aim("12345678");
        lUser.setUser_yim("12345678");
        lUser.setUser_from("12345678");
        lUser.setUser_email("abc@abc.com");
        lUser.setUser_password("21232f297a57a5a743894a0e4a801fc3");
        		
        //in phpbb_users table attributes
        lUser.setUser_active(true);
        lUser.setUser_timezone(new BigDecimal(323));
        lUser.setUser_dateformat("d M Y h:i a");
        lUser.setUser_posts(10);
        lUser.setUser_sig("Edmond is a nice person");
        lUser.save();
        t.commit();
      } catch (Exception e) {
        t.rollback();
      }
    }


    Source From: src\CreatePHPbbReverseUserData.java
    Notice: Some attributes need to be initialized before Phpbb_users instance call because their corresponding columns do not allow null.

  2. Select the phpbb_users table to read the user perference and select the phpbb_users_info to read the user personal information.
    Select from phpbb_users table:
    SQL statement : SELECT user_active, user_timezone, user_dateformat, user_sig FROM phpbb_users;
    image022.jpg

    Select from phpbb_users_info:
    SQL statement : SELECT * FROM phpbb_users_info;
    image023.jpg

  3. You can see that the Phpbb_users instance information is stored in different tables.

Retrieve the Phpbb_users from Split Tables

You can modify the RetrieveAndUpdatePHPbbReverseUserData.java to test the read the Phpbb_users instance from the split tables.

  1. Modify the RetrieveAndUpdatePHPbbReverseUserData.java
    public void retrieveAndUpdateTestData() throws PersistentException {
      PersistentTransaction t = PHPbbReverseUserPersistentManager.instance()
        .getSession().beginTransaction();
      try {
        Phpbb_users lUser = Phpbb_usersFactory.loadPhpbb_usersByQuery(null, null);
        // Update the properties of the persistent object
        //in phpbb_user_info table attributes
        System.out.println("username : " + lUser.getUsername());
        System.out.println("password : " +lUser.getUser_password());
    		
        //in phpbb_users table attributes
        System.out.println("is user active : " + lUser.getUser_active());
        System.out.println("user dateformat : " + lUser.getUser_dateformat());
        lUser.save();
        t.commit();
      } catch (Exception e) {
        t.rollback();
      }
    }


    Source From: src\RetrieveAndUpdatePHPbbreverseUserData.java

  2. The result of the code executed.
    username : Edmond
    password : 21232f297a57a5a743894a0e4a801fc3
    is user active : true
    user dateformat : d M Y h:i a
    


  3. The data is stored in different tables but it can be retrieved from a single Phpbb_users instance.

Downloads

 
 
Last modified: 2005/12/15 02:33
 
 
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