<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JavaBeat &#187; Uncategorized</title>
	<atom:link href="http://www.javabeat.net/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javabeat.net</link>
	<description>Java Technology News</description>
	<lastBuildDate>Tue, 21 May 2013 13:43:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Examining Functional Concepts in Google Collections using Scala</title>
		<link>http://www.javabeat.net/2011/06/examining-functional-concepts-in-google-collections-using-scala/</link>
		<comments>http://www.javabeat.net/2011/06/examining-functional-concepts-in-google-collections-using-scala/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 17:25:31 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2392</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on Scala in Action , to be published on Fall 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><a id="dd_start"></a><p><i>This article is based on <a href="http://www.manning.com/raychaudhuri/" target="_blank">Scala in Action</a> , to be published on Fall 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <font color="red"><u><b>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</b></u> </font>]</i></p>
<h1><center>Examining Functional Concepts in Google Collections</center></h1>
<h2>Introduction</h2>
<p>The Google Collections API adds a lot of power to the standard Java collections. It brings a nice set of efficient immutable data structures and some functional ways of interacting with your collections, primarily the Function interface and the Predicate interface. These interfaces are used from the Iterables and Iterators classes. Let’s take a look at the Predicate interface (listing 1) and its uses.</p>
<p><b><font color="red">Listing 1 Google Collections Predicate interface</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">interface Predicate {
		public boolean apply(T input);	 				#A
		public boolean equals(Object other);
	}
	#A Matches Function1.apply</pre></td></tr></table></div>

</p>
<p>The predicate interface is rather simple. Besides equality, it contains an apply method, which returns true or false against its argument. This is used in Iterators/Iterables filter method. The filter method takes a collection and a predicate. It returns a new collection containing only elements that pass the predicate apply method. Predicates are also used in the find method. The find method looks in a collection for the first element passing a Predicate and returns it.</p>
<p>The filter and find method signatures are shown in listing 2.</p>
<p><b><font color="red">Listing 2 Iterables filter and find methods</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">class Iterables {
		public static &lt;T&gt; Iterable&lt;T&gt; filter(Iterable&lt;T&gt; unfiltered,
		Predicate&lt;? super T&gt; predicate) {...} 				#A
		public static &lt;T&gt; T find(Iterable&lt;T&gt; iterable,
		Predicate&lt;? super T&gt; predicate) {...} 				#B
		...
	}
	#A Filters using predicate
	#B Finds using predicate</pre></td></tr></table></div>

</p>
<p>There is also a Predicates class that contains static methods for combining predicates (ands/ors) as well as standard predicates for use, such as not null. This simple interface creates some powerful functionality through the potential combinations that can be achieved with very terse code. Also, since the predicate itself is passed into the filter function, the function can determine the best way or time to execute the filter. The data structure may be amenable to lazily evaluating the predicate, thus making the iterable return a “view” of the original collection.</p>
<p>It might also determine that it could best optimize the creation of the new iterable through some form of parallelism. The fact is that this has been abstracted away, so the library could improve over time with no code changes on our part.</p>
<p>The Predicate interface itself is rather interesting because it looks like a very simple function. This function takes some type T and returns a Boolean. In Scala, this would be represented as T => Boolean. Let’s rewrite the filter/find methods in Scala and see what their signatures would look like (listing 3).</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><b><font color="red">Listing 3 Iterables filter and find methods in Scala</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">object Iterables {
		def filter[T](unfiltered : Iterable[T],
		predicate : T =&gt; Boolean) : Iterable[T] = {...} 		#A
		def find[T, U :&gt; T](iterable : Iterable[T], predicate : U =&gt; Boolean) : T = {...}
		...
	}
	#A No need for ?</pre></td></tr></table></div>

</p>
<p>You’ll immediately notice that in Scala we aren’t using any explicit ? super T type annotations. This is because the Function interface in Scala is appropriately annotated with covariance and contravariance. Covariance (+T or ? extends T) is when a type can be coerced down the inheritance hierarchy. Contravariance (-T or ? super T) is when a type can be coerced up the inheritance hierarchy. Invariance is when a type cannot be coerced at all. In this case, a Predicate’s argument can be coerced up the inheritance hierarchy as needed. This means, for example, that a predicate against mammals could apply to a collections of cats, assuming a cat is a subclass of mammal. In Scala, you specify co-/contra-/in-variance at class definition time.</p>
<p>What about combining predicates in Scala? We can accomplish a few of these rather quickly using some functional composition. Let’s make a new Predicates module in Scala that takes in function predicates, and provides commonly used function predicates. The input type of these combination functions should be T => Boolean and the output should also be T => Boolean. The predefined predicates should also have a type T => Boolean.</p>
<p><b><font color="red">Listing 4 Predicates in Scala</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">object Predicates {
		def or[T](f1 : T =&gt; Boolean, f2 : T =&gt; Boolean) =
		(t : T) =&gt; f1(t) || f2(t) #A
		def and[T](f1 : T =&gt; Boolean, f2 : T =&gt; Boolean) =
		(t : T) =&gt; f1(t) &amp;&amp; f2(t) #B
		val notNull[T] : T =&gt; Boolean = _ != null
	}
	#A explicit anonymous function
	#B placeholder function syntax</pre></td></tr></table></div>

</p>
<p>We’ve now started to delve into the realm of functional programming. We are defining first-class functions and combining them to perform new behaviors. You’ll notice the or method takes two predicates, f1 and f2. It then creates a new anonymous function that takes an argument t and ors the results of f1 and f2.</p>
<p>This is the essence of functional programming. Playing with functions also makes more extensive use of generics and the type system. Scala has put forth a lot of effort to reduce the overhead for generics in daily usage.</p>
<h2>Summary</h2>
<p>As Scala blends together various concepts, users of Scala will find themselves striking a delicate balance between functional programming techniques, object orientation, integration with existing Java applications, Expressive library APIs, and enforcing requirements through the type system.</p>
<div class='dd_outer'><div class='dd_inner'><div id='dd_ajax_float'><div class='dd_button_v'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http%3A%2F%2Fwww.javabeat.net%2Fcategory%2Funcategorized%2Ffeed%2F" send="false" show_faces="false"  layout="box_count" width="50"  ></fb:like></div><div style='clear:left'></div><div class='dd_button_v'><script type='text/javascript' src='https://apis.google.com/js/plusone.js'></script><g:plusone size='tall' href='http://www.javabeat.net/category/uncategorized/feed/'></g:plusone></div><div style='clear:left'></div><div class='dd_button_v'><a href="http://twitter.com/share" class="twitter-share-button" data-url="http://www.javabeat.net/category/uncategorized/feed/" data-count="vertical" data-text="Uncategorized" data-via="javabeat" ></a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><div style='clear:left'></div><div class='dd_button_extra_v'><script type="text/javascript">jQuery(document).load(function(){ stLight.options({publisher:'bab47279-62c9-46af-addc-79fd1fe8fee0'}); });</script><div class="st_email_custom"><span id='dd_email_text'>email</span></div></div><div style='clear:left'></div><div class='dd_button_extra_v'><div id='dd_print_button'><span id='dd_print_text'><a href='javascript:window:print()'>print</a></span></div></div><div style='clear:left'></div></div></div></div><script type="text/javascript">var dd_offset_from_content = 44; var dd_top_offset_from_content = 0;</script><script type="text/javascript" src="http://www.javabeat.net/wp-content/plugins/digg-digg//js/diggdigg-floating-bar.js?ver=5.3.0"></script><div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/examining-functional-concepts-in-google-collections-using-scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Methods and Pattern Matching in Scala</title>
		<link>http://www.javabeat.net/2011/06/methods-and-pattern-matching-in-scala/</link>
		<comments>http://www.javabeat.net/2011/06/methods-and-pattern-matching-in-scala/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 17:25:21 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2391</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on Scala in Action , to be published on Fall 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><i>This article is based on <a href="http://www.manning.com/raychaudhuri/" target="_blank">Scala in Action</a> , to be published on Fall 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <font color="red"><u><b>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</b></u> </font>]</i></p>
<h1><center>Methods and Pattern Matching</center></h1>
<h2>Introduction</h2>
<p>Expression-oriented programming challenges some good practices from other languages. A common practice in Java programming is having a single point of return for any method. This means that, if there is some kind of conditional logic, the developer creates a variable that contains the eventual return value. As the method flows, this variable is updated with what the method returns. The very last line in every method is a return statement. See an example in listing 1.</p>
<p><b><font color="red">Listing 1 Java Idiom: One return statement</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">def createErrorMessage(errorCode : Int) : String = {
		var result : String = _ 			#1
		errorCode match {
			case 1 =&gt;
			result = “Network Failure” 		#2
			case 2 =&gt;
			result = “I/O Failure”
			case _ =&gt;
			result = “Unknown Error”
		}
		return result;
	}
	#1 Initializes to default
	#2 Directly assigns result</pre></td></tr></table></div>

</p>
<p>As you can see, the result variable is used to store the final result. The code falls through a pattern match, assigning error strings as appropriate and then returns the result variable. We can improve this code slightly using the expression-oriented syntax that pattern matching allows. A pattern match actually returns a value. The type of the value is determined as a common super type from all case statement returns. Pattern matching also throws an exception if no pattern is matched, so we’re guaranteed a return or error here. Let’s translate the code for an expression-oriented pattern match (listing 2).</p>
<p><b><font color="red">Listing 2 Updated createErrorMessage with expression-oriented pattern match</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">def createErrorMessage(errorCode : Int) : String = {
		val result = errorCode match { 			#1
			case 1 =&gt; “Network Failure” #2
			case 2 =&gt; “I/O Failure” #2
			case 3 =&gt; “Unkonwn Error” #2
		}
		return result
	}
	#1 Assigning pattern match
	#2 Returns expression</pre></td></tr></table></div>

</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>You’ll notice two things. First, we changed the result variable to a val and let the type inferencer determine the type. This is because we no longer have to change the val after assignment, the pattern match should determine the unique value. So, not only have we reduced the size and complexity of the code, we’ve also increased immutability in the program. Immutability refers to the unchanging state of an object or variable. Immutability is the opposite of mutability. Mutability is the ability of an object or variable to change or mutate during its lifetime. You’ll frequently find that expression-oriented programming and immutable objects work very well together.</p>
<p>The second thing we’ve done is remove any kind of assignment from the case statements. The last expression in a case statement is the “result” of that case statement. We could have embedded further logic in each case statement if necessary, as long as we eventually had some kind of expression at the bottom. The compiler will also warn us if we accidentally return the wrong type.</p>
<p>The code is looking a lot more concise; however, we can still improve it somewhat. In Scala, most developers avoid return statements in their code, preferring to have the last expression be the return value (similar to all the other expression-oriented styles). In fact, for the createErrorMessage method, we can remove the intermediate result variable altogether. Let’s take a look at listing 3 for the final transformation.</p>
<p><b><font color="red">Listing 3 Final expresison-oriented createErrorMessage method</font></b></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">def createErrorMessage(errorCode : Int) : String = errorCode match {
		case 1 =&gt; “Network Failure”
		case 2 =&gt; “I/O Failure”
		case _ =&gt; “Unknown Error”
	}</pre></td></tr></table></div>

</p>
<p>Notice how we haven’t even opened up a code block for the method? The pattern match is the only statement in the method, and it returns an expression of type String.</p>
<h1>Summary</h1>
<p>We’ve completely transformed the method into an expression-oriented syntax. Notice how much more concise and expressive the code is. Also note that the compiler will warn us of any type infractions or unreachable case statements.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/methods-and-pattern-matching-in-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MongoDB Query Selectors</title>
		<link>http://www.javabeat.net/2011/06/mongodb-query-selectors/</link>
		<comments>http://www.javabeat.net/2011/06/mongodb-query-selectors/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:23:38 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2388</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on MongoDB in Action ( Buy from Amazon), to be published on July, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/banker/" target="_blank">MongoDB in Action</a> ( <a href="http://www.javabeat.net/books/251-mongodb-in-action-1.html">Buy from Amazon</a>), to be published on July, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<h2>Introduction</h2>
<p>In this article, we present parts of MongoDB’s query language. Specifically, we discuss general description queries, their semantics, and types.</p>
<h2>Selector matching</h2>
<p>The simplest way to specify a query is with a selector whose key-value pairs literally match the document you’re looking for. A couple of examples follow.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find({last_name: &quot;Banker&quot;})
	db.users.find({first_name: &quot;Smith&quot;, age: 40})</pre></td></tr></table></div>

<p>The second query reads, “Find me a user with a first name is Smith <em>and</em> aged 40.” Notice that, whenever you pass more than one key-value pair, both have to match; the query conditions are additive.</p>
<h2>Ranges</h2>
<p>It’s frequently necessary to query for documents whose values span a certain range. In SQL, we’d use, and &gt;=; with MongoDB, we get the analogous set of operators $gt, $gte, $lt, and $lte. Their behavior is mostly predictable. However, beginners sometimes struggle with combining these operators. A common mistake is to repeat the search key:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find({age: {$gte: 0}, age: {$lte: 30})</pre></td></tr></table></div>

<p>But, because keys cannot be repeated, this query selector is invalid. This query is properly expressed as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find({age: {$gte: 0, $lte: 30})</pre></td></tr></table></div>

<p>The only other surprise regarding the range operators involves types. In short, range queries will only match values having the same type as the value passed in. For example, suppose you have a collection with the following documents:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">{ &quot;_id&quot; : ObjectId(&quot;4caf82011b0978483ea29ada&quot;), &quot;value&quot; : 97 }
	{ &quot;_id&quot; : ObjectId(&quot;4caf82031b0978483ea29adb&quot;), &quot;value&quot; : 98 }
	{ &quot;_id&quot; : ObjectId(&quot;4caf82051b0978483ea29adc&quot;), &quot;value&quot; : 99 }
	{ &quot;_id&quot; : ObjectId(&quot;4caf820d1b0978483ea29ade&quot;), &quot;value&quot; : &quot;a&quot; }
	{ &quot;_id&quot; : ObjectId(&quot;4caf820f1b0978483ea29adf&quot;), &quot;value&quot; : &quot;b&quot; }
	{ &quot;_id&quot; : ObjectId(&quot;4caf82101b0978483ea29ae0&quot;), &quot;value&quot; : &quot;c&quot; }</pre></td></tr></table></div>

<p>You then issue the following query:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.items.find({value: {$gte: 97})</pre></td></tr></table></div>

<p>Now, you may think to yourself that this query should return all six documents since the strings are numerically equivalent to the integers 97, 98, and 99. This is not the case. If you want the integer results, you need to issue the above query. If you want the string result, here’s the query you’ll want:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.items.find({value: {$gte: &quot;a&quot;})</pre></td></tr></table></div>

<p>Of course, you won’t have to worry about this type restriction as long as you never store multiple types for the same key within the same collection. Definitely a good base rule of thumb.</p>
<h2>Set operators</h2>
<p>Three query operators &#8211;$in , $all, and $nin&#8211; take a list of one or more values as their predicate. $in, the most used of the three, returns a document if any of the given values matches the search key. We might use this operator to return all products belonging to some discrete set of categories. If the following list of category ids correspond to the lawn mowers, hand tools, and work clothing categories:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">[ObjectId(&quot;6a5b1476238d3b4dd5000048&quot;),
	ObjectId(&quot;6a5b1476238d3b4dd5000051&quot;),
	ObjectId(&quot;6a5b1476238d3b4dd5000057&quot;)
	]</pre></td></tr></table></div>

<p>Then finding all products belonging to these categories looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find(main_cat_id: { $in:
		[ObjectId(&quot;6a5b1476238d3b4dd5000048&quot;),
		ObjectId(&quot;6a5b1476238d3b4dd5000051&quot;),
		ObjectId(&quot;6a5b1476238d3b4dd5000057&quot;) })]</pre></td></tr></table></div>

<p>$in is frequently used with lists of ids. $nin matches only when none of the given elements matches. We might use $nin to find all products that are neither black nor blue:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find(‘details.color’: { $nin: [&quot;black&quot;, &quot;blue&quot;] }</pre></td></tr></table></div>

<p>Finally, $all matches if all the given elements match the search key. If we wanted to find all products tagged as gift and garden, $all would be a good choice:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find(‘tags’: { $all: [&quot;gift&quot;, &quot;garden&quot;] }</pre></td></tr></table></div>

<p>Naturally, this query only makes sense if tags is an array.</p>
<h2>Logical operators</h2>
<p>MongoDB’s logical operators include $ne, $not, $or, and $exists. There is no $and, since adding conditions to a query selector always implies AND.</p>
<p>$ne, the not equal operator, works as expected. In practice, it’s best used in combination with at least one other operator; otherwise, it’s likely to be pretty inefficient. We might use $ne to find all products manufactured by ACME that aren’t tagged &#8220;gardening.&#8221;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find(‘details.manufacturer’: ‘ACME’, tags: {$ne: &quot;gardening&quot;} }</pre></td></tr></table></div>

<p>$ne works on keys pointing to single values and to arrays, as shown in the foregoing example.</p>
<p>While $ne matches the negation of the specified values, $not will negate the result of another MongoDB operator or regular expression query. Keep in mind that most query operators already have a negated form ($in and $nin, $gt and $lte, and so on); $not shouldn’t be used with any of these. Reserve $not for times when the operator or regex you’re using lacks a negated form. If you wanted to query for all users with last names not beginning with B, you could use $not like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find(last_name: {$not: /^B/} }</pre></td></tr></table></div>

<p>$or expresses the logical disjunction of two values for two different keys. This is an important point: if the possible values are scoped to the same key, use $in instead. Trivially, finding all products that are either blue or green looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find(‘details.color’: {$in: [‘blue’, ‘green’]} }</pre></td></tr></table></div>

<p>But finding all products that are either blue or made by ACME requires $or:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt; db.products.find({ $or: [{‘details.color’: ‘blue’}, ‘details.manufacturer’: ‘ACME’}] }) &lt;/code&gt;</pre></td></tr></table></div>

<p>Notice that $or takes an array of query selectors, where each selector can be arbitrarily complex and contain other query operators.</p>
<p>Because collections don’t enforce a fixed schema, we sometimes need a way to query for documents containing a particular key. For this, we can use the $exists operator. Suppose we had planned to use a product’s details attribute to store custom fields. Assuming that not all products specify a set of colors, we can query for them:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find({ $or: [{‘details.color’: ‘blue’}, ‘details.manufacturer’: ‘ACME’}] })</pre></td></tr></table></div>

<p>The opposite query is also possible:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find({‘details.color’: {$exists: true}})</pre></td></tr></table></div>

<p>Now, before moving on, it’s worth noting that there’s another way to check for existence that’s practically equivalent: by querying for null.2 This would alter the above two queries slightly. The first could be expressed like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.products.find({‘details.color’: null})</pre></td></tr></table></div>

<p>And the second:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find({‘details.color’: {$ne: null}})</pre></td></tr></table></div>

<p>The main advantage of querying for null is that such a query can use an index. While $exists always performs a full table scan, these queries for a null or non-null value will use an index if it exists. That’s a good thing. So prefer the query for null if your application allows it.</p>
<h2>Objects</h2>
<p>Some of the entities in our e-commerce data model have keys that point to a single embedded object. The product details attribute is one good example. Here’s part of the relevant document, expressed as JSON:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">{ _id: {sym: ‘gs’, date: 20101005}
		open: 40.23,
		high: 45.50,
		low: 38.81,
		close: 41.22
	}</pre></td></tr></table></div>

<p>We can query on such objects by separating the relevant keys with a dot. For instance, if we want to find all products manufactured by Acme, here would be our query:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt; db.products.find({‘details.manufacturer_id’: 432}); &lt;/code&gt;</pre></td></tr></table></div>

<p>Such queries can be specified arbitrarily deep. Thus, supposing we had the following slightly modified representation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;code&gt; { _id: ObjectId(&quot;4c4b1476238d3b4dd5003981&quot;), slug: &quot;wheel-barrow-9092&quot;, sku: &quot;9092&quot;, details: { model_num: 4039283402, manufacturer: { name: &quot;Acme&quot;, id: 432 }, color: &quot;Green&quot; } } &lt;/code&gt;</pre></td></tr></table></div>

<p>The query would be as expected:</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.products.find({‘details.manufacturer.id’: 432});</pre></td></tr></table></div>

<p>But, in addition to querying on a single object attribute, we can query on an object as a whole. This sometimes comes in quite handy. For instance, imagine you’re using MongoDB to store stock data. To save space, you may want to forgo the standard object id and replace it with a compound key consisting of the stock symbol and a timestamp. Here’s how a representative document might look:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> { _id: {sym: ‘gs’, date: 20101005} open: 40.23, high: 45.50, low: 38.81, close: 41.22 } &lt;/code&gt;</pre></td></tr></table></div>

<p>We could then find the summary of GS for October 5, 2010 with the following _id query:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.ticks.find({_id: {sym: ‘gs’, date: 20101005} });</pre></td></tr></table></div>

<p>Keep in mind that these queries do a strict, byte-by-byte comparison, which means that the order of the keys matters. The following query is not equivalent and won’t match our sample document:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.ticks.find({_id: {date: 20101005, sym: ‘gs’} });</pre></td></tr></table></div>

<p>And while the order of keys will be preserved in JSON documents entered via the shell, this is not necessarily true for document representations in the various language drivers. In particular, hashes in Ruby 1.8 are not order-preserving. To preserve order in Ruby 1.8, you’ll need to explicitly construct an OrderedHash:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">doc = BSON::OrderedHash.new
	doc[‘sym’] = ‘gs’
	doc[‘date’] = 20101005
	@ticks.find(doc)</pre></td></tr></table></div>

<h2>Arrays</h2>
<p>Arrays give the document model its power. They’re used to store lists of strings, object ids, and even other documents. Arrays afford us rich yet comprehensible documents; it stands to reason that MongoDB would let us query and index the array type with ease. Indeed, the simplest array queries look just like queries on any other document type. Take product tags, for example. These tags are represented as a simple list of strings:,/p&gt;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">{ _id: ObjectId(&quot;4c4b1476238d3b4dd5003981&quot;),
		slug: &quot;wheel-barrow-9092&quot;,
		sku: &quot;9092&quot;,
		tags: [&quot;tools&quot;, &quot;equipment&quot;, &quot;soil&quot;]
	}</pre></td></tr></table></div>

<p>Querying for products with the tag soil is trivial:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.products.find({tags: &quot;soil&quot;})</pre></td></tr></table></div>

<p>This query is also efficient so long as we have an index on the tags field:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.products.ensureIndex({tags: 1})</pre></td></tr></table></div>

<p>When we need more control over our queries, we can use dot-notation to query for a value at a particular position within the array. Here’s how we’d restrict the previous query to the first of a product’s tags:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.products.find({‘tags.0’: &quot;soil&quot;})</pre></td></tr></table></div>

<p>It might not make much sense to query tags in this way, but imagine we’re dealing with user addresses. In this case, we have an array of sub-documents.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">{ _id: BSON::ObjectID(&quot;4c4b1476238d3b4dd5000001&quot;)
		username: &quot;kbanker&quot;,
		addresses: [
			{name: &quot;home&quot;,
				street: &quot;588 5th Street&quot;,
				city: &quot;Brooklyn&quot;,
				state: &quot;NY&quot;,
				zip: 11215},
			{name: &quot;work&quot;,
				street: &quot;1 E. 23rd Street&quot;,
				city: &quot;New York&quot;,
				state &quot;NY&quot;,
				zip 10010},
		]
	}</pre></td></tr></table></div>

<p>We might stipulate that the first of these addresses always be the user’s primary shipping address. Thus, to find all users whose primary shipping address is in New York, we could again use the specify the zero position and combine that with a dot to target the state field:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.find({‘addresses.0.state’: &quot;NY&quot;})</pre></td></tr></table></div>

<p>We can just as easily omit the position and use a specify a field alone. Following query will return a user document if <em>any</em> of the addresses in the list are in New York.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.find({‘addresses.state’: &quot;NY&quot;})</pre></td></tr></table></div>

<p>As we did before, we can index this dotted field:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.ensureIndex({‘addresses.state’: 1})</pre></td></tr></table></div>

<p>Notice that the same dot-notation is used regardless of whether a field points to a subdocument or to an array of subdocuments. This is powerful, and the consistency is reassuring. However, ambiguity can arise when querying for more than attribute within an array of subobjects. For example, suppose we want to fetch a list of all users whose home address is in New York. Can you think of a way to express this query?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.find({‘addresses.name’: ‘home’, ‘addresses.state’: ‘NY’})</pre></td></tr></table></div>

<p>The problem with this query is that the field references aren’t restricted to a single address. In other words, this query will match as long as one of the addresses is designated as &#8220;home&#8221; and one is in New York, but these need to be the same address. To restrict multiple conditions to the same subdocument, you have to use the $elemMatch operator. Here’s how we’d use $elemMatch to ensure that home and NY appear in the same subdocument:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find({addresses: {$elemMatch: {name: ‘home’, state: ‘NY’}}})</pre></td></tr></table></div>

<p>Logically, $elemMatch is used only when you need to match two or more attributes in a subdocument.</p>
<h2>JavaScript</h2>
<p>If you can’t express your query with the tools described thus far, then you may have to write some JavaScript. You can use the special $where operator to pass a JavaScript expression to any query. Within the JavaScript context, this refers to the current document. To take an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.reviews.find({$where: &quot;function() { return this.helpful_votes &amp;gt; 3; }&quot;}})</pre></td></tr></table></div>

<p>There’s also an abbreviated form for simple expressions like this one:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.reviews.find({$where: &quot;this.helpful_votes &amp;gt; 3&quot;}})</pre></td></tr></table></div>

<p>This query works but can be easily expressed with the standard query language. JavaScript expressions can’t use an index, and they incur a substantial cost because they must be evaluated within a JavaScript interpreter context. For these reasons, you should issue JavaScript queries only when the standard query language can’t express your query. If you do find yourself needing JavaScript, try to combine the JavaScript expression with another query operator. The standard query operator will prompt the server to pare down the result set, thus limiting the number of documents that must be loaded into a JS context. Let’s take a quick example to see how this would work.</p>
<p>Imagine that, for each user, we’ve calculated a rating reliability factor. This is essentially an integer that, when multiplied by the user’s rating, results in a more normalized rating. Suppose further that we want to query a particular user’s reviews and only return a normalized rating greater than 3. Here’s how that query would look:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.reviews.find({user_id: ObjectId(&quot;4c4b1476238d3b4dd5000001&quot;), $where: &quot;(this.rating * .92) &amp;gt; 3&quot;})</pre></td></tr></table></div>

