Skip to main content

Posts

Hibernate Reverse Engineering

Hibernate Revers enginering is the process of creating hibernate mapping files and pojo classes from an exising database tables.It has been explicitly achived by using HIbernate Tools. In Eclipse IDE we have to put the hibernate tool in our eclipse version. In net beans 6.8 it is by defaultly avialable to you. Fid below the handling of the required hibernate tool on different IDE's. Reveres Engineering using Net Beans Ide

Managing Data with the ThreadLocal Class

ThreadLocal class allows you to put local data on a thread, so that every module running in the thread can access it. ThreadLocal has been around since JDK 1.2, but hasn't been used much, maybe because of a first, rather poor implementation, performance-wise. Documentation From ThreadLocal Api This class provides thread-local variables. These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID). An example may help clarify this concept. A servlet is executed in a thread, but since many users may use the same servlet at the same time, many threads will be running the same servlet code concurrently. If the servlet uses a ThreadLocal object, it can hold data local to each thread. The user ID is a good example of what c...

HIbernate Performance Tunning

I highly recommend reading them. The first post is directly about tips and tricks when working with Hibernate; and knowing what to do to optimize your queries. The author recommends that unit testing and metrics using System.currentTimeMillis() can help you get the information you need to start the optimization process right. Hibernate 3 also ships with a complete statistics and metrics API that allows you to figure out *everything* that is happening under the covers. All you have to do is a.) enable staticstics for the session factory and b.) retrieve the statistics and use them. Getting them an enabling them is easy: SessionFactory sessionFactory = getSessionFactoryForApplication(); Statistics stats = sessionFactory.getStatistics(); stats.setStatisticsEnabled(true); The tricky part (or at least the part that requires the most attention) is figuring out what statistics are available, and what they really mean. There are a billion methods available on the top level, but here is a glimp...

Struts 2 and springs Integration

In this simple hello world example you will see how to integrate Spring and Struts 2 using the struts2-spring-plugin. By doing this you can utilize the Spring's powerful Dependency Injection feature. To learn more about Dependency Injection refer this example. Step1: First add the org.springframework.web.context.ContextLoaderListener to the web.xml file. <?xml version="1.0" encoding="UTF-8"?> <web-app xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2Example14</display-name> <!-- Context Configuration locations for Spring XML files --> <context-param> <param-name> contextConfigLocation </param-name> ...

Hibernate Interview Questions

Q)What are the different Transaction Factories available with Hibernate? A) There are three types of Tx Factory available in hibernate JDBCTransactionFactory, JTATransactionFactory and CMTTransactionFactory. Q) Which one is the default transaction factory in Hibernate 3.2? A)JDBCTransactionFactory is the default local transaction factory withHibernate 3.2. Q)Can Hibernate Session Factory be bound to JNDI? A) Yes, by configuring in hibernate.cfg file, session factory can be bound to initial context (as defined by properties hibernate.jndi.url and hibernate.jndi.class). Example: You have to expose this property hibernate.session_factory_name= java:/hibernate/MySessionFactory in hibaernate.cfg file. hibernate.jndi.class = com.sun.jndi.fscontext.RefSContextFactory hibernate.jndi.url = file:/auction/jndi In side HibernateUtil class SessionFactory sf = (SessionFactory) new IntialContext().lookup("/hibernate/MySessionFactory") Q) Can Hibernate be used to call stored procedur...

Spring’s AOP with Struts

Using Spring’s AOP with your Struts Application: To tackle cross-cutting you can use Spring interceptors To use Spring’s AOP in your Struts Applications you need to : 1. Create the interceptor. 2. Registor the interceptor. 3. Delclare interceptors. For example A sample logging interceptor package net.champ.example1.employee.interceptors; import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class LoggingInterceptor implements MethodBeforeAdvice { public void beforeMethod(Method method, Object[] objects, Object o) throws Throwable { System.out.println("logging before intersection"); } } This interceptor will execute the beforeMethod() before every intersection (We are using MethodBeforeAdvice). Here we are just printing a line but we can use it for anything we like. Next let us register the interceptor. Registering the interceptor in the Spring config file <?xml version="1.0" encoding="UTF-8...

Struts and Springs Integration

Integration using ActionSupport Integrating Struts with Spring doesn’t sound to be easy but if you follow this method you will find it quite simple and to add to this Spring provides you with org.springframework.web.struts.ActionSupport class. This class provides a method getWebApplicationContext() which you can use to obtain the Spring context. So you just have to extend Spring’s ActionSupport from your strut’s actions. Using ActionSupport to integrate Struts package net.champ.example1.actions; import java.io.IOException; import javax.servlet.*; import org.apache.struts.action.*; import org.springframework.context.ApplicationContext; import org.springframework.web.struts.ActionSupport; import net.champ.example1.beans.Employee; import net.champ.example1.business.EmpSearchService; public class SubmitSearch extends ActionSupport { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOExcep...