{"id":744,"date":"2009-10-26T23:24:22","date_gmt":"2009-10-26T22:24:22","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=744"},"modified":"2021-03-11T09:44:57","modified_gmt":"2021-03-11T08:44:57","slug":"db4o-queries-in-java-or-queries-without-linq","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/10\/db4o-queries-in-java-or-queries-without-linq\/","title":{"rendered":"db4o: Queries in Java or Queries Without LINQ"},"content":{"rendered":"<p>Before I continue with client-server concurrency I explain how to run queries against a db4o database without LINQ. Most of the stuff in db4o works on every platform. The API is nearly the same in Java and .NET, except the different naming-convections. The lucky .NET developers have one big advantage over Java, they have LINQ which is just awesome. The less lucky Java or C# 2.0 developers have to use other methods. So I\u2019ll give an overview over the alternative query-methods. For this post I use Java instead of C# to avoid using LINQ by accident \ud83d\ude09<\/p>\n<p><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-java-bff.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-745\" title=\"db4o-java-bff\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-java-bff.png\" alt=\"db4o-java-bff\" width=\"250\" height=\"346\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-java-bff.png 250w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-java-bff-216x300.png 216w\" sizes=\"(max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>(All posts of this series: <a href=\"?p=620\">the basics<\/a>, <a href=\"?p=637\">activation<\/a>, <a href=\"?p=654\">object-identity<\/a>, <a href=\"?p=671\">transactions<\/a>, <a href=\"?p=695\">persistent classes<\/a>, <a href=\"?p=733\">single container concurrency<\/a>, <a href=\"?p=744\">Queries in Java, C# 2.0<\/a>, <a href=\"?p=779\">client-server concurrency<\/a>, <a href=\"?p=840\">transparent persistence<\/a>, <a href=\"?p=949\">adhoc query tools<\/a>)<\/p>\n<h3>Native Queries<\/h3>\n<p>When you hear <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/java\/reference\/html\/reference\/object_lifecycle\/querying\/native_queries.html\">\u2018Native Queries\u2019<\/a> you might think that it is something like SQL. The language which is \u2018native\u2019 to the database. You\u2019re wrong. It means that it\u2019s \u2018native\u2019 to your programming language. So you write queries just as Java-Code. Native queries are in my opinion the best alternative to <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/java\/reference\/html\/reference\/object_lifecycle\/querying\/linq.html\">LINQ<\/a>. Basically you pass a inner class (poor man\u2019s closures) to the query-method. There you implement which criteria have to be fulfilled. A simple example:<\/p>\n<pre class=\"csharpcode\">List&lt;SimpleObject&gt; result = db.query(<span class=\"kwrd\">new<\/span> Predicate&lt;SimpleObject&gt;() {\r\n    @Override\r\n    <span class=\"kwrd\">public<\/span> boolean match(SimpleObject o) {\r\n        <span class=\"kwrd\">return<\/span> o.getNumber()&gt;2;\r\n    }\r\n});<\/pre>\n<pre class=\"csharpcode\">\u00a0<\/pre>\n<p>So this quite easy and natural. You just write your condition in the match-method of a predicate. A more complex example which also sorts the result:<\/p>\n<pre class=\"csharpcode\">List&lt;ComplexObject&gt; result2 = db.query(<span class=\"kwrd\">new<\/span> Predicate&lt;ComplexObject&gt;() {\r\n@Override\r\n<span class=\"kwrd\">public<\/span> boolean match(ComplexObject o) {\r\n    <span class=\"kwrd\">return<\/span> o.getVersion()==1\r\n            &amp;&amp; o.getKnowsAbout().getNumber()&lt;2\r\n            || o.getMightKnowsAlso()!=<span class=\"kwrd\">null<\/span>;\r\n}\r\n},<span class=\"kwrd\">new<\/span> QueryComparator&lt;ComplexObject&gt;() {\r\n    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">int<\/span> compare(ComplexObject complexObject, ComplexObject complexObject1) {\r\n        <span class=\"kwrd\">return<\/span> complexObject.getKnowsAbout().getName().compareTo(complexObject1.getKnowsAbout().getName());\r\n    }\r\n});\u00a0<\/pre>\n<p>Modern Java-IDEs collapse the verbose inner classes to readable little \u2018closures\u2019. Therefore it looks nicer in the real world. (Eclipse with <a href=\"http:\/\/code.google.com\/p\/lambda4jdt\/\">lambda4jdt<\/a>, <a href=\"http:\/\/www.jetbrains.com\/idea\/index.html\">IntelliJ 9<\/a>)<\/p>\n<div id=\"attachment_746\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-native.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-746\" class=\"size-medium wp-image-746\" title=\"db4o-native\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-native-300x150.png\" alt=\"native queries\" width=\"300\" height=\"150\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-native-300x150.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-native.png 950w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-746\" class=\"wp-caption-text\">native queries<\/p><\/div>\n<h3>Query By Example<\/h3>\n<p>In this <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/java\/reference\/html\/reference\/object_lifecycle\/querying\/query_by_example.html\">query-type<\/a> you give the database an object as an example. Then the database returns all objects which are similar to the given object.<\/p>\n<p>An example. We create an new object with name \u2018MyObject-3\u2019 and the number 3. Then we pass it to db4o. As result we get all object which have the same name and number.<\/p>\n<pre class=\"csharpcode\">SimpleObject toSearchFor = <span class=\"kwrd\">new<\/span> SimpleObject(<span class=\"str\">\"MyObject-3\"<\/span>,3);\r\nfinal ObjectSet&lt;Object&gt; result1 = db.queryByExample(toSearchFor);<\/pre>\n<p>It\u2019s simple but quite limiting. It\u2019s might a suitable solution when you implement some kind of search-forms.\u00a0Then you\u00a0just can fill an object with values of the search-form and off you go. But you can imagine the limits. For example you cannot get the objects which have number==0. Because query by example only matches fields which differ from the default-value. For a int 0 is the default and you\u2019re fucked. As far as I know there\u2019s no way to express such condition by example. So you have to use other query-types.<\/p>\n<pre class=\"csharpcode\">System.<span class=\"kwrd\">out<\/span>.println(<span class=\"str\">\"-cannot get the object with Number=0---\"<\/span>);\r\nSimpleObject doesnotWork = <span class=\"kwrd\">new<\/span> SimpleObject(0);\r\nfinal ObjectSet&lt;Object&gt; result2 = db.queryByExample(doesnotWork);<\/pre>\n<pre class=\"csharpcode\">\r\n\r\n<div id=\"attachment_747\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-by-example.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-747\" class=\"size-medium wp-image-747\" title=\"db4o-by-example\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-by-example-300x135.png\" alt=\"query be example\" width=\"300\" height=\"135\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-by-example-300x135.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-by-example.png 700w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-747\" class=\"wp-caption-text\">query be example<\/p><\/div><\/pre>\n<h3>SODA-Queries<\/h3>\n<p>Now <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/java\/reference\/html\/reference\/object_lifecycle\/querying\/soda_query.html\">SODA-Queries<\/a> is the low level query API for db4o. Normally you don\u2019t touch SODA unless you have some special scenarios. For example when you need to build queries very dynamically. Because of that I just mention it here but don\u2019t give any examples (Maybe another time).<\/p>\n<p>All other query-types, LINQ, Native-Queries and Query By Example are actually translated into SODA-queries. To achieve this db4o uses byte-code analysis and generates then SODA-queries. Simple queries are translated into SODA an run directly on the database. However more complex queries may cannot be optimized. Then db4o actually instantiates the objects and runs the query on the instances. For more details see here: <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/object_lifecycle\/querying\/linq\/optimization.html\">LINQ-Optimization<\/a>, <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/tuning\/native_query_optimization.html\">Native-Query-Optimization<\/a><\/p>\n<div id=\"attachment_748\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-soda.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-748\" class=\"size-medium wp-image-748\" title=\"db4o-soda\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-soda-300x141.png\" alt=\"soda queries\" width=\"300\" height=\"141\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-soda-300x141.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/db4o-soda.png 900w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-748\" class=\"wp-caption-text\">soda queries<\/p><\/div>\n<p><a title=\"http:\/\/code.google.com\/p\/lambda4jdt\/\" href=\"http:\/\/code.google.com\/p\/lambda4jdt\/\"><\/a><\/p>\n<h3>Conclusion<\/h3>\n<p>So, db4o supports a variety of query-types. In my opinion LINQ is the best one, but only available on then most recent .NET framework. Native queries is the second best choice. For special cases query be example might be an alternative. In the end everything is crunched down to SODA-Queries, which is the low level query API. Next time I cover the client-server-concurrency as promised =)<\/p>\n<p>Java-Code: <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/ComplexObject.java\">ComplexObject.java<\/a>, <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/Main.java\">Main.java<\/a>, <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/SimpleObject.java\">SimpleObject.java<\/a>, <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2009\/10\/Preconditions.java\">Preconditions.java<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before I continue with client-server concurrency I explain how to run queries against a db4o database without LINQ. Most of the stuff in db4o works on every platform. The API&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[150],"tags":[100,295],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/744"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=744"}],"version-history":[{"count":13,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/744\/revisions"}],"predecessor-version":[{"id":3829,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/744\/revisions\/3829"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}