<p>This query meets both recommendations: it uses a standard query, on a presumably-indexed field, and it employs a JavaScript user_id expression that’s absolutely beyond the capabilities of the standard query language.</p>
<p>In addition to the attendant performance penalties, it’s good to be aware of the possibility of JavaScript injection attacks. An injection attack becomes possible whenever a user is allowed to enter code directly into a JavaScript query. While there’s never any danger of the user writing or deleting data, a user might be able to read sensitive data. A naive JavaScript query in Ruby might look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@users.find({$where =&amp;gt; &quot;this.#{attribute} == #{value}&quot;})</pre></td></tr></table></div>

<p>Assuming that the user controls the values of attribute and value, he can use manipulate query to search the users collection on any attribute pair. While this might not be the worst imaginable intrusion, we’d be wise to prevent this.</p>
<h2>Regular expressions</h2>
<p>You can use a regular expression within a query. You would use, for example, a prefix expression, /^B/, to find last names beginning with a B, and this query would use an index. In fact, much more is possible. MongoDB is compiled with PCRE,5 which supports a huge gamut of regular expressions.</p>
<p>Now, with the exception of the prefix-style query just described, regular expressions queries can’t use an index. Thus, I recommend using them as you would a JavaScript expression, that is, in combination with at least one other query term. Here’s how you might query a given user’s reviews for review text containing one of the words &#8220;best&#8221; or &#8220;worst.&#8221; Notice that we use the i regex flag to indicate case insensitivity:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.reviews.find({user_id: ObjectId(&quot;4c4b1476238d3b4dd5000001&quot;), text: /best|worst/i })</pre></td></tr></table></div>

<p>If the language you’re using has a native regex type, you can use a native regex object to perform the query. The above query, in Ruby, is practically identical:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> @reviews.find({:user_id =&amp;gt; BSON::ObjectId(&quot;4c4b1476238d3b4dd5000001&quot;), :text =&amp;gt; /best|woest/i })</pre></td></tr></table></div>

<p>If you’re querying from an environment that doesn’t support a native regex type, you can use the special $regex and $options operators. Using these operators from the shell, the previous query would be expressed like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.reviews.find({user_id: ObjectId(&quot;4c4b1476238d3b4dd5000001&quot;), text: {$regex: &quot;best|worst&quot;, $options: &quot;i&quot; })</pre></td></tr></table></div>

<h2>Special query operators</h2>
<p>There are two more query operators that aren’t easily categorized and, thus, deserve their own section. The first is $mod, which allows you to query for the modulo of a given value.</p>
<p>Finally, there’s the $type operator, which matches a value if it’s represented as a particular BSON type. I don’t recommend storing multiple types for the same field within a collection but, if the situation ever arises, you have a query operator that essentially lets you test against type. This came in handy recently when we had discovered a user whose _id queries weren’t always matching when they should have. This problem, it turned out, was that the user had been storing ids as both strings and as proper object ids. These are represented as BSON types 2 and 7, respectively; to a new user, this distinction is easy to miss.</p>
<p>Correcting the issue first required finding all documents with ids stored as strings. The $type operator can help us to just that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.users.find({_id: {$type: 2}})</pre></td></tr></table></div>

<h2>Summary</h2>
<p>Queries allow us to get the data as it’s stored. You’ve learned about general description queries, their semantics, and types. If you&#8217;re ever unsure of how a particular combination of query operators will serve you, the shell is always a ready test bed.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/mongodb-query-selectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indexing in Practice using MongoDB</title>
		<link>http://www.javabeat.net/2011/06/indexing-in-practice-using-mongodb/</link>
		<comments>http://www.javabeat.net/2011/06/indexing-in-practice-using-mongodb/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:23:29 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2387</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on MongoDB in Action ( Buy from Amazon), to be published on July, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/banker/" target="_blank">MongoDB in Action</a> ( <a href="http://www.javabeat.net/books/251-mongodb-in-action-1.html">Buy from Amazon</a>), to be published on July, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<p><center></p>
<h2>Indexing in Practice</h2>
<p></center></p>
<h2>Introduction</h2>
<p>Indexes are enormously important. With the right indexes in place, MongoDB can use its hardware efficiently and serve your application&#8217;s queries quickly. With the wrong indexes, you&#8217;ll see the exact opposite effect: slow queries and poorly utilized hardware. It stands to reason, then, that anyone wanting to use MongoDB effectively and make the best use of hardware resources must understand indexing.</p>
<p>We&#8217;re going to look at some refinements on the kinds of indexes that can be created in MongoDB. We&#8217;ll then proceed to some of the niceties of administering those indexes.</p>
<h2>Index types</h2>
<p>All indexes in MongoDB use the same underlying data structure, but several kinds of indexes are nevertheless permitted. In particular, unique, sparse, and multikey indexes are frequently used, and here we describe them in some detail.</p>
<h2>Unique indexes</h2>
<p>To create a unique index, specify the unique option:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.ensureIndex({username: 1}, {unique: true})</pre></td></tr></table></div>

<p>Unique indexes ensure that all index entries are unique. Thus, if we try to insert a document into the collection with an already-indexed username, users that insert will fail with the following exception:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">E11000 duplicate key error index: gardening.users.$username_1 dup key: { : &quot;kbanker&quot; }</pre></td></tr></table></div>

<p>If using a driver, this exception will be caught only if you perform the insert using your driver&#8217;s safe mode. If you need a unique index on a collection, it&#8217;s usually best to create the index before inserting any data. If you create the index in advance, you guarantee the uniqueness constraint from the start. Creating a unique index on a collection that already contains data, you run the risk of failure since it&#8217;s entirely possible that duplicate keys will exist in the collection. When duplicate keys exist, the index creation fails.</p>
<p>If you do find yourself needing to create a unique index on an established collection, you have a couple of options. The first is to repeatedly attempt to create the unique index and use the failure messages to intelligently remove the documents with duplicate keys. But if the data isn&#8217;t so important, you can also instruct the database to drop documents with duplicate keys automatically using the <em>dropDups</em> option. So, to take an example, if our users collection already contains data and if we don&#8217;t care that documents with duplicate keys are removed, then we can issue the index creation command like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.users.ensureIndex({username: 1}, {unique: true, dropDups: true})</pre></td></tr></table></div>

<h3>Sparse indexes</h3>
<p>Indexes are dense by default. This means that, for every document, in an indexed collection, there will be a corresponding entry in the index even if the document lacks the indexed key. Suppose you have a products collection in an e-commerce data model, and imagine that you&#8217;ve built an index on the product attribute category_ids. Now, suppose that there are a few products that haven&#8217;t been assigned to any categories. For each of these category-less products, there will still exist a null entry in the category_ids index. We can query for those null values:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.find({category_ids: null})</pre></td></tr></table></div>

<p>When searching for all products lacking a category, the query optimizer will still be able to use the category_ids index to locate those products. When the number of null values in the index is small, this is still a good thing because this query can use the index.</p>
<p>But, there are two cases where a dense index is undesirable. The first is when you want a unique index on a field that doesn&#8217;t appear in every document in the collection. For instance, we definitely want a unique index on every product&#8217;s <em>sku</em> field. But, suppose that, for some reason, products are entered into the system before a sku is assigned. If we have a unique index on sku and we attempt to insert more than one product without a sku, then those inserts will fail because there will already be an entry in the index where <em>sku</em> is null. This is, therefore, a case where a dense index doesn&#8217;t serve our purpose. What we want instead is a sparse index.</p>
<p>In a sparse index, only those documents having a non-null value for the indexed attribute will appear. If we want to create a sparse index in MongoDB, all we have to do is specify {sparse: true}. So, for example, we can create a unique, sparse index on <em>sku</em> like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.products.ensureIndex({sku: 1}, {unique: true, sparse: true})</pre></td></tr></table></div>

<p>There&#8217;s another case where a sparse index is desirable: when a large number of documents in a collection don&#8217;t contain the indexed key. For example, suppose we allowed anonymous reviews on our e-commerce site. In this case, half the reviews might lack a <em>user_id</em> field and, if that field were indexed, then half the entries in that index would be null. This would be pretty inefficient for two reasons. First, it would increase the size of the index. Second, it would require updates to the index when adding and removing documents with null <em>user_id</em> fields.</p>
<p>If we rarely (or never) expected queries on anonymous reviews, we might elect to build a sparse index on user_id. Again, setting the sparse option is simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.reviews.ensureIndex({user_id: 1}, {sparse: true})</pre></td></tr></table></div>

<p>Now, only those reviews linked to a user via the user_id field would be indexed.</p>
<h3>Multikey indexes</h3>
<p>Indexing fields whose values are arrays is made possible by what&#8217;s known as a multikey index, which allows multiple entries in the index to reference the same document. This makes sense if we take a simple example. Suppose we have a product document with a few tags like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">{ name: &quot;Wheelbarrow&quot;,
		tags: [&quot;tools&quot;, &quot;gardening&quot;, &quot;soil&quot;]
	}</pre></td></tr></table></div>

<p>If we create an index on <em>tags</em>, then each value in this document&#8217;s tags array will appear in the index. This means that a query on any one of these array values can use the index to locate the document. This is the idea behind a multikey index: multiple index entries, or keys, end up referencing the same document.</p>
<p>Multikey indexes are always enabled in MongoDB. Anytime an indexed field contains an array, each array value will be given its own entry in the index. The intelligent use of multikey indexes is essential to proper MongoDB schema design.</p>
<h2>Index administration</h2>
<p>When it comes to administering indexes in MongoDB, there may be some gaps in your understanding. Here we&#8217;ll examine index creation and deletion in detail and address questions surrounding compaction and backups.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<h3>Creating and deleting indexes</h3>
<p>If you&#8217;ve created a few indexes, there should be no mysteries surrounding the index creation syntax. Simply call one of the index creation helper methods, either in the shell or with your language of choice and a document defining the new index will be placed into the special system.indexes collection.</p>
<p>While it&#8217;s usually easier to use a helper method to create an index, you can insert an index specification manually (this is, after all, what the helper methods do). You just need to be sure you&#8217;ve specified the minimum set of keys: ns, key, and name. ns is the namespace, key is the field or combination of fields to index, and name is a name used to refer to the index. Any additional options, like sparse, can also be specified here. So to have an example, let&#8217;s create a sparse index on our users collection:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">spec = {ns: &quot;green.users&quot;, key: {‘addresses.zip': 1}, name: ‘zip'}
	db.system.indexes.insert(spec, true)</pre></td></tr></table></div>

<p>If no errors are returned on insert, then the index now exists and we can query the system.indexes collection to prove it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.system.indexes.find()
	{ &quot;_id&quot; : ObjectId(&quot;4d2205c4051f853d46447e95&quot;), &quot;ns&quot; : &quot;green.users&quot;,
	&quot;key&quot; : { &quot;addresses.zip&quot; : 1 }, &quot;name&quot; : &quot;zip&quot;, &quot;v&quot; : 0 }</pre></td></tr></table></div>

<p>If you&#8217;re running MongoDB v1.8 or later, you&#8217;ll see that an extra key, v, has been added. This version field allows for future changes in the internal index format but should be of little concern to application developers.</p>
<p>To delete an index, you might think that all you need do is remove the index document from system.indexes, but this operation is prohibited. Instead, you can delete indexes using the database command deleteIndexes. As with index creation, there are helpers for deleting indexes but, if you want to run the command itself, you can do that too. It takes as its argument a document containing the collection name and either the name of the index to drop or &#8220;*&#8221; to drop all indexes. To manually drop the indexes we just created, issue the command like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">use green
	db.runCommand({deleteIndexes: &quot;users&quot;, index: &quot;zip&quot;})</pre></td></tr></table></div>

<p>That&#8217;s the basics of creating and deleting indexes. For what to expect when an index is created, read on.</p>
<h3>Building indexes</h3>
<p>Most of the time, you&#8217;ll want to declare your indexes before putting your application in production. This allows indexes to be built incrementally, as the data is inserted. But there are two cases where you might choose to build an index after the fact. The first case is when you need to import a lot of data before switching to production. For instance, you might be migrating an application to MongoDB and need to seed the database with user information from a data warehouse. You could certainly create the indexes on your user data in advance, but doing so after you&#8217;ve imported the data will ensure an ideally balanced index from the start. Furthermore, the net time updating will be minimized. The second and more obvious case of creating indexes on existing data sets is when you have to optimize for new queries.</p>
<p>Regardless of why you&#8217;re creating new indexes, the process isn&#8217;t always pleasing. For large data sets, building an index can take hours, even days. You can see its progress in the MongoDB logs. Let&#8217;s take an example from a data set that we&#8217;ll be using in the next section. First, we declare an index to be built:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.values.ensureIndex({open: 1, close: 1})</pre></td></tr></table></div>

<p>The index builds in two steps. In the first step, the values to be indexed are sorted. A sorted data set makes for a much more efficient insertion into the B-tree. Notice that the progress of the sort is indicated by the ratio of the number of documents sorted to the total number of documents:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">Tue Jan 4 09:58:17 [conn1] building new index on { open: 1.0, close: 1.0 } for stocks.values
	1000000/4308303 23%
	2000000/4308303 46%
	3000000/4308303 69%
	4000000/4308303 92%
	Tue Jan 4 09:59:13 [conn1] external sort used : 5 files in 55 secs</pre></td></tr></table></div>

<p>For step two, the sorted values are inserted into the index. Progress is indicated in the same way, and, when complete, the time it took to complete the index build is indicated as the insert time into system.indexes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">1200300/4308303 27%
	2227900/4308303 51%
	2837100/4308303 65%
	3278100/4308303 76%
	3783300/4308303 87%
	4075500/4308303 94%
	Tue Jan 4 10:00:16 [conn1] done building bottom layer, going to commit
	Tue Jan 4 10:00:16 [conn1] done for 4308303 records 118.942secs
	Tue Jan 4 10:00:16 [conn1] insert stocks.system.indexes 118942ms</pre></td></tr></table></div>

<p>In addition to examining the MongoDB log, you can check the index build progress by running the currentOp() command:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&gt; db.currentOp()
	{
		&quot;inprog&quot; : [
		{
			&quot;opid&quot; : 58,
			&quot;active&quot; : true,
			&quot;lockType&quot; : &quot;write&quot;,
			&quot;waitingForLock&quot; : false,
			&quot;secs_running&quot; : 55,
			&quot;op&quot; : &quot;insert&quot;,
			&quot;ns&quot; : &quot;stocks.system.indexes&quot;,
			&quot;query&quot; : {
			},
			&quot;client&quot; : &quot;127.0.0.1:53421&quot;,
			&quot;desc&quot; : &quot;conn&quot;,
			&quot;msg&quot; : &quot;index: (1/3) external sort 3999999/4308303 92%&quot;
		}
		]
	}</pre></td></tr></table></div>

<p><strong><span style="color: red;">WARNING: Be careful declaring indexes</span></strong></p>
<p>Because it&#8217;s so easy to declare indexes, it&#8217;s also quite easy to inadvertently trigger an index build. If the data set is large enough, then the build will take a long time. And, in a production situation, this can be a nightmare since there&#8217;s no easy way to kill an index build. If this ever happens to you, you&#8217;ll have to fail over to a secondary node—if you have one. But the most prudent advice is to treat an index build as a kind of database migration and ensure that your application code never declares indexes automatically.</p>
<h3>BACKGROUND INDEXING</h3>
<p>If you&#8217;re running in production and simply can&#8217;t afford to halt access to the database, you can specify that an index be built in the background. Although the index build will still take a write lock, the job will pause to allow other readers and writers to access the database. If your application typically exerts a heavy load on MongoDB, then a background index build will certainly degrade performance, but this may be acceptable under certain circumstances. For example, if you know that the index can be built within a time window where application traffic is at a minimum, background indexing in this case might be a good choice. To build an index in the background, simply specify {background: true when you declare the index. The previous index can be built in the background like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;"> db.values.ensureIndex({open: 1, close: 1}, {background: true})</pre></td></tr></table></div>

<h3>OFFLINE INDEXING</h3>
<p>If your production data set is too large to be indexed within a few hours, then a more sophisticated plan will be required. This will usually involve taking a replica node offline, building the index on that node by itself, and then allowing the node to catch up with the master replica. This does presume that your replication oplog is large enough to prevent the offline node from becoming stale during the index build.</p>
<h3>Backups</h3>
<p>Because indexes are hard to build, you definitely want to back them up. Unfortunately, not all backup methods include indexes. For instance, you might be tempted to use <em>mongodump</em> and <em>mongorestore</em>, but these utilities handle documents only. This means that, when you run <em>mongorestore</em>, all the indexes defined for any collections you&#8217;ve backed up will be recreated. As always, if your data set is large, the time it takes to build all indexes may be unacceptable.</p>
<p>Consequently, if you want your backups to include indexes, then you&#8217;ll want to opt for backing up the MongoDB data files themselves.</p>
<h3>Compaction</h3>
<p>If your application heavily updates existing data or performs a lot of large deletions, then you may end up with a highly fragmented index. The primary symptom of this is an index size much larger than you&#8217;d expect for the given data size, but you may also see certain performance problems. In these cases, you may want to consider rebuilding one or more indexes. You can do this by recreating individual indexes or by running the <em>reIndex</em> command, which will rebuild all indexes in a given collection.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">db.values.reIndex();</pre></td></tr></table></div>

<p>Be careful about reindexing: the command will take out a write lock for the duration of the rebuild, rendering your MongoDB instance unusable for that time. This is best done offline.</p>
<h2>Summary</h2>
<p>For many developers, indexes are a topic shrouded in mystery. This need not be the case. The purpose of this article was to help you develop a mental model for thinking clearly about indexes. We discussed unique, sparse, and multikey indexes, and provided a number of pointers on index administration.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/indexing-in-practice-using-mongodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MongoDB vs. RDBMS Schema Design</title>
		<link>http://www.javabeat.net/2011/06/mongodb-vs-rdbms-schema-design/</link>
		<comments>http://www.javabeat.net/2011/06/mongodb-vs-rdbms-schema-design/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 17:22:33 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2384</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on MongoDB in Action ( Buy from Amazon), to be published on July, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/banker/" target="_blank">MongoDB in Action</a> ( <a href="http://www.javabeat.net/books/251-mongodb-in-action-1.html">Buy from Amazon</a>), to be published on July, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<h2>Introduction</h2>
<p>This article takes a closer look at the document-oriented data model and at how data is organized at the database, collection, and document level in MongoDB. We start with a brief, general discussion of schema design. This is helpful because a large number of MongoDB users have never designed schemas outside the realm of a traditional relational database management system (RDBMS). This exploration of principles helps set the stage for the second part of the article, where we examine the design of an e-commerce schema in MongoDB. Along the way, I&#8217;ll explain how this schema differs from an equivalent RDBMS schema, and we&#8217;ll learn how common relationships between entities, such as one-to-many and many-to-many, are replicated in MongoDB.</p>
<h2>Principles of schema design</h2>
<p>Database schema design is the process of choosing the best representation for a data set given the features of the database system, the nature of the data, and the requirements of the application. The principles of schema design for relational database systems are well established. With RDBMSs, we&#8217;re encouraged to shoot for a normalized data model, which helps to ensure generic queryability and avoid updates to the data that might result in inconsistencies. Developers aren&#8217;t wondering how to create one-to-many and many-to-many relationships in an RDBMS. But, having said that, schema design is not a hard science even with relational databases. Performance-intensive applications or applications that have to consume data flexibly, may require a more <em>denormalized</em> data model. Some applications are so demanding in their storage and scaling requirements that they&#8217;re forced to break all the old rules of schema design. FriendFeed is a great example of this, and reading about their data model is well worth your time.</p>
<p>If you&#8217;re moving to MongoDB from the RDBMS world, you may be troubled by the lack of hard schema design rules. Good practices are beginning to emerge but, frankly, there&#8217;s often more than one good way to model a given data set. The premise of this section is that principles can drive schema design. But those principles are, in practice, variable. Here, then, are few questions that can be applied when modeling data with any database system.</p>
<ul>
<li><em>What&#8217;s the basic unit of data?</em>
<p>In an RDBMS, we have tables with columns and rows. In a key-value store, we have keys and amorphous values. And, in MongoDB, the basic unit of data is the BSON document.</li>
<li><em>How can we query and update that data?</em>
<p>Once we have a basic unit of data, we need to know how we can manipulate it. RDBMSs feature indexed dynamic queries and joins. MongoDB also has indexed dynamic queries, but joins aren&#8217;t supported. And a simple key-value store can only get and put values based on a single key. Databases also diverge in the kinds of updates they permit. With an RDBMS, we can update documents in sophisticated ways using SQL and we can wrap such updates in a transaction to get atomicity and rollback. MongoDB doesn&#8217;t support transactions, but it does support a variety of atomic update operations that can work on the internal structures of a complex document. With simple key-value stores, we might be able to update a value but every update involves replacing the value completely. The essential point of this second question is that building the best data model means understanding the features of your database. If you want to model data well in MongoDB, you need to know exactly what sorts of queries and updates it performs the best.</li>
<li><em>What are our application access patterns?</em>
<p>In addition to understanding our basic unit of data and database features, we also need to pin down the needs of our application. If you read the FriendFeed article suggested earlier, you see that the idiosyncrasies of an application can easily demand a schema that goes against firmly-held data modeling principles. Thus, numerous questions can be asked about the application to help determine the data model. What&#8217;s the read/write ratio? What&#8217;s sorts of queries do we need? How is the data updated? What concurrency issues are involved?</li>
</ul>
<p>The best schema designs are always the product of deep knowledge of the database you&#8217;re using, good judgment about the requirements of the application at hand, and plain old experience.</p>
<h2>Designing an e-commerce data model</h2>
<p>Demonstrations of next-generation data stores typically revolve around social media: Twitter-like demo apps are the norm. Unfortunately, such apps tend to have rather simple data models. E-commerce has the advantage of including a large number of familiar data modeling patterns. It&#8217;s not difficult to imagine how products, categories, product reviews, and orders would typically be modeled using an RDBMS. This should make the upcoming examples more instructive, as we&#8217;ll be able to contrast them with our preconceived notions of schema design.</p>
<p>E-commerce has always been a domain exclusive to RDBMSs for a couple of reasons. The first is that e-commerce sites generally require transactions, and transactions are a strong point of RDBMSs. The second is that, up until very recently, domains that require rich data models and dynamic queries have been assumed to fit best within the realm of RDBMS. The examples below call into question this second assumption.</p>
<p>Building an entire e-commerce back-end is beyond this article&#8217;s scope. What we&#8217;re going to do instead is pick out a handful of e-commerce entities and show how they might be modeled in MongoDB. In particular, we&#8217;re going to look at products and categories, users and orders, and product reviews. For each entity, we&#8217;ll show an example document. Then, we&#8217;ll hint at some of the database features that make that particular structure viable.</p>
<p>Before moving on, we must say a few words about object mappers. When developing applications on an RDBMS, you&#8217;ve probably used an object-relational mapping library, such as Java&#8217;s Spring framework or Ruby&#8217;s ActiveRecord. Such libraries are absolutely necessary for building applications efficiently on an RDBMS. But they&#8217;re less necessary with MongoDB. This is due in part to documents already being an object-like representation. It&#8217;s also partly due to the good usability of drivers, which provide a high-level interface to MongoDB.</p>
<p>Indeed, applications can frequently be built on drivers alone. Having said that, object mappers are nice for larger projects, since they usually provide validation, type checking, and associations for free. And there are, undoubtedly, a number of mature object mappers for MongoDB that work on top of the basic language drivers. Ultimately, regardless of the object mapper, what gets stored in MongoDB are documents. Knowing the shape of documents in a good MongoDB schema will prepare you to work with the database efficiently with or without an object mapper.</p>
<h3>Products and categories</h3>
<p>Products and categories are mainstays of any e-commerce site. Products in a normalized RDBMS model tend to require a large number of tables. There&#8217;s a table for basic product info, such as the name and SKU, but there will also be other tables to relate shipping information and pricing histories. If the system allows creating products with arbitrary attributes, a complicated series of tables will be added to define and store those attributes. All of this will be facilitated by the RDBMS&#8217;s ability to join tables.</p>
<p>Modeling a product in MongoDB should be less complicated. Because collections don&#8217;t enforce a schema, any product document will have room for whichever dynamic attributes the product dictates. And, because we can use arrays to contain inner document structures, we can typically condense a multitable RDBMS representation into a single MongoDB collection. Listing 1 shows a product from a sample gardening store.</p>
<p><strong><span style="color: red;">Listing 1 A sample product document</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@doc =
	{ :_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5003981&quot;),
		:slug =&gt; &quot;wheel-barrow-9092&quot;,
		:sku =&gt; &quot;9092&quot;,
		:name =&gt; &quot;Extra Large Wheel Barrow&quot;,
		:description =&gt; &quot;Heavy duty wheel barrow...&quot;,
		:details =&gt; {
			:weight =&gt; 47,
			:weight_units =&gt; &quot;lbs&quot;,
			:model_num =&gt; 4039283402
		},
		:tags =&gt; [&quot;tools&quot;, &quot;equiptment&quot;, &quot;soil&quot;],
		:pricing =&gt; {
			:retail =&gt; 589700,
			:sale =&gt; 489700,
		},
		:price_history =&gt; [
			{:retail =&gt; 529700,
				:sale =&gt; 429700,
				:start =&gt; Time.utc(2010, 5, 1),
				:end =&gt; Time.utc(2010, 5, 8)
			},
			{:retail =&gt; 529700,
				:sale =&gt; 529700,
				:start =&gt; Time.utc(2010, 5, 9),
				:end =&gt; Time.utc(2010, 5, 16)
			},
		],
		:category_ids =&gt; [BSON::ObjectID(&quot;6a5b1476238d3b4dd5000048&quot;),
		BSON::ObjectID(&quot;6a5b1476238d3b4dd5000048&quot;)],
		:average_review =&gt; 4.5
	}</pre></td></tr></table></div>

<p>The document contains the basic name, sku, and description fields. There&#8217;s also the standard MongoDB object id stored in the _id field. In addition, we&#8217;ve defined a slug, &#8220;wheel-barrow-9092,&#8221; to provide a meaningful URL. MongoDB users sometimes justifiably complain about the ugliness of object ids in URLs. Naturally, we don&#8217;t want URLs looking like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://mygardensite.org/products/4c4b1476238d3b4dd5003981</pre></td></tr></table></div>

<p>Meaningful ids are so much better:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">http://mygardensite.org/products/wheel-barrow-9092</pre></td></tr></table></div>

<p>As a result, we generally recommend building out a slug field if a URL will be generated for the document. Such a field should have a unique index on it so that it can be used as a primary key. Here&#8217;s how we&#8217;d create the unique index in Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@products.create_index('slug')</pre></td></tr></table></div>

<p>If we have a unique index on slug, we&#8217;ll need to insert our product document using safe mode so that we&#8217;ll know if the insert fails. That way, we can retry with a different slug if necessary. To take an example, imagine our garden store has multiple wheel barrows for sale. We decide to start selling a new wheel barrow, so our code will need to generate a unique slug for the new product. Here&#8217;s how we perform the insert:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@products.insert({:name =&gt; &quot;Extra Large Wheel Barrow&quot;,
		:sku =&gt; &quot;9092&quot;,
		:slug =&gt; &quot;wheel-barrow-9092&quot;},
		:safe =&gt; true)</pre></td></tr></table></div>

