Adam Bien's Weblog
Mapping JPA Entities To SQL Views - It Works Even With Derby
SQL Views can be considered either as best practice or an anti-pattern. It depends on the perspective. Sometimes, however, it is required to provide an efficient way to iterate over an excerpt or even a set of related entities, but return a different “view” to the client. This can be achieved by fetching the entities with EntityManager and merging them together inside a Session Bean (a Service). This approach is neither fast, nor easy to maintain. Especially the merging and extraction of entity data is error-prone and can become quite complex. Another possibility is the execution of more complex native SQL-statements and mapping them into existing entities or TOs. The query could become complex and the filter criteria highly repetitive. You will need the filter in all related queries which return the particular subset. Especially in the context of pagination, where the data is mostly retrieved for read-only purposes, database views are the easier and more efficient alternative. Instead of implementing a lot of plumbing on the “Java-side” all the work could be easily done in the database. You have just to create a SQL View – it is a part of the SQL standard and is even supported by the Derby DB shipped with Java:
CREATE VIEW APP.V_CUSTOMER(NAME,CITY) AS SELECT NAME,CITY FROM APP.CUSTOMER
For SQL queries there is no difference between views and tables, so you can easily map a JPA entity to a view transparently. The code on the Java side remains clean and simple – and you will even get better performance. There is a drawback: not all views are updatable. Whether a view is updatable or not highly depends on the complexity and particular database. E.g. in Derby DB all views are not updatable.
[It is an excerpt from my current book "Productive Java EE - Rethinking Best Practices]
Posted at 11:08AM Jan 06, 2009 by Adam Bien in Productive Java EE - Rethinking Best Practices | Kommentare[2]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Netbeans 6.5 With Visual Ice Faces - Installation, Test, EJB 3 Integration, An Error And Solution
The installation of icefaces is very smooth. You don't even have to restart Netbeans 6.5 (and this without OSGI :-)) . It takes few minutes / seconds - it depends on your bandwidth (9MB). IceFaces installation enhances the web-project type, so you will get two additional choices in the "Frameworks" tab: ICEFaces and Visual Web ICEFaces. The latter choice let you design your page visually with drag and drop vrom pallette etc. - very much like woodstock. The first choice is for source editor only. Don't select both frameworks - it will not work...
First impressions:
- Visual Designer works really good - as already known from Woodstock.
- Page Flow designer works.
- EJB 3 injection of Stateless and Stateful Session Beans into managed beans works.
- Deployment to Glassfish v2 works.
- Declarative Data Binding (Value Binding) between ICEFaces components and JavaBeans (JPA-entities) works.
- "Start Page" support doesn not work. It generates in the web.xml something like this:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>faces/Page1.jsp</welcome-file>
</welcome-file-list>
This will cause an error in case you will try to "Run" the application.
java.lang.NullPointerException
at com.sun.faces.renderkit.RenderKitImpl.createResponseWriter(RenderKitImpl.java:169)
at com.icesoft.faces.renderkit.D2DRenderKit.createResponseWriter(D2DRenderKit.java:82)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:168)
at com.icesoft.faces.application.D2DViewHandler.renderView(D2DViewHandler.java:151)
at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:283)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245)
Solution: Just delete the section <welcome-file-list> and change the Page1.jsp into Page1.iface. The URL should looks like: http://localhost:8080/[WAR_NAME]/Page1.iface. If you want to "Run" your application, put the Page1.iface (or your actual welcome page) into the Relative URL box in the Properties (Run) of your project.
I lost about one hour for finding a solution for the problem above, so my overall impression is just good and not very good :-).
Posted at 08:35AM Jan 05, 2009 by Adam Bien in Netbeans | Kommentare[2]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Best Of / Most Popular Entries (>10k views) In This Blog
- January: Domain Driven Design, EJB 3, JPA, Glassfish v2, JSF (Model View Presenter), Data Binding, Java DB ...or sports for Java Hackers [13203 views]
- February: Nice, but hidden, CRUD wizard in Netbeans 6.0 [17038 views]
- March: Eclipse 3.3 or NetBeans 6.0 - with surprising result [44679 views]
- April: High Level thinking about the introduction of closures in Java [12728 views]
- May: java.net@JavaONE: Which Programming Language J. Gosling would use now, except Java? [40156 views]
- June: First Indicators of Overengineering in Your Project [20027 views]
- July: First Week With Netbeans 6.5M1 - Some Smoke - But Interesting Directions [32204 views]
- August: First Week With Netbeans 6.5M1 - Some Smoke - But Interesting Directions [13744 views] and Useful Explanation: "iBATIS, Hibernate, and JPA: Which is right for you?", Strange Conclusion [12725 views]
- September: New UMLKit Available For Netbeans 6.5 - First Tests - Reverse Engineering Of EJB 3 Rocks [17808 views] and EJB 3.X ...the Consultants Killer [15303 views]
- October: Productive Java EE 6 - Rethinking Best Practices, Planned For San Jose, Takes Place in Krakow (JDD) ...and Netbeans Days, or Poland the "Java Land"? [4342 views]
- November: Eclipse RCP vs. Netbeans RCP - and what really counts [20086 views]
- December: Top Eight, Non Java, Netbeans 6.5 Features [31852 views]
So the winner seems to be: Eclipse 3.3 or NetBeans 6.0 - with surprising result [44679 views]
The views above are monthly results - not overall views. The actual number should be much higher. RSS-Feed results are even not included in this statistic. What suprised me is the high number of visits a day. The daily average ranged from 3k - 4.5k. Thanks to all readers / subscriber and especially for constructive comments! Feedback is highly appreciated.
Some overall stats:
| Summary by Month | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Month | Daily Avg | Monthly Totals | ||||||||
| Hits | Files | Pages | Visits | Hosts | KBytes | Visits | Pages | Files | Hits | |
| Dec 2008 | 19955 | 17350 | 11225 | 4570 | 32264 | 15695635 | 141681 | 347996 | 537880 | 618631 |
| Nov 2008 | 15402 | 12319 | 9027 | 3349 | 19468 | 13240661 | 100479 | 270832 | 369593 | 462074 |
| Oct 2008 | 14753 | 11585 | 9620 | 3287 | 16059 | 14005297 | 101909 | 298234 | 359157 | 457344 |
| Sep 2008 | 17825 | 14265 | 10173 | 3573 | 19249 | 15068533 | 107211 | 305198 | 427963 | 534777 |
| Aug 2008 | 17280 | 13630 | 9521 | 3590 | 20059 | 14621088 | 111317 | 295165 | 422560 | 535694 |
| Jul 2008 | 19936 | 15732 | 10770 | 3906 | 23417 | 15021836 | 121093 | 333888 | 487707 | 618040 |
| Jun 2008 | 19545 | 15536 | 11399 | 3838 | 22382 | 13927917 | 115157 | 341970 | 466104 | 586360 |
| May 2008 | 20966 | 16934 | 11208 | 4154 | 26671 | 16597906 | 128804 | 347458 | 524962 | 649954 |
| Apr 2008 | 21220 | 16665 | 10834 | 4130 | 27886 | 19052403 | 123913 | 325020 | 499973 | 636600 |
| Mar 2008 | 18901 | 13811 | 10210 | 3385 | 24515 | 13900018 | 104956 | 316533 | 428155 | 585932 |
| Feb 2008 | 17751 | 12187 | 10756 | 3240 | 22073 | 12407522 | 93975 | 311945 | 353444 | 514799 |
| Totals | 164067617 | 1254834 | 3506066 | 4891411 | 6217440 | |||||
Posted at 09:54AM Jan 04, 2009 by Adam Bien in General | Kommentare[4]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Netbeans 6.5 Quicktip: Changing The Archive (EJB-JAR) Name, Deploying RARs
- Go to: [PROJECT_NAME]/nbproject/project.properties (using CTRL+2)
- The property: jar.name specifies the archive name which is going to be deployed to the server and copied into: [PROJECT_NAME]/dist as well.
Posted at 09:32AM Jan 03, 2009 by Adam Bien in Netbeans | Kommentare[1]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Softskills and Other Semi-Serious Posts
I'm refining now the slides for my first (at probably last :-)) "softskills" session for the OOP 2009 conference in Munich. It is based on some older posts from this blog:
- How To Be A Java Guru Without Knowing Java.
- The VooDoo Consulting Style.
- Motivation over Experience
- A Perfect Developer Should
- ...but a Perfect Developer Needs
- Your Project Starts To Be Sub-Optimal If:
- The Ultimate Inteview Question
Posted at 01:12PM Jan 02, 2009 by Adam Bien in Fun | Kommentare[4]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
What Is Faster: Parsing a java.util.Properties Or Invoking Class#getAnnotation()?
I'm building an EJB 3.1 capable ServiceLocator, which works with the global JNDI names (portable and standardized version JNDI-names needed to lookup a local, remote, or even no-interface-view interface) . The implementation relies on parsing java.util.Properties to fetch the current EAR and EJB-JAR (module) names - both cannot be derived via reflection. I was concerned about the performance of parsing and accessing the properties from a file and fired up the Netbeans 6.5 profiler.
I wasn't even able to find the java.util.Properties in the hotspot list at all. ...the reason was - it is lightening fast (took only about 4.5 ms...).
The ServiceLocator 2.0 :-) accesses the annotations as well - which turned out to be the real hot spot. The first access to the annotation takes 55 ms. It is the method Class#initAnnotationsIfNecessary (indirectly invoked by Class#getAnnotation), which takes so long. It uses Sun's internal class sun.reflect.annotation.AnnotationParser, which in turn creates a dynamic proxy. The creation of the dynamic proxy (Proxy#newProxyInstance) takes 44 ms.
The exact performance was:
55 ms Class.getAnnotation
4.43 ms Creating, loading and parsing java.util.Properties (two String entries)
Posted at 11:35AM Dec 30, 2008 by Adam Bien in Java EE 5 Architectures And Idioms | Kommentare[0]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Toshiba Notebooks With OpenSolaris - The Java Workstation?
I installed during a not so exciting, sponsored keynote at JavaONE OpenSolaris in VirtualBox - it worked perfectly. Recently I saw at Devoxx Brian Leonard's session about OpenSolaris, TimeSlider, VirtualBox and testing - everything worked really well. I was especially impressed by the VirtualBox and ZFS integration. So far it looks promising - the remaining challenge is good device (w-lan, graphics device etc.) support for notebooks - I just remember the old linux days :-). It seems like Toshiba will ship some notebooks with OpenSolaris - this is interesting. What's about lenovo? It would be the next step... :-)
Posted at 04:38PM Dec 29, 2008 by Adam Bien in General | Kommentare[1]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Simplest Possible EJB 3.1
@Stateless
public class SimpleSample{
public void doSomething() { /*business logic*/ }
}
How to compile:
You will need the the EJB 3.0 / 3.1 API in the classpath, or at least the @Stateless annotation.
How to deploy:
Just JAR the class and put the JAR into e.g: [glassfishv3-prelude-b23]\glassfish\domains\domain1\autodeploy
How to use:
e.g.:
public class MyServlet extends HttpServlet{
@EJB
private SimpleSample sample;
}
And: there is no XML, strange configuration, libraries, additional frameworks or jars needed...
See also: EJB 3 series and EJB 3.1 From Legacy To Secret Weapon.
Posted at 09:21AM Dec 28, 2008 by Adam Bien in Java EE 5 Architectures And Idioms | Kommentare[12]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Netbeans 6.5 Quicktip: Unit Tests And Profiler
With Netbeans it is easy to launch your JUnit tests with the Profiler. You just have to click on the file in the "Projects" Tag and choose "Profile File" (this works only in non-EJB-projects :-(). The profiler will be launchend and executed with all unit tests in the chosen class. It is fast and convenient way to find potential hotspots in advance. It is hard to find memory leaks this way, but you can check the performance of your algorithms... The profiler looks like this.
Posted at 11:32AM Dec 27, 2008 by Adam Bien in Netbeans | Kommentare[1]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]
Netbeans 6.5 Screenshot (1.6u11 + Nimbus) With Visual Form Designer
I found another NB 6.5 screenshot with matisse and Japanese(?) characters in this post.
Posted at 09:38AM Dec 26, 2008 by Adam Bien in Netbeans | Kommentare[1]
[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]


