Adam Bien's Weblog   

Tuesday Jan 06, 2009

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]


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Monday Jan 05, 2009

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:

  1. Visual Designer works really good - as already known from Woodstock.
  2. Page Flow designer works.
  3. EJB 3 injection of Stateless and Stateful Session Beans into managed beans works.
  4. Deployment to Glassfish v2 works.
  5. Declarative Data Binding (Value Binding) between ICEFaces components and JavaBeans (JPA-entities) works.
  6. "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 :-).


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Sunday Jan 04, 2009

Best Of / Most Popular Entries (>10k views) In This Blog

  1. January: Domain Driven Design, EJB 3, JPA, Glassfish v2, JSF (Model View Presenter), Data Binding, Java DB ...or sports for Java Hackers [13203 views]
  2. February: Nice, but hidden, CRUD wizard in Netbeans 6.0 [17038 views]
  3. March: Eclipse 3.3 or NetBeans 6.0 - with surprising result [44679 views]
  4. April: High Level thinking about the introduction of closures in Java [12728 views]
  5. May: java.net@JavaONE: Which Programming Language J. Gosling would use now, except Java? [40156 views]
  6. June: First Indicators of Overengineering in Your Project [20027 views]
  7. July: First Week With Netbeans 6.5M1 - Some Smoke - But Interesting Directions [32204 views]
  8. 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]
  9. 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]
  10. 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]
  11. November: Eclipse RCP vs. Netbeans RCP - and what really counts [20086 views]
  12. 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
The drop of traffic in October and November was caused by my Roller / Glassfish experiments, some offline-hours and many broken links :-).


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Saturday Jan 03, 2009

Netbeans 6.5 Quicktip: Changing The Archive (EJB-JAR) Name, Deploying RARs

  1. Go to: [PROJECT_NAME]/nbproject/project.properties (using CTRL+2)
  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.
Tip: if you change the ending from .jar to .rar you can misuse an EJB project to develop, debug and redeploy JCA connectors to Glassfish v2 - its really convenient.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Friday Jan 02, 2009

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:

  1. How To Be A Java Guru Without Knowing Java.
  2. The VooDoo Consulting Style.
  3. Motivation over Experience
  4. A Perfect Developer Should
  5. ...but a Perfect Developer Needs
  6. Your Project Starts To Be Sub-Optimal If:
  7. The Ultimate Inteview Question


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Tuesday Dec 30, 2008

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)


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Monday Dec 29, 2008

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... :-)


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Sunday Dec 28, 2008

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.:

import javax.ejb.EJB;
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.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Saturday Dec 27, 2008

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.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

Friday Dec 26, 2008

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.


[This entry is based on / extends my books: Enterprise Architekturen, Leitfaden fuer effiziente Software-Entwicklung and: Java EE 5 Architekturen, Patterns und Idiome]

 Rss

About

About me: www.adam-bien.com
JavaONE 2008 Interview
My Recent Books
Adverts
Search
Links
greenfire.dev.java.net
...the last 150 posts
...the last 10 comments
my.netbeans.org
Visitors
License