<p>If the insert succeeds without raising an exception, we know we&#8217;ve chosen a unique slug. But if an exception is raised, our code will have to retry. In any case, the management of slugs is easy with unique indexes.</p>
<p>Continuing on, we have a key, details, that points to a subdocument containing various product details. We&#8217;ve specified the weight, weight units, and the manufacturer&#8217;s model number. But, we might store other ad-hoc attributes here as well. For instance, if we were selling seeds, we might include attributes here for the expected yield and time to harvest, and, if we were selling lawn mowers, we could include horse power, fuel type, and mulching options. The details attribute provides a nice container for these types of dynamic attributes.</p>
<p>Notice that we&#8217;re also able to store the product&#8217;s current and past prices in the same document: pricing simply points to an object containing retail and sale prices; price history, by contrast, references a whole array of pricing options. This is a common technique for versioning a document.</p>
<p>Next, we see an array of tag names for the product. Since we can index array keys, this is the simplest and best way of storing relevant tags on an item while at the same time assuring efficient queryability.</p>
<p>But what about relationships? We&#8217;re able to use rich document structures, like embedded documents and arrays, to store product details, prices, and tags¡Xall in a single document. And yet, we do eventually have to relate to documents in other collections. To start, we&#8217;re going to relate products to a category structure. This relationship between products and categories is usually conceived as many to many, where each product can belong to more than one category, and each category can contain multiple products. In an RDMBS, we&#8217;d use a join table to represent a many-to-many relationship like this. Join tables are great because they store all the relationship references between two tables in a single table. Using a SQL join, it then becomes possible to issue a single query to pull back a product and all its categories, and vice versa.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Now, MongoDB doesn&#8217;t support joins, so we need a different many-to-many strategy. Looking at our wheelbarrow document, you&#8217;ll see a field called category_ids containing an array of object ids. Each of these object ids is a reference to the _id field of some category document. For reference, you can see an example category document in listing 2.</p>
<p><strong><span style="color: red;">Listing 2 A category document</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">doc =
	{ :_id =&gt; BSON::ObjectID(&quot;6a5b1476238d3b4dd5000048&quot;),
		:slug =&gt; &quot;gardening-tools&quot;,
		:parents =&gt; [{:name =&gt; &quot;Home&quot;,
			:_id =&gt; BSON::ObjectID(&quot;8b87fb1476238d3b4dd5000003&quot;),
			:slug =&gt; &quot;home&quot;
		},
		{:name =&gt; &quot;Outdoors&quot;,
			:_id =&gt; BSON::ObjectID(&quot;9a9fb1476238d3b4dd5000001&quot;),
			:slug =&gt; &quot;outdoors&quot;
		}
		],
		:name =&gt; &quot;Gardening Tools&quot;,
		:description =&gt; &quot;Gardening gadgets galore!&quot;,
	}</pre></td></tr></table></div>

