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.