Five ‘s Weblog

October 9, 2007

Embedded Value

Filed under: Five's thought — by powerdream5 @ 1:18 am
Tags: ,

       The word of Embedded Value is used widely, and it has different meanings for different areas. To be honest, Embedded Value is more popular in insurance industry than in J2EE field. When we search it by google, most of the returning result about its definition about insurance. So today I am going to present the use of Embedded Value in J2EE. For more information about it, please refer to Martin Flower’s book –  <<Patterns of Enterprise Application Architecture>>

8_3

         In J2EE, Embedded value is a pattern of ORM(Object Relational Mapping). Martin Flower define it as mapping an object into several fields of another object’s table. I think an example will help you understand it more clearly.
         There are two persistent classes: ShippingItem and Address. The class of ShippingItem has an reference to the class of Address, as the following shows:

public class ShippingItem{
         private long shippingId;
         private String itemName;
         private Date shippingDate;
         private Address destination;
        ….omit the method….       
}

public class Address{
       private String street;
       private String city;
       private String state;
       private String postcode;
       ….omit the method….   
}

       Usually, we can mapping the two classes into their own tables:

8_1

       However, we can see that the relationship between the shipping Items and their addresses is one to one. Furthermore, the addresses table only stores the address of the shipping items, it doesn’t sotre the address of other elements. If we mapping the two classes into two tables, when wanting to search the whole information about the shipping items, we have to use use “join”.
//using inner join here
select s.*, a.*
from shippingItems s, addresses a
where s.shippingId = a.shippingId;

      As we all know, the manipulation of “join” will influence the performance of the application. Therefore, why do we not map the two classes into only one table.

8_2

       Now, when we want to get the whole informaiton about the shipping items, just using the following simple sql sentence is ok, and it has better permormance that the one mentioned above.
select s.* from shippingItems s;

       At last, let’s make a conclusion of Embedded value. As the example shows, when some small objects make sense in an OO system that don’t make sense as tables in a database, we need to use Embedded value to map the values of the object to fields in the record of the object’s owner.

8_4

October 5, 2007

Find balance among technologies!

Filed under: Five's thought — by powerdream5 @ 11:57 pm
Tags: , , ,

         Whenever we develop a new application, how to design and access the database is always one of the most significant parts we need to consider. In fact, there are many best practices for us to optimize the performance of our databases. However, if you compare these best practices, you may find that these best practices are not consistent with each other. In other words, you cannot apply all the best practices in one database. I will show two examples to explain this: 

jdbc

          The first example: choosing between JDBC(iBATIS) and ORM framework.
          In the recent years, ORM framework, such as Hibernate and JDO, is more and more popular in the J2EE field. No matter how popular it is, ORM framework still has some drawbacks. The performance of using ORM tools to access the database is not as good as using JDBC, furthermore, the technology of JDBC is more mature. However, using ORM tools get us out of the complexity of the SQL, and it also makes the application more portable. Therefore, in most cases, ORM framework is our first choice, but it not absolute. Of course, when you decide to use a ORM tool, it means you need to study it firstly, which would prevent some programmer from using the ORM tool, because they know JDBC and SQL pretty well and it seems that they are wasting their time to study another technology to access the database, in addition, ORM framework is not good enough!

            The second example: find balance between normalization and denormalization
            As we all know, when we design our database system, the process normalization helps us to reduce the redundant data in our database. However, the disadvantage of normalization is seprating the related data into differenct tables, which means that when you search those data, you need use a lot of “join” manipulations. That will influences the performance of the application. Therefore, in order to make up the performance, sometimes, we have to take the step of denormalization. The problem is that normalization and denormalization are totally contrast to each other. Each time when we design our databses, there is no doubt that we should use 1NF, 2NF and 3NF to normalize the database. But when should we take the process of denormalization? I cannot answer this question, because I think that depends on your experience.

chinsescoins.jpg

            In a word. each technology has its own pros and cons. Do not stick to one, and choose them according to the specific problem.

Blog at WordPress.com.