<p>If you look carefully at the object ids in the category_ids fields, you&#8217;ll see that our product is related to the &#8220;Gardening Tools&#8221; category just shown. Having the category_ids array key in the product document enables all of the kinds of queries we&#8217;d want to do on a many-to-many relationship. For instance, to query for all products in the &#8220;Gardening Tools&#8221; category, the Ruby code is quite simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">gardening_id = @category['_id']
	@products.find({:category_ids =&gt; gardening_id})</pre></td></tr></table></div>

<p>To query for all categories for our products, we pass the array of category ids using the special $in operator:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@categories.find({:_id =&gt; {&quot;$in&quot; =&gt; @product['category_ids']}})</pre></td></tr></table></div>

<p>Queries like this, with the $in operator, are not unlike SQL&#8217;s IN directive. With the many-to-many relationship out of the way, we must say a few words about the category document itself. You&#8217;ll notice the standard _id, slug, name, and description fields. These are straightforward, but the array of parent documents probably won&#8217;t be. Why are we storing such a large percentage of each parent category document? The fact is that categories are always conceived as a hierarchy and the ways of representing such a hierarchy in a database are many.</p>
<p>The strategy we choose is always dependent on the needs of the application. In this case, because MongoDB doesn&#8217;t support joins, we&#8217;ve elected to cache the parent category names in each child document. This way, when querying for the &#8220;Gardening Products&#8221; category, there&#8217;s no need to perform additional queries to get the names and URLs of the parent categories, &#8220;Outdoors&#8221; and &#8220;Home.&#8221;</p>
<p>Some developers would, no doubt, consider this level of denormalization unacceptable. In those cases, there are other options for representing a tree. For the moment, try to be open to the possibility that what best determine the schema are the demands of the application, and not necessarily the dictates of theory.</p>
<h3>Users and orders</h3>
<p>Looking at how we model users and orders illustrates another common relationship: one to many. In this case, a user is said to have many orders. In an RDBMS, we&#8217;d use a foreign key in our orders table; here, the convention is similar. Examine listing 3.</p>
<p><strong><span style="color: red;">Listing 3 An e-commerce order as a document with line items, pricing, and a shipping address</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">doc =
	{ :_id =&gt; BSON::ObjectID(&quot;6a5b1476238d3b4dd5000048&quot;)
		:user_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5000001&quot;)
		:state =&gt; &quot;CART&quot;,
		:line_items =&gt; [
			{ :_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5003981&quot;),
				:sku =&gt; &quot;9092&quot;,
				:name =&gt; &quot;Extra Large Wheel Barrow&quot;,
				:pricing =&gt; {
					:retail =&gt; 5897,
					:sale =&gt; 4897,
				}
			},
			{ :_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5003981&quot;),
				:sku =&gt; &quot;10027&quot;,
				:name =&gt; &quot;Rubberized Work Glove, Black&quot;,
				:pricing =&gt; {
					:retail =&gt; 1499,
					:sale =&gt; 1299,
				}
			}
		],
		:shipping_address =&gt; {
			:street =&gt; &quot;588 5th Street&quot;,
			:city =&gt; &quot;Brooklyn&quot;,
			:state =&gt; &quot;NY&quot;,
			:zip =&gt; 11215
		},
		:subtotal =&gt; 6196
	}</pre></td></tr></table></div>

<p>The second attribute, user_id, stores a given user&#8217;s _id. That points to our sample user, shown in listing 4 (we discuss this listing presently).</p>
<p><strong><span style="color: red;">Listing 4 A user document with addresses and payment methods</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">doc =
	{ :_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5000001&quot;)
		:username =&gt; &quot;kbanker&quot;,
		:first_name =&gt; &quot;Kyle&quot;,
		:last_name =&gt; &quot;Banker&quot;,
		:hashed_password =&gt; &quot;bd1cfa194c3a603e7186780824b04419&quot;,
		:addresses =&gt; [
			{:name =&gt; &quot;home&quot;,
				:street =&gt; &quot;588 5th Street&quot;,
				:city =&gt; &quot;Brooklyn&quot;,
				:state =&gt; &quot;NY&quot;,
				:zip =&gt; 11215},
			{:name =&gt; &quot;work&quot;,
				:street =&gt; &quot;1 E. 23rd Street&quot;,
				:city =&gt; &quot;New York&quot;,
				:state =&gt; &quot;NY&quot;,
				:zip =&gt; 10010},
		],
		:payment_methods =&gt; [
			{:name =&gt; &quot;VISA&quot;,
				:last_four =&gt; 2127,
				:crypted_number =&gt; &quot;43f6ba1dfda6b8106dc7&quot;,
				:expiration_date =&gt; Time.utc(2014, 5)
			}
		]
	}</pre></td></tr></table></div>

<p>This modeling arrangement makes it easy to query either side of the relationship. To find all orders for a given user is simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">@orders.find({:user_id =&gt; some_user_id})</pre></td></tr></table></div>

<p>The query for getting the user for a particular order is equally simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">user_id = @order['user_id']
	@users.({:_id =&gt; user_id})</pre></td></tr></table></div>

<p>Thus, a one-to-many relationship between orders and users is easily accomplished using a foreign key. Let&#8217;s look at some other salient aspects of our order document. In general, we&#8217;re using the rich representation afforded to us by the document data model to represent the object holistically. You&#8217;ll see that the document includes both the line items and the shipping address, which, in a normalized relational model, would be located in separate tables. Our line items consist of an array of subdocuments, each describing a product in our shopping cart. The shipping address attribute points to a single object containing address fields.</p>
<p>Let&#8217;s take just a moment to discuss the merits of this representation. First, there&#8217;s a win for the human mind. Our entire concept of an order, including line items, shipping address, and, eventually, payment information, can be encapsulated in a single entity. When querying the database, we can return the entire order object without a single join. What&#8217;s more, the products, as they appeared when purchased, are effectively captured within our order document. The order document can be easily queried and modified.</p>
<p>The user document presents similar patterns in storing a list of address document along with a list of payment method documents. In addition, at the top level of the document, we store the basic attributes common to any user model.</p>
<p>And, like the slug field on our product, the username field here would have a unique index.</p>
<h3>Reviews</h3>
<p>We&#8217;re going to close out our sample data model with product reviews. Relationally speaking, each product has many reviews. Here, that relationship is encoded using a foreign key, review_id, which you can see in our sample review document in listing 5.</p>
<p><strong><span style="color: red;">Listing 5 A document representing a product review</span></strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">doc =
	{ :_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5000041&quot;),
		:product_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5003981&quot;),
		:date =&gt; Time.at(2010, 6, 7),
		:title =&gt; &quot;Amazing&quot;,
		:text =&gt; &quot;Has a squeaky wheel, but still the best wheel barrow I've ever owned.&quot;,
		:rating =&gt; 4,
		:user_id =&gt; BSON::ObjectID(&quot;4c4b1476238d3b4dd5000041&quot;),
		:username =&gt; &quot;dgreenthumb&quot;,
		:helpful_votes =&gt; 3,
		:voter_ids =&gt; [ BSON::ObjectID(&quot;4c4b1476238d3b4dd5000041&quot;),
			BSON::ObjectID(&quot;7a4f0376238d3b4dd5000003&quot;),
			BSON::ObjectID(&quot;92c21476238d3b4dd5000032&quot;)
		]
	}</pre></td></tr></table></div>

<p>Most of the remaining attributes are self-explanatory: we store the review&#8217;s date, title, and text; the rating provided by the user; and the user&#8217;s id. But it may come as a surprise that we store the username as well. After all, if this were an RDBMS, we&#8217;d be able to pull in the username with a join on the users&#8217; table. Since we don&#8217;t have the join option with MongoDB, we can proceed in one of two ways: either query against the users collection for each review or accept some denormalization. Issuing a query for every review seems unnecessarily costly when the attribute being queried, the username, is extremely unlikely to change. So it makes sense to take the denormalized route. This does mean that a username update is more expensive, since a username will have to change in every place that it appears but, again, that happens infrequently enough to justify this design choice.</p>
<p>Also noteworthy is our decision to store votes in the review document itself. Of course, it&#8217;s common for users to be able to vote on reviews. Here, we store the object id of each voting user in an array of voter ids. This allows us to prevent users from voting on a review more than once, and it also gives us the ability to query for all the reviews a user has voted on. Notice that we also cache the total number of helpful votes, which allows, among other things, sorting of reviews based on helpfulness. With that, we&#8217;ve covered a basic data model for e-commerce.</p>
<h2>Summary</h2>
<p>We&#8217;ve covered a lot of ground in this article! We began with a theoretical discussion of schema design and then proceeded to outline the data model for an e-commerce application. This gave you a chance to see what documents might look like in a production system and it should have gotten you thinking in a more concrete way about the differences between schemas in RDMBSs and MongoDB. Granted, some of the rationale for the design of these documents won&#8217;t become clear until you get more experience with MongoDB&#8217;s query and update powers. But the general pattern of using a rich document data structure should be coming to the fore.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/mongodb-vs-rdbms-schema-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Liferay&#8217;s Service Builder, part 2</title>
		<link>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-2/</link>
		<comments>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-2/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 17:21:34 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2381</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on Liferay in Action, to be published on August, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/sezov/" target="_blank">Liferay in Action</a>, to be published on August, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<h2>Introduction</h2>
