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

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: