mvn eclipse:eclipse and m2e eclipse import – Errors in Web project – Richfaces showcase example – OutOfMemoryError

If you convert a maven project to eclipse using eclipse:eclipse, like richfaces showcase, and try to open it in eclipse, it might lead to ClassNotFound errors when you run the app in Tomcat. To fix that, delete the ‘.settings’ directory and install m2e plugin, and m2e webtools plugin (wtp). Then import the project into eclipse via maven. Then run the project in Tomcat server in Eclipse. If you run into “java.lang.OutOfMemoryError: PermGen space”, set permsize arguments for Tomcat as shown:
-XX:PermSize=64m -XX:MaxPermSize=128m

Hibernate: why do we use inverse=”true” in many to many

I was going through the hibernate tutorial, I couldn’t understand why they used inverse=”true” in the examples. The tutorial wasn’t clear enough.

Consider the case given in the example: Event and Person are two entities and there is a many to many mapping between  them. So when you add a event to a person using this code:

Event e=(Event)session.load(Event.class,eid);
Person p=(Person)session.load(Person.class,pid);
p.getEvents().add(e);
return p;//End of method

As in the above code, if you are making changes to only one end of the relationship (the Person end) then you don’t have to set inverse=”true” in the hbm file. But if you want the addtion of Events to a Person reflect on the Events entity (so that you can get persons attending an event using e.getPersons() )  with out causing hibernate to query the database when you do a event.getPersons(), you can do some thing like this

p.getEvents().add(e);//line a
e.getPersons().add(p);//line b
return p;//End of method

In this case, if you don’t set an inverse=”true”, you will get an error when hibernate tries to insert twice in the join table PERSON_EVENTS, once for line a and once for line b. Setting inverse=”true” on one side of the relationship tells hibernate to ignore changes to the relationship for this ‘many to many’ property. So if you set set inverse to true on one end of the relationship, you’ll see only one insert statement in hibernate log. If you set it on both ends, then hibernate won’t even insert a record in thte join table. If you still didn’t understand, please post your question and I’ll try to answer it.

Calling a procedure from oracle sql developer

Look for the package in the left
Double click the package declaration
Now click on the green colored ‘Run’ button. it will show you a dialog to run the proc.

what is serailVersionUID?

As per sun java doc at http://java.sun.com/j2se/1.5.0/docs/api/java/io/Serializable.html

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender’s class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

 ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class–serialVersionUID fields are not useful as inherited members.

keywords: why override serailVersionUID, serialization serailVersionUID,InvalidClassException serailVersionUID

J2EE listeners mini tutorial

You can listen to servlet container  and http session events using

  • javax.servlet.ServletContextListener
  • javax.servlet.http.HttpSessionListener

ServletContextListener :
This interface contains two methods :

  • public void contextInitialized(ServletContextEvent sce);
  • public void contextDestroyed(ServletContextEvent sce);

The HttpSessionListener interface also contains just two methods, for session creation and invalidation events respectively :

  • public void sessionCreated(HttpSessionEvent se);
  • public void sessionDestroyed(HttpSessionEvent se);

There are also these:

HttpSessionBindingListener
HttpSessionAttributeListener
HttpSessionActivationListener

ServletContextAttributeListener

(These are only available from a particular j2ee version. So check the j2ee version before using them)

How does the JNDI lookup for the ejb work when the ejb is bound to a different EJB location in sun-ejb-jar.xml?

The lookup is:
java:comp/env/OMParticipantServicesEJB
The acutal JNDI location is:
ejb.omservices.OMParticipantServicesLocalHome

This EJB lookup using JNDI with a layer of indirection

Using the ejb-local-ref tag in web.xml
<web-app>
<ejb-local-ref>
<ejb-ref-name>OMParticipantServicesEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>com.biperf.ejb.participant.OMParticipantServicesLocalHome</local-home>
<local>com.biperf.ejb.participant.OMParticipantServicesLocal</local>
<ejb-link>omservicesEJB.jar#OMParticipantServicesEJB</ejb-link>
</ejb-local-ref>
</web-app>

The actual sun-ejb-jar.xml

<ejb>
<ejb-name>OMParticipantServicesEJB</ejb-name>
<jndi-name>ejb.omservices.OMParticipantServicesLocalHome</jndi-name>
<bean-pool/>
</ejb>

So the app server is smart enough to understand the look ups for java:comp/env/OMParticipantServicesEJB as the lookup for the EJB ‘OMParticipantServicesEJB’ and is able to locate home interface. If you remove the <ejb-local-ref> entry from web.xml, the lookup will fail with NameNotFoundException

More on this here:  http://www.ibm.com/developerworks/websphere/library/bestpractices/increase_app_portability.html

This is for glassfish. Has details about mapping the JNDI name (resource-ref-name) in web.xml with actual JNDI (global):
http://forums.sun.com/thread.jspa?threadID=5411011

You can also read more about this sun-web.xml here: Sun GlassFish Enterprise Server 2.1 Application Deployment Guide

Printing ant classpath

Here is the trick to debug ant classpath.  

 <property name=”myclasspath” refid=”wsit.classpath”/>

    <echo message=”Compiling the webservice classes. The classpath is ${myclasspath}” />

More on ant debugging from:

http://www.javalobby.org/java/forums/t71033.html

http://www.eclipsezone.com/eclipse/forums/t71656.html

http://www.ibm.com/developerworks/edu/os-dw-os-ecl-easyant.html

Do I have to use c:out

If I want to print some variable in any of the 4 scopes, do I have to use c:out tag? Can’t I just use the ${} ??

Incremental deployment with netbeans on glassfish

Heard that development on glassfish is quite fast with netbeans using incremental deployment. Here are some links that will help

http://www.adam-bien.com/roller/abien/entry/how_fast_is_the_build

http://jars.de/english/improving-glassfish-deployment-performance-in-eclipse

Ant splitting entries in manifest.mf

I’m having another problem with ant splitting class-path entry in manifest file to two lines. And glassfish server is being stupid and not building the whole classpath.

Posted this on glassfish forums http://forums.java.net/jive/thread.jspa?threadID=47392