<p>Liferay&#8217;s code generator for database persistence, which is called Service Builder, jump-starts portlet development. This utility (which ships as part of Liferay) creates code and SQL for accessing a database from within portlets. Since it uses Spring and Hibernate to implement this, it&#8217;s not much different from what you would already do manually, with the important exception that it does much of this &#8220;grunt work&#8221; automatically, freeing time for you to implement your business logic.</p>
<p>In this article, we&#8217;ll continue working with the data layer of a product registration portlet for a fictional company named Inkwell, which we started in part 1 of this series. In part 1, you used Service Builder to generate your persistence objects. Now we&#8217;ll see how to use that persistence layer to store and retrieve objects.</p>
<h2>How Service Builder works</h2>
<p>As shown in figure 1, Service Builder automatically generates several files for you. Among them are both the Data Access Object and the Data Transfer Object layers.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-12.jpg"><img class="aligncenter size-medium wp-image-2383" title="liferay-in-action-1" src="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-12-112x300.jpg" alt="" width="112" height="300" /></a></p>
<p><center><span style="color: red;">Figure 1 Service Builder generates for you everything from the service layer down. You might need to modify/customize some of this code, but you won&#8217;t have to touch most of it. This significantly increases developer productivity.</span></center>The layer you&#8217;ll work with for the most part is the Data Transfer Objects (DTO) layer. This layer talks to the Service Layer, which is automatically generated and allows you to work with objects that need to be persisted or have been retrieved from the database. These are the -Impl classes that you generated in part 1 of this series, and this is the reason why they were placed in your src folder with the rest of your portlet code. You&#8217;ll call methods from the Data Access Objects in the persistence layer to do the actual persisting. These methods are generated from the &lt;finder&gt; tags you placed in your service.xml file for Inkwell&#8217;s product registration portlet.</p>
<p>Let&#8217;s see how this actually works.</p>
<h2>Implementing the DTO layer</h2>
<p>Though not created yet, Inkwell&#8217;s design calls for a form in Edit mode of the portlet. This form allows users to enter products that appear in a dropdown selection box in the actual registration form that makes up the portlet&#8217;s View mode. Only two fields will be coming from this form because the third is the primary key, which will be generated:</p>
<ul>
<li>Product name</li>
<li>Product&#8217;s serial number mask, which is set by the manufacturing department and can be used for field validation</li>
</ul>
<p>So, how do we get the data the user entered into the database? Remember, we&#8217;re after the idea of loose coupling. This means that we don&#8217;t want to have any database insertion code in our business logic. So our business logic (when we get to it) will need to call a generic method that has nothing to do with the database. This is where the DTO layer comes in. It acts as a buffer between your business logic and your underlying database code. Service Builder generates both layers, leaving the DTO layer as a stub for you to implement as you wish.</p>
<h2>Create a generic method</h2>
<p>Presumably, we&#8217;ll want a method that has two parameters in the method signature for our two values—the product name and the product&#8217;s serial number mask. So let&#8217;s create it.</p>
<p>Open the file ProductLocalServiceImpl.java. You&#8217;ll find this in a newly generated package called com.inkwell.internet.productregistration.service.impl. This class extends a generated class called ProductLocalServiceBaseImpl (see figure 2).</p>
<p>If you&#8217;re using an IDE (such as Eclipse) that does not automatically recognize when another tool adds files, you may need to click on the project name and press F5 to refresh the project in order to see this package.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-2.jpg"><img class="aligncenter size-medium wp-image-2382" title="liferay-in-action-2" src="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-2-258x300.jpg" alt="" width="258" height="300" /></a></p>
<p><center><span style="color: red;">Figure 2 Service Builder generates both your DAO and DTO layer. Shown above is the DTO layer, which has an instance of the DAO layer (the productPersistence attribute) injected into it by Spring.</span></center>The first thing we&#8217;ll implement in ProductLocalServiceImpl is adding a product. Once you see how that&#8217;s done, implementing the other methods will be very simple.</p>
<h2>Adding a product</h2>
<p>Another great thing about the design of Service Builder is that you never have to touch or modify any of the classes it generates. All of the Spring dependency injection, the Hibernate session management, and the query logic stay in classes you never have to touch. You add code in a class that extends the generated class and, if you add something that changes the interface/implementation contract, those changes are propagated up the chain so the contract is never broken. We&#8217;ll see an example of this a bit later. For now, you&#8217;ll see that this file is just an empty stub. This is because you have yet to implement anything. So we&#8217;ll implement our first method, which will take the values passed to it and call the underlying database code to persist the data to the database (listing 1).</p>
<p><span style="color: red;">Listing 1 Adding a product to the database</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public Product addProduct(PRProduct newProduct, long userId)
throws SystemException, PortalException {
	PRProduct product = prProductPersistence.create(counterLocalService.increment(PRProduct.class.
	GetName())); 1
	resourceLocalService.addResources(newProduct.getCompanyId(),
	newProduct.getGroupId(), userId,
	Product.class.getName(), product.getPrimaryKey(), false, true, true); 2
	product.setProductName(newProduct.getProductName());
	product.setSerialNumber(newProduct.getSerialNumber());
	product.setCompanyId(newProduct.getCompanyId());
	product.setGroupId(newProduct.getGroupId());
	return prProductPersistence.update(product, false);
}
&nbsp;
&nbsp;
#1 Create empty object
#2 Create permissions resources</pre></td></tr></table></div>

