Showing posts with label J2EE. Show all posts
Showing posts with label J2EE. Show all posts

05 June 2011

Project "Dali" a very exiting JPA plugin for Eclipse

Salam,

http://www.eclipse.org/webtools/dali/

a very existing tool exist in Eclipse ( I think >= 3.5)


to open it, Window>Show Views> JPA Details

02 June 2010

A note about Cascade.MERGE

Al salamo Alykom,

Hi folks, I've a long time since I've been posted about Java EE.
So, today we gonna talking about JPA, we are talking about the merge operation.

From Specs:

The semantics of the merge operation applied to an entity X are as follows:
....
- If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.


This is what I need to talk with you about:

Here' an example:
Consider we have a uni-directional many-to-one relationship between Jobs and the user that do that job:


public class User{
// .. user data here...
}

public class Job{

private User user;
//....

public User getUser(){
return user;
}
}



as it appears, It is a uni-directional from Job to User (many-to-one)

suppose we have the following annotation over the getUser operation:
@ManyToOne(cascade=CascadeType.MERGE)

this means, when a managed Job have changed its User, on merging the Job Object to DB, also merge the in-relation Object (User).


Here's a code example that prove this specs element:

Job job = em.find(Job.class, 1);
User u = job.getUser();
u.setName("mohammed");
em.merge(job);


After this code, the field name of User will be updated to be "mohammed".

18 January 2010

Calling session beans on JSF Managed bean's start up

One situation may occurs is that you need to call some session bean method on some JSF page's start up.

If you try to put the code that calls the injected EJB session bean's method in your JSF request-scoped Managed bean constructor, you will get a NullPointerException as the Injection occurred after making an instance of the managed bean.

The solution is that to provide a @PostConsruct method and put your EJB calling code in.
@PostConsruct is supported in JSF1.2 and if you running your JSF Application in an Application Server (example JBoss)

So, Here's the sequence the managed bean instance is created in:

Calling manged bean constructor => Injecting Session bean (if any) => calling PostConstruct method(s) (if any)

Here's an example:

public class ShowArticleBean {

@EJB(name="SimpleWiki/ArticleManagementBean/local")
private ArticleManagement articleMgmt;

public ShowArticleBean() {
System.out.println("In Constructor : " + articleMgmt);

}

@PostConstruct
public void construct() {
System.out.println("In PostConsruct : " + articleMgmt);
}


And here's the output :

04:51:29,546 INFO [STDOUT] In Constructor : null
04:51:29,547 INFO [STDOUT] In PostConsruct : ArticleManagementBean

12 January 2010

Avoid throwing RuntimeException from you Session beans

Working with session beans usually requires you to throw some type of exceptions to the calling layer (usually the presentation layer), So beware, don't try to throw RuntimeExceptions or RemoteException because the Container will encapsulate these exceptions in EJBException and then destroys the bean instance from the bean bool.

If you need to throws Runtime/Remote Exceptions, then add @ApplicationException(rollback=true) as a class level annotation to your Exception class, and this applicable only on your own custom exceptions.

In all cases (RuntimeException or Application Exceptions) the container will rollback the transactino.

From EJB in Action :
“In EJB , an application exception is an exception that the client is expected to handle. When thrown, such exceptions are passed directly to the method invoker. By default, all checked exceptions except for
java.rmi.RemoteException are assumed to be application exceptions.
On the other hand, all exceptions that inherit from either java.rmi. RemoteExceptions or java.lang.RuntimeException are assumed to be system exceptions (as you might already know, all exceptions that inherit
from java.lang.RuntimeException are unchecked). In EJB , it is not assumed that system exceptions are expected by the client. When encountered, such exceptions are not passed to the client as is but are wrapped in a javax.ejb.EJBException instead.”

Here's an example session bean that throws IllegalArguemntException ( a well-known runtime exception):

@Stateless
@Remote(AccountManagement.class)
@Local(AccountManagement.class)
public class AccountManagementBean extends DBBaseBean implements AccountManagement {

public void registerUser(User user) throws IllegalArgumentException {

if (user == null || StringUtil.isNullSpacesOrEmpty(user.getUsername()) || user.getPassword() == null)
throw new IllegalArgumentException("Uncomplete user registeration data");

em.persist(user);
logger.info("User created :" + user.getUsername());
}
@PostConstruct
public void init() {
System.out.println("INIT");
}
@PreDestroy
public void destroy() {
System.out.println("DESTROY");
}

}

And here's a test client :

public class Main {
public static void main(String[] args) throws Exception{
AccountManagement test = InitialContext.doLookup("SimpleWiki/AccountManagementBean/remote");

try {
test.registerUser(null);
}catch (Exception ex) {
if (ex.getCause() instanceof IllegalArgumentException) {
System.out.println("IllegalArgumentException : " + ex.getCause().getMessage());
}else {
ex.printStackTrace();
}
}
}
}

And here's the output of the EJB Container :

21:33:12,504 INFO [STDOUT] INIT
21:33:28,595 INFO [STDOUT] DESTROY

As you can see from the output of the session bean, When a RuntimeException thrown, the EJB Container destroys the bean instance from the bean bool !

10 October 2009

Layerd architectures (1)

Most enterprise applications contain a large number of components. Enterprise applications are designed to solve a unique type of customer problem, but they share many common characteristics. For example see more ...

26 February 2009

Application Layers

Today I want to talk About an important Programming concept, Application Layers.

All of us know that there might be in your layered application some common layers such as Database layer, Business layer and Presentation layer, but most of us do not exactly know the responsibility of each layer, so lets talk about Layers in our Layered Applications.

In any modern J2EE ( and non-Java ) layered application we have Four layers in our applications, three in our programming code and one outside it, it represented in the Database.

These layers are ( from top to bottom ) Presentation layer, Business layer and Integration layer .

We will define each layer and give common technologies used to implemented that layer.


Presentation Layer :

presentation layer is the layer that the end-user sees and interacts with, this is mostly implemented as Web Interface or Desktop interface or even as Mobile-application interface.

Common technologies:

mostly the presentation layer implemented in JSF, Struts, Oracle ADF, Apache Coccon, JSP/Servlet for Web-based presentation layers and Swing, AWT for Desktop-based presentation layers

Business Layer :

The business Layer ( also known as Application Layer )  is the most important layer in your application, it represents the core application modules implemented, the more clearer and design patterns involved, the best your application in both flexibility and ability to be maintained on the long run.

Mostly implemented in your native Java code as Objects that represents your domain business model or in Large applications,  it is implemented as EJB Session Beans and MDBs.

Again, this layer is the most important one cause it decides whether you application design and coding is good or bad.

It depends on the developers experience in solving business problems.

Integration Layer :

Also known as Data Access Layer, It is the layer responsible for getting the data back and forth from and to the database.

It is mainly responsible for Making data access processes for you Business layer also it makes your application independent from the underlaying database technology.

Common technologies:

Also many developers implement this layer using regular JDBC code, there are many frameworks and technologies found in this area, for frameworks there are Hibernate, Oracle Toplink, iBATS and OJB that is an implementation for JDO technology.

In this layer there is a common Design patten arise in J2EE applications that make the implementation of this layer an easy and organized task , that is DAO pattern (Data Access Objects). ( see this post for more info about DAO pattern)

The last layer is the Database layer that stores the data of your applications, also there are many DBMS found as Oracle Database, MySQL, HSQL, the windows-only MS SQL Server and many more.

11 December 2008

The First Run of Red Hat Developer Studio

I have today installed the Red Hat Developer Studio (RHDS) version 1.0 beta, it based on eclipse with many and many very handy add-ins.



From the first run, it seemed very well especially with its size, it is more that 500 MB (The larger the size, the more the capabilities added to it).

In my opinion, there are many disadvantages (at least compared to NetBeans, the one I prefer) and also there are many advantages.
Well, let me tell you about what I found:
1-its application server is JBoss ( that basically made me installed it)
But when running Struts application on, I found it seems like apache tomcat (this means that the JBoss web container based on tomcat)

2-It is based on eclipse (the most IDE that I don't like, because it is not smart at all!!)
I till this moment cannot know how to start running the web application in an easy way. To run web applications I use: alt+shift+x then press R, oops !!!!!!!
In netbeans I just press F6 !!!
Besides when I change any thing in the web application, the container has to redeploy the web app, in netbeans I just press F6 after doing changes and all things get done perfectly in a small amount of time, but in RHDS I need to stop server manually and then start it and wait for it to finish and then run the web app using alt+shift+x then press R!!!!!
3-There are shining parts in this IDE, like its struts-config easy-to-use tool.
We all know that in java there is a big problem named meta-data hell, which we have a lot of .xml files we have to maintain, but this new tool makes maintaining the struts configuration file is an enjoyable task. Moreover the tool isn't just a single tool, but its actually two tools, one for managing the file (adding/removing elements ) and one for managing the entire application throw dragging and dropping actions/form-beans/jsp web pages so you can control the application from a higher view. (see the next two pictures)






4-A new Visual Editor was added to design you html/struts/jsf pages, one designer for all these.
Really it is a good designer I liked it over Oracle JDeveloper's one. That visual designer has four aspects, Visual/Source, Visual, Source and preview.
Really it is a great thing in the IDE.


5-The IDE also supporting many technologies such as Hibernate, JBoss seam, JSF, JBoss RichFaces, Spring and of course basic web development such as JSP/Servlets and Struts 1.1 and 1.2
6-In general it is great but missing some things that are inherited from Eclipse.