Monday, January 20, 2014

Using same Embedded object more than once in the same entity

If more than once we have to use the same Embedded object, then we have to use @Attributeoverrides and @AttributeOverride annotations to give override the column names of the embedded objects. Otherwise, we get a error like this : "org.Hibernate.MappingException: Repeated column in mapping entry:" for all the columns of the Embedded objects that have been repeated.

For example, if Name is a Embedded object in Student Entity, then the mapping should be like this
 @Entity  
 public class Student implements Serializable {  
      @AttributeOverrides({  
                @AttributeOverride(name = "firstName", column = @Column(name = "FATHER_FIRST_NAME")),  
                @AttributeOverride(name = "surName", column = @Column(name = "FATHER_SUR_NAME")),  
                @AttributeOverride(name = "lastName", column = @Column(name = "FATHER_LAST_NAME")),  
                @AttributeOverride(name = "displayName", column = @Column(name = "FATHER_DISPLAY_NAME")) })  
      @Embedded  
      private Name father;  
      @AttributeOverrides({  
                @AttributeOverride(name = "firstName", column = @Column(name = "MOTHER_FIRST_NAME")),  
                @AttributeOverride(name = "surName", column = @Column(name = "MOTHER_SUR_NAME")),  
                @AttributeOverride(name = "lastName", column = @Column(name = "MOTHER_LAST_NAME")),  
                @AttributeOverride(name = "displayName", column = @Column(name = "MOTHER_DISPLAY_NAME")) })  
      @Embedded  
      private Name mother;  
  ...  
 }  
And Embeddable object'r mapping being:
 @Embeddable  
 public class Name implements Serializable {  
      /**  
       * Default generated Serialization ID  
       */  
      private static final long serialVersionUID = -971064645279229579L;  
      @Column(name = "FIRST_NAME")  
      private String firstName;  
      @Column(name = "SUR_NAME")  
      private String surName;  
      @Column(name = "LAST_NAME")  
      private String lastName;  
 ...   
 }  

Configuring log4j with Spring framework


  • Make sure that the commons-logging is there in the classpath of the projet. Including spring-core will automatically include commins-logging with it. I tried for spring-core version 3.1.1.RELEASE
  • First, define the log4j.xml with the desired log4j configuration for the project. Example is given below. Place the file along with log4j.dtd in the resources/config folder of the  project.

 <?xml version="1.0" encoding="UTF-8" ?>  
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
 <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>  
      <!-- console logger -->  
      <appender name="stdout" class="org.apache.log4j.ConsoleAppender">  
           <layout class="org.apache.log4j.PatternLayout">  
                <param name="ConversionPattern"  
                     value="%-5p [%d{EEE MMM d HH:mm:ss z yyyy:}] [%t] [%X{ProcessId}] [%L] %c : %m%n" />  
           </layout>  
           <filter class="org.apache.log4j.varia.LevelRangeFilter">  
                <param name="LevelMin" value="DEBUG" />  
           </filter>  
      </appender>  
     <!-- Specify the path for the logFile -->  
      <appender name="FILE" class="org.apache.log4j.RollingFileAppender">  
           <param name="File" value="<path of the log file>" />  
           <param name="Threshold" value="DEBUG" />  
           <param name="MaxFileSize" value="10MB" />  
           <param name="MaxBackupIndex" value="10" />  
           <layout class="org.apache.log4j.PatternLayout">  
                <param name="ConversionPattern" value="%d{ISO8601}: [%t] %c, %p, %X{ProcessId}: %m%n" />  
           </layout>  
      </appender>  
      <!-- Place your application's package whose classes needs to be logger enabler at Debug level. -->  
      <logger name="<Your application package>" additivity="false">  
           <level value="DEBUG" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <logger name="org.springframework" additivity="false">  
           <level value="INFO" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <logger name="org.hibernate" additivity="false">  
           <level value="INFO" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <root>  
           <priority value="DEBUG" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </root>  
 </log4j:configuration>  
  • In the spring configuration, define a bean of type 'org.springframework.beans.factory.config.MethodInvokingFactoryBean' like this:
 
   <bean id="log4jInitialization"  
           class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
           <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />  
           <property name="targetMethod" value="initLogging" />  
           <property name="arguments">  
                <list>  
                     <value>classpath:config/log4j.xml</value>  
                </list>  
           </property>  
      </bean>  
          The above bean is for the class org.springframework.beans.factory.config.MethodInvokingFactoryBean, which takes in 3 arguments. First the targetClass, which in this case is Spring's Log4jConfigurer class and the second argument will be the method "initLogging" of class Log4jConfigurer, which will be called with the third arguments passed in as the third argument.
We will pass the location of the log4j.xml.

During runtime, MethodInvokingFactoryBean will invoke the method "initLogging" of class Log4jConfigurer with the arguments passes to it through property "arguments"

  • Class in which the logging should be used should have a declaration like this.   org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(clazz);
  • Level of logging can be set in the log4j.xml.

Saturday, March 2, 2013

Insertion Sort


Insertion sort, as the name says works by inserting the element in proper position of a sorted list. This works by first marking a element in the array as a markedElement. All the elements before the markedElement will be sorted. All the elememts to the right of the markedElement will be unsorted.
Sorting proceeds by comparing the markedElement with the sorted elements, inserting the markedElement in the correct position based on the comparision.

In Detail: For example, if we consider an array of N elements, to startwith, we will consider the second element as the markedElement. So, to the left of the markedElement is only one element, which we consider as sorted.(Remember, elements towards left of the markedElement is considered as sorted).
We proceed by comparing the markedElement with the largest element(rightmost element) in the sorted list. Upon each pass, the size of the sorted sub-list will increase.

Best case sorting is when the elements are already sorted. Then, the sort has a complexity of O(N). Worst case is when a array is sorted in the reverse order. Thats when the complexity will be quadratic O(N2).
However, insertion sort is considered as a quickest way to sort small data. It is considered quicker than quickSort for small data. It is not a good solution for sorting large data.



InsertionSort Program

package com.algoWork.sort;

import java.util.ArrayList;
import java.util.List;

public class InsertionSort<T extends Comparable<T>> {

    public void sort(List<T> arrayElements) {

        // Iterate over the arrayElements from second element
        for (int i = 1; i < arrayElements.size(); i++) {

            // consider the secondElement as the markedElement
            T markedElement = arrayElements.get(i);
            // Get the index of the markedElement
            int markedElementIndex = i;
            // Keep shifting the elements in the subList towards right until you
            // find the correct position to insert the markedElement. This
            // Comparison will be done till markedIndex is greater than 0 and
            // markedElement is less that the compared element in the subList.

            while (markedElementIndex > 0
                    && markedElement.compareTo(arrayElements
                            .get(markedElementIndex - 1)) < 0) {
                arrayElements.set(markedElementIndex,
                        arrayElements.get(markedElementIndex - 1));
                markedElementIndex--;
            }
            // At the end of the loop, correct position to insert the
            // markedElement in the subList will be found and insert the element
            // in that position.
            arrayElements.set(markedElementIndex, markedElement);
        }

    }

}


Complexity: Complexity of this sorting mechanism is O(N2). But this is twice as fast compared to Bubble sort and Selection sort.

Reference: Data Structures and Algorithms 2nd Edition Robert Lafore