Today I used a great analytic function : LAG. With LAG, the value of the the previous row in a query can be evaluated. In combination with CASE (or the good old DECODE) you can then make break columns in a view. Instead of writing procedural code in pl/sql or java
(define cursor, loop, compare old and new value, etc ..), you just have to add 1 line of code in the SQL-statement.
create table x (i number, j number)
/
insert into x (i,j) values (1,1)
/
insert into x (i,j) values (1,2)
/
insert into x (i,j) values (1,3)
/
insert into x (i,j) values (2,1)
/
insert into x (i,j) values (2,2)
/
select
i
, case i when lag(i) over (order by i, j) then null else i end i_breack
, j
from x
order by i, j
/
I I_BREACK J
---------- ---------- ----------
1 1 1
1 2
1 3
2 2 1
2 2
Posted June 29th, 2006 by Fred Fokkinga | 2 Comments »
Due to my interests in the ongoing development in the field of WS-technology, I decided to start looking more closely at how I can develop and expose an EJB3.0 session bean using Oracle’s JDeveloper 10.1.3.0.0 as my IDE and Oracle’s OC4J 10.1.3.0.0 as my run-time platform.
I started to make a simple entity bean based on a simple Customer table:
package nl.iteye.ejb3.ws.entity;
import..
@Entity
@NamedQuery(name="findAllCustomer", query="select object(o) from Customer o")
@Table(name="CUSTOMER")
public class Customer implements Serializable {
private Long bankaccount;
private String country;
private String firstname;
private Long id;
private String lastname;
...
}
By using the easy-to-use wizard in JDeveloper it is quite easy to build a standard session facade EJB for the
Customer entity bean:
package nl.iteye.ejb3.ws.session;
import..
import oracle.webservices.annotations.Deployment;
@WebService
@Deployment(contextPath="customerService")
@Stateless(name="CustomerFacade")
public class CustomerFacadeBean implements CustomerFacade {
@Resource
private EntityManager em;
public CustomerFacadeBean() {
}
public Object mergeEntity(Object entity) {
return em.merge(entity);
}
public Object persistEntity(Object entity) {
em.persist(entity);
return entity;
}
public Object refreshEntity(Object entity) {
em.refresh(entity);
return entity;
}
public void removeEntity(Object entity) {
em.remove(em.merge(entity));
}
/** <code>select object(o) from Customer o</code> */
@WebMethod(operationName="findAll")
public List<Customer> findAllCustomer() {
return em.createNamedQuery("findAllCustomer").getResultList();
}
}
Thanks to the wizard options, JDeveloper generates a lot of nice facade methods for free. Thank you Oracle
When you look closer to the code, you will probably notice the the special webservice annotations. These annotations are provided by the new Java XML API for invoking webservices: JAX-WS 2.0 API. With the @WebService method I tell the EJB container that it has to expose this bean as a webservice. The EJB container will deal with all the configuration stuff, which you needed to do by yourself in older days in the webservices.xml file. With the @WebMethod annotation, I specify the methods that will form the operations of the webservice. In this case I give the operation the name findAll. Special attention is going to the @Deployment annotation this is an Oracle specific annotation with which you can control the Webservice context root. When this annotation is omitted the context root will be defined by the name attribute of the @Stateless annotation and when this attribute is not set the context root will be defined by the class name of the bean. Hmmm…when you look at the documentation of the EJB 3.0 API, you will discover that you can also do this with specific attributes of the @Webservice annotations. So why does Oracle have its own annotations for defining the endpoint? I also asked Oracle this question and they gave me this answer:
For the context root (endpoint) issue in 10.1.3.0, you should use the proprietary annotations Deployement (in package oracle.webservices.annotations.Deployment.
This is due to the fact that the specifications at the time we implemented was not clear about this specific usage. So using this annotation you will be able to control the context.
However, I will take a closer look to the specs and feedback to development to be sure we handle everything properly.
The answer is acceptable in my opinion, because the EJB specs changed a lot during the process to reach it final release state.
After deploying my simple EJB3.0 webservice in my stand-alone OC4J 10.1.3 container, I can test my webservice by using the following url:
http://localhost:8888/customerService/CustomerFacade
New to Oracle OC4J 10.1.3 is that is also generates a bunch of Javascript functions for invoking the operations of the webservice in both synchronous and asynchronous way. For the asynchronous
method you have to provide a callback function that accepts the result of the webservice operation…Hey this sounds like AJAX, isn’t it? I think its a nice feature that you get for free here..Thank you again Oracle!
For now this is enough for this posting I think..I’m going to have some sleep now
. I want to use my next posting to show you how I use Apache Axis2 to invoke the findAll operation..so stay tuned
Posted June 28th, 2006 by Tom Hofte | 12 Comments »
I was doing some tests with Hibernate3 and Spring 2 integration and stumbled upon trouble with Hibernate proxies (cglib proxies).
Since Hibernate 3 nearly everything is loaded lazyly unless specified otherwise. So think about all collections and all associations a class might have. In fact this is a good thing, but it may lead to some weird behavior sometimes.
If you know that these things happen and you need such collections/associations you can usually take care of this by initializing such objects, either by Hibernate.initialize(..) or by navigating the relations, and doing this before the hibernate session is closed solves everything fine.
In Spring you can use the HibernateTemplate class for integration, to make things more easy. Assuming you have this hierarchy:
- Controller (web)
- Service
- Dao
So your controller requests the service, the service requests dao and the dao does its thing with in this case Hibernate. Assume you have a service: getPersonById(long key), which gets a person by its primary key. The real hibernate call would be session.load(clazz,serializable).
If a Person cannot be found, normally an exception is raised by hibernate – the spring framework wraps it in a spring runtime exception – this one gets to your frontend (e.g. webapplication) and you have an exception resolver, which handles it just fine. That’s how it works for the data access layer, exceptions are wrapped in a spring runtime exception. But …
For this particular example, session.load(..), it ain’t gonna work with the default spring implementation. In fact an Object is returned, and guess what: An unitialized proxy! Yes, cglib wraps it in a proxy and its initialized lazyly. That is, it is never initialized until you really use the object. The Spring implementation just loads the object, but never touches it.
If you will touch this object in your frontend now, your hibernate session is closed already (assuming you don’t use open session in view), you will get lazyinitialized exceptions – hibernate exceptions. Exceptions you never want to see in your front-end normally. So you might think: Well let’s initialize it in my Dao method (when I receive the Object), yes you could do that. However this results in the hibernate ObjectNotFoundException, and its never wrapped in a Spring DataAccessException, still not what you want.
There are 2 solutions to this:
- Override the load method of HibernateTemplate
- Override execute(..) of HibernateTemplate
The first solution solves this particular problem, the second solves all proxied objects problems (not the children’s collections / associations). The second is more risky as you might load too much and too often trees of objects that you never use.
So I’d go for the first one to play it safe. By doing it this way, the proxy is ‘unmasked’ and its exception thrown, nicely wrapped in a Spring runtime exception.
public Object load(final Class entityClass, final Serializable id, final LockMode lockMode) throws DataAccessException {
return execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Object retValue;
if (lockMode != null) {
retValue = session.load(entityClass, id, lockMode);
}
else {
retValue = session.load(entityClass, id);
}
//
// Only check if indeed not null.
// This will cause exception if proxy, but object was not found
//
if(retValue != null) {
Hibernate.initialize(retValue);
}
return retValue;
}
},true);
}
Posted June 24th, 2006 by Martijn Reuvers | 3 Comments »
Both Wilfred at OraTransplant and Lucas Jellema at Amis are writing about the ADF Faces presentations at ODTUG. Wilfred has included some movies in his post Demo of AJAX enabled ADF Faces, which demo, among others, the Rich Table Component in ADF Faces. He also has some other movies made during Duncan Mills’ presentation. The Rich Table component is pretty impressive, it dynamically loads the data when the users drags the scrollbar, and you can reorder, resize and sort the columns.
Lucas writes in his post ADF Faces Rich Client Components – Marrying JSF and AJAX together that these Ajax components will also be opensourced by Oracle, just as they opensourced the current ADF Faces components, which is good news.
Posted June 22nd, 2006 by Andrej Koelewijn | No Comments »
Wild west and Chaos. That’s basically where we are today regarding web user interfaces. For a couple of years it was pretty clear how to implements a web application. JSP was the default choice. Today that’s not the case, as we all have to do AJAX. Every day a new framework or toolkit is released. ADF Faces, JSF, Adobe Flex, Google GWT, JBoss Seam, Echo 2, etc, etc, etc.
Today i saw two article that more or less propose to dump Html and use java. Frank thinks we should be using Webstart: Do we need an Ajax client runtime: What about Webstart for Ajax?. Ted Stockwell wants the Google Web Toolkit to be implemented as an applet: The Google Web Applet?.
Do you know that there is a mature environment called Oracle Forms? Your application will run as an Applet and you’ll be able to use very productive tools like Form Developer (4gl, drag and drop) or Oracle Designer (case tool, completely generate your app). Seems like the framework a lot of people are looking for.
The only problem i see is that it’s not free, and it proprietary, and you have to use an oracle database. So, actually 3 problems. Which can all be solved by open sourcing Forms. oracle, will you please open source Forms?
Of course i’m just kidding here, but sometimes it’s just weird to see what’s happening. How we’re returning to the client-server model, and only the deployment model has actually changed.
Posted June 16th, 2006 by Andrej Koelewijn | 4 Comments »
Today, the NL-JUG J-Spring congress was held. During registration, I had subscribed myself to several sessions. In this posting I will out-line my impressions of this J-Spring edition. I will not summarize all the sessions that I have attended, but will only address some specific issues that caught my attention.
Adobe Flex
In one presentation Adobe Flex was positioned as an alternative to JSF/AJAX in an enthusiastic manner by the speaker. The speaker started the session, in front of a Java audiance, with an enumeration of all the disadvantages of developing web-based applications with Java frameworks like JSF, Spring or AJAX. Some reasons he gave where:
-
State preserving on the server is ineffecient and not preferable.
-
“There are so many different frameworks to build web applications with. There must be something terribly wrong about this..”
Despite the first mentioned reason, I could not find any argument myself for the second mentioned reason and the other reasons that where given. Unfortunately, the speaker also did not give them..But I really got frowning eyebrows (along with some others in the audiance) when the speaker said that he thinks AJAX is only nice when building fantasy applications ike Google Maps..
After his pleading against Java frameworks, the speaker started with his quest to convince the audiance about the usefulness and usability of Adobe Flex. My opinion about Adobe Flex is that you can build nice applications with it. but it can never compete with frameworks like ADF Faces, especially when used in combination with AJAX. First, Adobe Flex is not free distributed as a full-featured version, so you (your customer) has to pay for it when you want to use every feature. There is a free version but it has limited functionality. Secondly, Adobe Flex applications has to be downloaded first and then runs like an applet in your browser, which means google will not index the page containing the application and bookmarking is also not possible on a specific application screen. Third, I wonder if an Adobe Flex application is backward-compatible with older versions of the FLASH-plugin than the one on which it is build. Note that Adobe Flex application needs a Flash plug-in to run in a browser, which also creates a dependency on the client configuration. Finally, I wonder how accessible a Adobe Flex application is for people with a less face capacity than I have or which are blind. Remember that well designed web-pages, that conform to the XHTML standard, will be accessible to those group of people, due to tools that can translate the XHTML content into speech.
It may be clear now that I’m not convinced about the usefulness of Adobe Flex as a tool to make web applications.
Frameworks and meta-frameworks.
The Key Note of this J-Spring was given to Duncan Mills, Oracle’s JEE evagalist. It was the first time ever that I attended a session of him, and I have to conclude now that he is a nice speaker with humor in his talks. In his key-note Duncan explains what a framework has to contain to become a real framework. After that he explained that he talked about meta-frameworks, or a framework of frameworks. He explained that the Oracle ADF framework is a meta-framework. To show that he gave an example in which he used ADF to use services from both an EJB 3.0 session bean and BC4J component, to show and browse data on a single JSF page.
Spring 2.0
Interface21 had a session about the new features in Spring 2.0. As an young-user of Spring, I was curious about those new features, so I attended this session also.
After the session I think Interface21 has some really nice things added to Spring. Here are some:
-
Full support for AspectJ. Spring AOP is not a fully-featured AOP implememtation, for example it lacks the definition of point-cuts on fieldnames.
-
Simplified XML. Spring contains some out-of-the box tags and you are able to define your own tags. The XML for
using aspects is also less verbose.
-
A jpaTemplate class that fully supports the new Java Persistence API (EJB3)
-
Several annotations to define for example point-cuts and aspects in directly in your beans
-
XML schema validation
A nice thing to hear is that Spring 2.0 fully supports Java 5.0, but still compatible with Java 1.3.
These where my impressions of this year’s J-Spring edition..
Posted June 15th, 2006 by Tom Hofte | 5 Comments »
In ADF (in combination with ADF Business Components) you can use setCurrentRowWithKey to specify which record should be active. This is often used when selecting one record from a table of results, after which you want to display a page with the details of the selected record.
If you do this, but instead of seeing the selected record, you get the first record in the view, and you also get a JBO-25020 error, it might help if you read section 7.9.3, What You May Need to Know About Enabling View Object Key Management for Read-Only View Objects, of the Oracle ADF Developer’s Guide for Forms/4GL Developers.
By default setCurrentRowWithKey doesn’t work for read only View objects. The solution is simple, just call setManageRowByKey(true) in the create method of the view object.
Posted June 14th, 2006 by Andrej Koelewijn | 4 Comments »
Today we organized a programming contest at the office of IT-eye. Some months ago i delivered a guest lecture for the Hogeschool van Amsterdam (HvA). During a full day the students learned about JDeveloper, EJB3, and ADF Faces. Today these students competed in a programming contest. They were joined by other students of the HvA which used Visual Studio, vb.net, and SQL-server.
The goal of the contest was to create an application to handle the administration of a general practitioner, ie. register patients, appointments, etc. It was a long and hot day. We are currently experiencing temperatures over 30 degrees celcius. Despite this, the students were working hard, and all managed to implement quite some functionality. In the end, a student using JDeveloper with ADF won the contest, which is quite an accomplishment considering he had used JDeveloper for only one day.
So where’s the difference between .Net and ADF? The .net guys didn’t use an ORM framework, they were hand programming most of the SQL and converting the data to hand programmed domain model objects. The students using JDeveloper were able to generate most of the EJB model. ADF also allows you to quickly add validations to entity bean properties, just by declaring them. Another difference i noticed was that the .Net people had to hand write sql code to be able to create a pageable data table, and again, this is something that is automatically handled by ADF Faces.
Anyway, a fun day, which showed everyone that both .net and java have become quite productive, even with quite novice users.
Posted June 13th, 2006 by Andrej Koelewijn | 6 Comments »
InfoQ reports that JBoss Seam 1.0 was released today: JBoss SEAM 1.0: rethinking web application architecture. The article lists some of the key features of Seam. One that surprised me was the way Seam makes EJB session beans available as Javascript objects in the browser. Web clients can also directly send and receive JMS messages through Javascript. Seems like a productive way to support AJAX.
Does make you think though. For some time oo people have been saying that business rules need to be implemented in the business layer, whereas database oriented people have insisted on implementing most of the business rules in the database. Seems to me that with Seam the EJBs are more tightly coupled to the user interface, and less reusable. To implement generic business rules you’d have to add another layer of user interface independent EJB’s. But maybe it’s better to just put the business rules near the data?
Posted June 13th, 2006 by Andrej Koelewijn | 1 Comment »
As announced during JavaPolis last december, Oracle donated their JSF implementation called ADF Faces to Apache. The ADF Faces source code has been available for sometime for download on the apache website. Today Jonas Jacobi announced the new name for these JSF components: Trinidad. More info on the MyFaces website.
Posted June 12th, 2006 by Andrej Koelewijn | 3 Comments »