<p>The first thing we do (#1) in this method is create a new Product object. Product is the Interface (inherited from PRProductModel), and PRProductImpl (which extends PRProductModelImpl) is the implementation. Note that all we have to do is specify the Interface here since a Factory design pattern is used to create the object for us using the prProductPersistence object that has been injected into this class by Spring. The interface and implementation of this object were both generated automatically by Service Builder, and the fields within the object map directly to the fields in the PRProduct table that we defined. To obtain a new instance of this object, we call a create method, which was also generated by Service Builder.</p>
<p>Because Liferay is database agnostic, it does not use any database-specific means of generating primary keys for its database. Instead, it provides its own utility for generating primary keys. Since the create method that was generated requires a primary key, we need to call Liferay&#8217;s Counter utility to generate this. Here&#8217;s the cool thing about how this is done: the Counter is automatically injected into the class we&#8217;re working on. Why? Because, if you&#8217;re doing work with databases and Service Builder, you&#8217;re 100 percent likely to need the Counter to create new objects that will be persisted.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Notice that in #2, we next make a call to resourceLocalService to persist resources. Resources are used to define permissions on the objects that are persisted. We added CompanyId and GroupId fields to our tables in order to make our portlet a non-instanceable portlet and to enable us to later implement Liferay&#8217;s permissions system.</p>
<p>These two variables track the portal instance and the community/organization, respectively. For example, say a user adds our portlet to a page in the Guest community of the default portal instance. Users then begin adding Products to the database. Because we&#8217;ve added the Company ID and the Group ID to the product entities, users can add our portlet to another community, organization, or portal instance and the set of records returned will be different based on the Company ID and Group ID. Our Finder methods filter the records being returned by Company ID and Group ID.</p>
<p>This is how Liferay allows you to place, for example, a Message Boards portlet in two different communities, and have completely different content in each. We&#8217;ll do the same thing as we write our portlet.</p>
<p>Once we have our object, it&#8217;s a simple matter to set the proper values in the object and then persist it to the database. We then return the object that we&#8217;ve created back to the layer that called this method in the first place.</p>
<p>But we&#8217;re not quite done yet.</p>
<h2>Propagating your changes up the chain</h2>
<p>Recall that changes to the interface/implementation contract get propagated up the chain automatically by Service Builder. We were just working in an implementation class, which extends another class, which we call a -BaseImpl class. All of the methods generated by Service Builder have entries in their Interfaces and actual implementations in their BaseImpl class. If developers wish to add more, they&#8217;re added in the -LocalServiceImpl class.</p>
<p>Because we&#8217;ve just added a new method to a class that implements an Interface, there is no method stub in the Interface for the method we just created. To continue without errors, we need to run Service Builder again. This can be done by running the build-service Ant task. When you run the task, Service Builder will regenerate the interface, which is called PRProductLocalService, to include a stub for the method you created in the implementation class.</p>
<p>This is generally the point where people scratch their heads and say, &#8220;Isn&#8217;t this backwards? Aren&#8217;t you supposed to define the Interface first and then write the implementation?&#8221;</p>
<p>In a sense, yes, that&#8217;s correct. But, remember that we&#8217;re working with a code generator, and it&#8217;s that code generator&#8217;s job to make things easier for us as developers. By using Service Builder, we are defining the Interface first, as much as we can do that up front. We did this in one step, when we defined our database table in service.xml. Service Builder generated our Interface as well as our default implementation—as best as it could guess at what we needed. That&#8217;s what went in PRProductLocalServiceBaseImpl.java. If you need further customization—and you generally do—you can add your own methods. These need to be added to a class that is free of the code generator, so that your changes don&#8217;t get overwritten. So PRProductLocalServiceImpl.java is provided as a DTO layer, which allows you to add any methods you may need.</p>
<p>You&#8217;ll generally only need to make customizations in one class: the -LocalServiceImpl class. In some cases, you may need to make customizations in -Impl classes in the model.impl package. The only thing we need to worry about right now is the -LocalServiceImpl class.</p>
<p>Now that we&#8217;re done with adding products, let&#8217;s try deleting products.</p>
<h2>Deleting Products</h2>
<p>If you think about how you might want to delete products, there are two ways:</p>
<ul>
<li>You already have a Product object, and you just want to delete it.</li>
<li>You have a Product&#8217;s primary key, and you want to delete it that way without having to retrieve the whole Product.</li>
</ul>
<p>To make things easier for us in the Controller layer, we&#8217;ll overload the delete method by creating versions of it that can handle both cases:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public void deleteProduct(long productId, long companyId)
throws NoSuchProductException, SystemException, PortalException {
	PRProduct product = prProductPersistence.findByPrimaryKey(productId);
	deleteProduct(product, companyId);
}
public void deleteProduct(Product product, long companyId)
throws PortalException, SystemException {
	resourceLocalService.deleteResource(
	companyId, Product.class.getName(),
	ResourceConstants.SCOPE_INDIVIDUAL, product.getPrimaryKey());
	productPersistence.remove(product);
}</pre></td></tr></table></div>

<p>What we&#8217;ve done here is enable us to delete a PRProduct using its primary key or the object itself. If we&#8217;re using the primary key, we&#8217;ll first retrieve the PRProduct object and then call deleteProduct on it. Note that we delete the resource as well as deleting the product. This keeps Liferay&#8217;s permissions tables free of &#8220;dead&#8221; data that might otherwise be left there simply taking up space. When you&#8217;re finished modifying your -Impl class, run Service Builder again using ant build-service. As before, this adds your new methods to the interface.</p>
<p>The only thing remaining to do is to query the database for the products we&#8217;ve entered because, presumably, users will want to view them and/or edit them.</p>
<h2>Querying the database</h2>
<p>Just like adding and deleting, getting products out of the database is really easy. You&#8217;ll probably remember that we had Service Builder generate a finder for this, which queries the database for products by the GroupId. We always want to query by GroupId because our portlet is non-instanceable. So we&#8217;ll implement this in our DTO layer like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">public List&lt;PRProduct&gt; getAllProducts(long groupId)
throws SystemException {
	List&lt;Product&gt; products = prProductPersistence.findByGroupId(groupId);
	return products;
}</pre></td></tr></table></div>

<p>This will return a List of PRProducts, which can be used in the UI layer to display products for viewing or editing purposes. And, as you can see, whatever calls this method (it will be our portlet class) does not need to know anything about JDBC or databases. It&#8217;s simply requesting a List. This frees you to do something like swap out Service Builder later, if you find that it doesn&#8217;t meet your needs.</p>
<h2>Summary</h2>
<p>Service Builder makes it easy to generate a whole persistence layer of an application. Using well-known design patterns such as Data Access Objects and Data Transfer Objects, it not only implements a consistent design but also helps the developer to do so. Finders are automatically generated, which allow developers to access their data as Java objects.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Liferay&#8217;s Service Builder, part 1</title>
		<link>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-1/</link>
		<comments>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-1/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 17:19:43 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2376</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on Liferay in Action, to be published on August, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/sezov/" target="_blank">Liferay in Action</a>, to be published on August, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<p><center></p>
<h2>Using Liferay&#8217;s Service Builder, part 1</h2>
<p></center></p>
<h2>Introduction</h2>
<p>If you&#8217;re an experienced developer, it&#8217;s likely that you&#8217;ve dealt with a data-driven application before. Liferay ships with a tool called Service Builder, which makes the creation of data-driven applications very easy. I highly recommend that you use Service Builder when writing applications on Liferay&#8217;s platform because it will get you going very quickly. How? By generating a lot of the database plumbing code for you so you can concentrate on your application&#8217;s functionality.</p>
<h2>A database-persistence code generator</h2>
<p>With <strong>Service Builder</strong>, you can simply define a database table in an XML file. Based on that definition, <strong>Service Builder</strong> then generates the entire <strong>Hibernate configuration</strong>, the entire Spring configuration, finder methods, your model layer, the SQL to create the table on all leading databases, and your entire Data Access Object layer at one fell swoop. Figure 1 illustrates this.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-1.jpg"><img class="aligncenter size-medium wp-image-2377" title="liferay-in-action-1" src="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-1-300x268.jpg" alt="" width="300" height="268" /></a></p>
<p><center><span style="color: red;">Figure 1 <strong>Service Builder</strong> is a tool that sits on top of Hibernate and Spring, automatically generating both configurations for you, along with the Java code necessary to persist your entities to the database.</span></center>This tool is an excellent database-persistence code generator, which makes it easy to define new tables and to manipulate the data through select, insert, update, and delete operations. <strong>Service Builder</strong> uses a combination of standard technologies—Hibernate and Spring—that Java developers use every day.</p>
<h2>Did you know?</h2>
<p><strong>Service Builder</strong> is a proven tool that produces code suitable for enterprise deployments. In fact, all of Liferay&#8217;s internal database persistence is generated using Service Builder.</p>
<p>If you&#8217;re like me, I know what popped into your head as soon as I said &#8220;code generator.&#8221; It was, &#8220;Oh, no. Code generators are bad.&#8221; And then you began justifying that statement with many sound, accurate, and excellent arguments. Believe me, I agree with you. But Service Builder is different. From one code generator &#8220;hater&#8221; to another: <strong>Service Builder</strong> is designed to enable you to write custom code, not prevent it. It just takes care of the mundane stuff you hate writing, anyway. That is a code generator I&#8217;m on board with. I think you&#8217;ll like it, too.</p>
<p>We&#8217;re going to create a product registration application for a fictional company named Inkwell. This application is designed to replace those warranty registration cards that ship with electronic equipment. You&#8217;ll use Service Builder to generate the database tables as well as database persistence. To start, you&#8217;ll create that one XML file that is the key to generating the rest of the code.</p>
<h2>Creating the service.xml file</h2>
<p>Create a file called service.xml in the WEB-INF folder of your project. This file will contain your table definitions. You&#8217;ll populate this file with all the information Service Builder needs to generate the SQL for the table—for all the databases Liferay supports as well as database persistence objects you can use in your Java code.</p>
<p>We&#8217;ll use the simplest of the tables as our example, which is the Product table. The following listing defines the table for Service Builder.</p>
<p><span style="color: red;">Listing 1 Defining the Product table using Service Builder</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;!DOCTYPE service-builder PUBLIC &quot;-//Liferay//DTD Service Builder 6.0.0//EN&quot; &quot;http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd&quot;&gt;
&lt;service-builder package-path=&quot;com.inkwell.internet.productregistration&quot;&gt; 1
&lt;author&gt;Rich Sezov&lt;/author&gt; 2
&lt;namespace&gt;PR&lt;/namespace&gt; 3
&lt;entity name=&quot;PRProduct&quot; local-service=&quot;true&quot; remote-service=&quot;false&quot;&gt; 4
&lt;column name=&quot;productId&quot; type=&quot;long&quot; primary=&quot;true&quot; /&gt; 5
&lt;column name=&quot;productName&quot; type=&quot;String&quot; /&gt; 6
&lt;column name=&quot;serialNumber&quot; type=&quot;String&quot; /&gt;
&lt;column name=&quot;companyId&quot; type=&quot;long&quot; /&gt; 7
&lt;column name=&quot;groupId&quot; type=&quot;long&quot; /&gt;
&lt;order by=&quot;asc&quot;&gt; 8
&lt;order-column name=&quot;productName&quot; /&gt;
&lt;/order&gt;
&lt;finder name=&quot;G_PN&quot; return-type=&quot;Collection&quot;&gt; 9
&lt;finder-column name=&quot;groupId&quot; /&gt;
&lt;finder-column name=&quot;productName&quot; /&gt;
&lt;/finder&gt;
&lt;finder name=&quot;GroupId&quot; return-type=&quot;Collection&quot;&gt;
&lt;finder-column name=&quot;groupId&quot; /&gt;
&lt;/finder&gt;
&lt;finder name=&quot;CompanyId&quot; return-type=&quot;Collection&quot;&gt;
&lt;finder-column name=&quot;companyId&quot; /&gt;
&lt;/finder&gt;
&lt;/entity&gt;
&lt;/service-builder&gt;
&nbsp;
#1 Java package for code
#2 Author for JavaDoc
#3 Namespace for tables
#4 Name of entity
#5 Primary key
#6 Additional fields
#7 Foreign Keys
#8 Order data is returned
#9 Finder methods</pre></td></tr></table></div>

<p>This file is pretty easy to read, so we&#8217;ll attack it section by section instead of line by line.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<h2>DEFINE JAVA PACKAGE</h2>
<p>You can define the Java package (#1) into which Service Builder generates the code that powers this database table. Service Builder also generates JavaDoc, and the name you place in the Author tags (#2) will wind up in the JavaDoc as the author of the code. By default, tables you define with Service Builder will go in the Liferay database. To set them apart from the rest of Liferay&#8217;s tables, you can prefix them with a namespace (#3). This table, when created, will actually be called PR_PRProducts in the database.</p>
<h2>DEFINE ENTITY</h2>
<p>Now we get to the cool stuff. The database entity—which, for us, is a Product—is defined using the Entity tag (#4). The two parameters in this tag define how you want Service Builder to generate the service that retrieves and saves these entities. At minimum, you need to have a local service, but you can also have a remote service. This is not an EJB. It is instead a web service, complete with a WSDL document describing it so that your service may participate as part of a Services Oriented Architecture (SOA).</p>
<h2>DEFINE COLUMNS</h2>
<p>Next, we define the columns and finder methods. Our first column (#5) is defined as a primary key in the database. Other fields we want to store come next (#6). We are also defining two foreign keys (#7): a companyId and a groupId. The DBA team did not specify these two foreign key fields in the tables, but we added them anyway. These fields are internal to Liferay and are used for context purposes in non-instanceable portlets. The companyId corresponds to the portal to which the user has navigated, and the groupId corresponds to the community or organization to which the user has navigated. Since we will be using these field values as parameters in all of our queries, our portlet will have different data in different portals, communities, and organizations.</p>
<h3>Quick test</h3>
<p>Without reading the next sentence, answer this question: Is our portlet instanceable or non-instanceable? The portlet is non-instanceable because we&#8217;ll be using these fields to make sure that the portlet ties all data to the portal and to the community or organization upon which the portlet is placed.</p>
<p>#8 defines a default ordering of the entities when they are retrieved from the database. You can choose to have them returned in ascending or descending order. You aren&#8217;t limited to one column here; you can specify multiple columns, and the ordering will happen by priority in order of the columns.</p>
<h2>DEFINE FINDER METHODS</h2>
<p>The finder methods (#9) actually go and retrieve the objects. Specifying these finders means that Service Builder will automatically generate methods that retrieve objects from the database using the parameters you specify in the finder. For example, the first finder returns Products by groupId and productName.<br />
Now that you&#8217;ve defined your table, it&#8217;s time to run Service Builder.</p>
<h2>Running Service Builder</h2>
<p>Save the service.xml file and then run the Ant task called build-service. You&#8217;ll see several files get generated and the task will complete with a BUILD SUCCESSFUL message. If you&#8217;re using an IDE that compiles source files automatically, you&#8217;ll notice errors suddenly appear in your project. Don&#8217;t worry about this; it&#8217;s easy to fix.</p>
<p>What has happened is that Service Builder generated several Java source files. Some of them are in your existing src folder. Others are in a new source folder called service that it also generated. This folder will resideinside of WEB-INF like the src folder you already have. To fix the errors in your project, just use your IDE&#8217;s facility to add another source folder to your project. You may also need to refresh your project. Once you do that, the errors will go away.</p>
<p>Service Builder divides the source it generates into two layers:</p>
<ul>
<li>An interface layer gets generated in the aforementioned service folder. You&#8217;ll never change anything in the interface layer manually; Service Builder always generates the code that&#8217;s found there.</li>
<li>An implementation layer gets generated in your src folder and is initially just skeleton code that allows you to implement the functionality you need.</li>
</ul>
<p>You&#8217;ll notice that there&#8217;s also a new file in the root of your src folder called service.properties. This file was also generated by Service Builder. It contains properties that Service Builder needs at runtime to perform its functions. The most important of these properties is a list of Spring configuration files that were also generated.</p>
<p>Another new construct that was generated was a META-INF folder in your src folder. This folder contains all of the XML configuration files that Service Builder needs, including Spring configuration files and Hibernate configuration.</p>
<p>What all of this means is that Service Builder has taken care of your database persistence configuration for you. If you&#8217;ve ever used Hibernate alone or the combination of Hibernate and Spring, you know that there are multiple configuration files to juggle between the two. Service Builder automatically configures all of that for you and provides you with static classes that you can use to perform your database persistence functions. It provides both a Data Access Object (DAO) layer and a Data Transfer Object (DTO) layer for you automatically.</p>
<h2>Summary</h2>
<p>Liferay&#8217;s code generator for database persistence, which is called Service Builder, jump-starts portlet development. This utility (which ships as part of Liferay) creates code and SQL for accessing a database from within portlets. Since it uses Spring and Hibernate to implement this, it is not much different from what developers would already do manually, with the important exception that it does much of this &#8220;grunt work&#8221; automatically, freeing time for developers to implement their business logic.</p>
<p>In part 2 of this 2-part series, I&#8217;ll show you how to provide the functionality in your DTO to keep your portlet code from being dependent on anything related to SQL databases.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/using-liferays-service-builder-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loosely Coupled Data Persistence with Liferay</title>
		<link>http://www.javabeat.net/2011/06/loosely-coupled-data-persistence-with-liferay/</link>
		<comments>http://www.javabeat.net/2011/06/loosely-coupled-data-persistence-with-liferay/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 17:19:32 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2375</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on Liferay in Action, to be published on August, 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/sezov/" target="_blank">Liferay in Action</a>, to be published on August, 2011. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<h2>Introduction</h2>
<p><strong>Liferay&#8217;s Service Builder</strong> encourages the proper layering of functionality within a portlet application. Generally, when building an application, it&#8217;s good practice to separate the various layers of the application: UI, model, persistence, and so on. This is sometimes called separation of concerns. By keeping the layers as separate as possible, you gain the ability to change the implementation of any one layer more easily—if for some reason you find a better way to do it later.</p>
<p>To some, this must seem like some unnecessary work, but let me give you an example as to why this is important.</p>
<h2>The spaghetti code story</h2>
<p>Suppose you have a poorly designed application that consists of only two layers—your JSPs and your portlet class. You develop the application by creating action URLs in your JSPs, which correspond to various methods within the portlet. These methods use JDBC to connect to a database and select, insert, update, and delete data. Your database, validation, and page navigation code is all in the portlet or the JSPs, resulting in very large files with lots of logic in them. You test the portlet as well as you can, and it then gets deployed to production.</p>
<p>Within the first day of its use, a bug is found. Your application has a last name field, and a user tried to insert the last name O&#8217;Bannon. Because you coded all of your database interaction manually, you forgot that sometimes you need to escape out certain characters. In this case, the apostrophe in the name is causing your database insert to fail.</p>
<p>With the poor design you used for your portlet, you&#8217;ll now have to modify the Insert and Update methods of your portlet class and add some code that checks the values your users are inserting for apostrophes. This is an example of a tightly-coupled design: your database code is inside your portlet, so your implementation of code that communicates with the database is intertwined with your business and display logic. Because you were recently working on this portlet, you know where the problem is and so you fix it and redeploy the application. The users work with it for several months before another bug is found. It seems that, somewhere in the portlet, a database connection isn&#8217;t being closed and is causing the database server to run out of connections.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p>Now some significant time has passed, and you&#8217;re onto another project. You don&#8217;t remember exactly what you did when you wrote this one. You now have to slog through mounds of spaghetti code in your one portlet class looking for the problem—database code mixed in with all kinds of other functionality, such as JavaScript functions, field validation code, database access code, and so on. It&#8217;s become a hard-to-maintain mess. After hours of debugging, you finally find the condition where the database connection wasn&#8217;t being closed and it was part of a long block of if-then-else conditions, which included field validation logic, business logic, and persistence logic. It took you almost an entire day to find this one problem, because it was buried in a lot of code that should have been separated out into different layers of code logic.</p>
<p>If you had properly layered your application so that the UI code was in one place, the business logic was another, and the persistence logic was in another, you&#8217;d have a much more maintainable project. And you might decide at that point that you could use something like <strong>Hibernate</strong> for your database persistence, and you&#8217;d only have to replace a few classes in the one layer. Doing that would solve all of your persistence problems, because Hibernate escapes characters and closes connections automatically. You might then go another step further and replace your homegrown application logic with an MVC framework like <strong>JSF or Struts</strong>. On each refactor, you could modify one layer of your application at a time. Let&#8217;s see how to do that for the persistence layer.</p>
<h2>Using two layers for persistence</h2>
<p>While the above is a simplistic example, it serves as an example we can use to show how <strong>Service Builder</strong> encourages proper separation of concerns. For <strong>database persistence</strong>, two of these layers are the <strong>Data Access Object</strong> and the <strong>Data Transfer Object</strong> (see figure 1). These two layers can be written by developers manually, and often a lot of time is spent writing and debugging code in this layer. <strong>Service Builder</strong> automatically generates both layers for you.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-11.jpg"><img class="aligncenter size-medium wp-image-2378" title="liferay-in-action-1" src="http://www.javabeat.net/wp-content/uploads/2012/04/liferay-in-action-11-113x300.jpg" alt="" width="113" height="300" /></a></p>
<p><center><span style="color: red;">Figure 1 Service Builder generates for you everything from the service layer down. You&#8217;ll need to modify/customize some of this code, but you won&#8217;t have to touch most of it. This significantly increases developer productivity.</span></center>With <strong>Liferay</strong>, the layer you&#8217;ll work with for the most part is the <strong>Data Transfer Objects (DTO)</strong> layer. This layer talks to the Service Layer, which is automatically generated, and allows you to work with objects that need to be persisted or have been retrieved from the database. You&#8217;ll call methods from the <strong>Data Access Objects (DAO)</strong> in the <strong>persistence layer</strong> to do the actual persisting. In another article, I&#8217;ll walk through how this actually works.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/06/loosely-coupled-data-persistence-with-liferay/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing the UIAccelerometer in Iphone and iPad</title>
		<link>http://www.javabeat.net/2011/05/accessing-the-uiaccelerometer-in-iphone-and-ipad/</link>
		<comments>http://www.javabeat.net/2011/05/accessing-the-uiaccelerometer-in-iphone-and-ipad/#comments</comments>
		<pubDate>Mon, 30 May 2011 17:18:41 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2367</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on iPhone and iPad in Action, published on August, 2010. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/trebitowski/" target="_blank">iPhone and iPad in Action</a>, published on August, 2010. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<p><center></p>
<h2>Accessing the UIAccelerometer</h2>
<p></center></p>
<h2>Introduction</h2>
<p>When you&#8217;re using the iPhone&#8217;s orientation notification, the frameworks are doing the work for you: they&#8217;re taking low-level acceleration reports and turning them into more meaningful events. It&#8217;s similar to the concept of iPhone actions, which turn low-level touch events into high-level control events.</p>
<p>Notifications aren&#8217;t going to be sufficient if you would prefer to program entire interfaces that effectively use the iPhone&#8217;s movement in three-dimensional space as a new user-input device. For that, you&#8217;ll need to access two iPhone classes: UIAccelerometer and UIAcceleration. We&#8217;ll talk about UIAccelerometer.</p>
<p>The UIAccelerometer is a class that you can use to receive acceleration-related data. It is a shared object, like UIApplication and UIDevice. The process of using it is shown in listing 1.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p><span style="color: red;">Listing 1 Accessing the UIAccelerometer takes just a few steps</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">- (void)viewDidLoad {
UIAccelerometer *myAccel =
#1
[UIAccelerometer sharedAccelerometer]; #1
myAccel.updateInterval = .1; #2
myAccel.delegate = self; #3
[super viewDidLoad];
}
#1 Creates shared accelerometer
#2 Updates 10 times a second
#3 Delegates accelerometer protocol</pre></td></tr></table></div>

<p>The first step is to access the accelerometer, which is done with another call to a shared-object method (#1). Having this step on its own line is probably unnecessary, because you could perform the other two steps as nested calls, but we find this a lot more readable.</p>
<p>Next, you select your update interval (#2), which specifies how often you&#8217;ll receive information on acceleration. This is hardware limited, with a current default of 100 updates per second. That&#8217;s most likely just right if you&#8217;re creating a game using the accelerometer, but excessive for other purposes. We&#8217;ve opted for 10 updates per second, which is an updateInterval of 0.1. You should always set the lowest acceptable input to preserve power on the iPhone.</p>
<p>Finally, you must set a delegate for the accelerometer (#3), which is how you receive data on accelerometer changes. The delegate will need to respond to only one method, accelerometer:didAccelerate:, which sends a message containing a UIAcceleration object whenever acceleration occurs (to the limit of the updateInterval).</p>
<h2>Summary</h2>
<p>The iPhone&#8217;s accelerometers can give you access to a variety of information about where an iPhone exists in space. By measuring gravity, you can easily discover an iPhone&#8217;s precise orientation. By measuring movement, you can see how an iPhone is being guided through space. Finally, you can build more complex movements into three-dimensional gestures, such as the shake.</p>
<p>Accelerometers allow users to make simple adjustments to a program. We can imagine game controls and painting programs both built entirely around the accelerometers.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/05/accessing-the-uiaccelerometer-in-iphone-and-ipad/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adding a map to an application in iPhone and iPad</title>
		<link>http://www.javabeat.net/2011/05/adding-a-map-to-an-application-in-iphone-and-ipad/</link>
		<comments>http://www.javabeat.net/2011/05/adding-a-map-to-an-application-in-iphone-and-ipad/#comments</comments>
		<pubDate>Mon, 30 May 2011 17:17:44 +0000</pubDate>
		<dc:creator>krishnas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.javabeat.net/?p=2365</guid>
		<description><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><p>This article is based on iPhone and iPad in Action, published on August, 2010. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all [...]</p>]]></description>
				<content:encoded><![CDATA[<p>Connect to us ( <a href="https://twitter.com/javabeat">@twitter</a> | <a href="https://www.facebook.com/javabeat.net">@facebook )</p><div class="wpInsert wpInsertInPostAd wpInsertLeft" style="float: left; margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Rect */
google_ad_slot = "9976259118";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div><p><em>This article is based on <a href="http://www.manning.com/trebitowski/" target="_blank">iPhone and iPad in Action</a>, published on August, 2010. It is being reproduced here by permission from <a href="http://www.manning.com" target="_blank">Manning Publications</a>. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ <span style="color: red;"><span style="text-decoration: underline;"><strong>Use promotional code 'java40beat' and get 40% discount on eBooks and pBooks</strong></span> </span>]</em></p>
<p><center></p>
<h2>Adding a map to an application</h2>
<p></center></p>
<h2>Introduction</h2>
<p>Adding a map to any application is very similar to adding any other view. You can either do it through Interface Builder or programmatically. The choice is up to you depending on how you like to work with UI elements. We will show you both methods.</p>
<p>The view that displays a map is called MKMapView. This class contains quite a bit of functionality including how to display the map, annotations, and user location. We will first be discussing how to add an MKMapView through Interface Builder. Once we add the map to the view, we will connect it to an IBOutlet and set its delegate class.</p>
<h2>Adding the map using Interface Builder</h2>
<p>Before you start coding the MKMapView, you must first import MapKit.framework. This will provide you will all of the libraries needed to work with maps.</p>
<p>To add a map to your view, your must first create an MKMapView IBOutlet in the class that will be using the map. Listing 1 shows how to do this.</p>
<p><span style="color: red;">Listing 1 Declaring an IBOutlet for an MKMapView</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">#import &lt;UIKit/UIKit.h&gt;
#import &lt;MapKit/MapKit.h&gt;
@interface SimpleMapViewController : UIViewController&lt;MKMapViewDelegate&gt; {
IBOutlet MKMapView * theMap;
}</pre></td></tr></table></div>

<p>@property (nonatomic, retain) IBOutlet MKMapView * theMap;<br />
The first thing we want to point out is you must import the MapKit.h header file. This is included in MapKit.framework and is needed for all MapKit interaction. The second is, our class implements the MKMapViewDelegate interface. It will allow this class to become the delegate of the MKMapView and receive messages from it. In addition to making a property, you should also synthesize the map property in the .m file.</p>
<p>Now that the IBOutlet has been declared, you need to add the MKMapView to the view in the nib file. To do this open up the nib file associated with the class that contains your IBOutlet. Now, select MKMapView from the Library and drag it on to your view. Figure 1 shows what this object should look like.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-1.jpg"><img class="aligncenter size-medium wp-image-2369" title="iphone-ipad-1" src="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-1-255x300.jpg" alt="" width="255" height="300" /></a></p>
<p><center><span style="color: red;">Figure 1 Adding an MKMapView to your view</span></center>When you drag the map on to you view, you are able to move and resize it however you like. Now that it has been added, you must connect it to the IBOutlet. To do this, click on the File&#8217;s Owner object and open the Connections Inspector. Now, drag from the map outlet to the map view to make the connection.</p>
<p>The last thing that needs to be done is set the delegate of the MKMapView. To do this, right click on the map and drag to the File&#8217;s Owner object. It should pop up a bubble that reads, &#8220;delegate&#8221;. Click the word delegate to make the connection. Figure 2 shows what the Connection Inspector should look like after you have completed all of these steps.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-2.jpg"><img class="aligncenter size-full wp-image-2370" title="iphone-ipad-2" src="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-2.jpg" alt="" width="298" height="197" /></a></p>
<p><center><span style="color: red;">Figure 2 Connection inspector for the MKMapView connections</span></center>As you can see, adding an MKMapView using Interface Building is about the same as adding UILabels, UITextFields, and other view elements. Now that you have seen how to add the map visually, we will show you how to add one programmatically.</p><div class="wpInsert wpInsertInPostAd wpInsertMiddle" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* Article-Middle-Med-Rect */
google_ad_slot = "7805667846";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<h2>Adding the map programmatically</h2>
<p>As noted before, whether you add the map using Interface Builder or with code is up to you. It is completely dependent on your preferences and organization technique. Listing 2 demonstrates how to add an MKMapView to your view controller&#8217;s view using code.</p>
<p><span style="color: red;">Listing 2 Adding an MKMapView programmatically</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">- (void)viewDidLoad {
[super viewDidLoad];
MKMapView * map = [[MKMapView alloc] initWithFrame:
CGRectMake(0, 0, 320, 480)];
Map.delegate = self;
[self.view addSubview:map];
}</pre></td></tr></table></div>

<p>This code is actually quite short. We create the map using a frame, set its delegate to our class, and add it to our class&#8217; view. As with any other view, modifying the frame passed to the map will alter its size and position.</p>
<h2>Controlling the map</h2>
<p>By default, the map has gives the user a bit of control. Without any additional code, they have the ability to scroll all over the world by flicking the map. Also, the map comes with the ability to zoom in and out by using the pinch and pull gestures.</p>
<p>To navigate the map programmatically, you must specify a region. Doing this will allow you to move the map to a specified location. You will also have the ability to set the zoom level.</p>
<p>Let&#8217;s examine the region properties and methods for navigating the map. Table 1 discusses these properties and methods and their uses.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-3.jpg"><img class="aligncenter size-medium wp-image-2371" title="iphone-ipad-3" src="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-3-300x208.jpg" alt="" width="300" height="208" /></a>The code below shows you how to create an MKCoordinateRegion and move the map to display it on the screen. The method we created here fires when a user presses a button titled Apple. It will move the map from its current location to center on Apple&#8217;s headquarters in Cupertino.</p>
<p><span style="color: red;">Listing 3 moving the map to a given location</span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="language" style="font-family:monospace;">- (IBAction) apple:(id) sender {
CLLocationCoordinate2D coords;
coords.latitude = 37.33188;
coords.longitude = -122.029497;
MKCoordinateSpan span = MKCoordinateSpanMake(0.002389, 0.005681);
MKCoordinateRegion region = MKCoordinateRegionMake(coords, span);
[theMap setRegion:region animated:YES];
}</pre></td></tr></table></div>

<p>The first thing we see here is a CLLocationCoordinate2D being created. This is simply a struct that holds two doubles for latitude and longitude. Next, we assign them to the coordinates of Apple using the WGS 84 reference frame.</p>
<p>Following the coordinates, we create a span using the MKCoordinateSpanMake method. An MKCoordinateSpan is a struct made of two doubles that represent a delta for latitude and longitude. The span represents the amount of area to view and is used when setting the zoom level. A larger number here tells the map to show a larger view area resulting in the map zooming out. Similarly, a smaller number tells the map to show less area and causes it to zoom in.</p>
<p>Once the coordinates and span have been initialized, we can create a region using the MKCoordinateRegionMake method. Finally, we set the region of the map to our newly created one and animate the transition. In addition to controlling the map&#8217;s location, you can also control how the user can interact with it. Table 2 details these properties.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-4.jpg"><img class="aligncenter size-medium wp-image-2372" title="iphone-ipad-4" src="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-4-300x120.jpg" alt="" width="300" height="120" /></a>As mentioned above, the mapType property allows you to display the map in three different ways. Most likely, you will want to make this configurable by the user, as preferences tend to vary about how the map should display. Figure 3 shows the difference between the map types.</p>
<p><a href="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-5.jpg"><img class="aligncenter size-medium wp-image-2368" title="iphone-ipad-5" src="http://www.javabeat.net/wp-content/uploads/2012/04/iphone-ipad-5-300x150.jpg" alt="" width="300" height="150" /></a></p>
<p><center><span style="color: red;">Figure 3 mapTypes: (from left to right) MKMapTypeStandard, MKMapTypeSattelite, MKMapTypeHybrid</span></center>As you can see, you have quite a bit of control over how the map looks. The standard map view looks very much like a road map that you would use for navigation. This is often the most useful of the map types.</p>
<p>The other two are quite similar. The only difference between them is the hybrid view contains the road names in addition to displaying the satellite photos.</p>
<div class="wpInsert wpInsertInPostAd wpInsertBelow" style="margin: 5px; padding: 0px;"><script type="text/javascript"><!--
google_ad_client = "ca-pub-1490953723360528";
/* JB-Footer-LU 468x15 */
google_ad_slot = "8789107210";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>]]></content:encoded>
			<wfw:commentRss>http://www.javabeat.net/2011/05/adding-a-map-to-an-application-in-iphone-and-ipad/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
