Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Annotations and mappingJarLocations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Annotations and mappingJarLocations

    I am trying to load all my annotated persistant classes via the
    following bean definition...
    The first bean definition use LocalSessionFactoryBean which works, along
    with the mapping file...
    The second bean which uses AnnotationSessionFactoryBean , is NOT able to
    read the persistant classes from the specified jar file ..

    Code:
    <!-- this works -->
    <bean id="sfOne" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" 
    	 parent="abstractSessionFactory"   >
    		<property name="dataSource" ref="ds" />
    <property name="mappingJarLocations">
    	        <list>
    	            <value>classpath:hbmFiles.jar</value>            
    	        </list>
        	</property>
    </bean>
    
    
    <!-- this does NOT work -->
    <bean id="sfTwo" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" 
    	 parent="abstractSessionFactory"   >
    		<property name="dataSource" ref="ds" />
    
    <property name="mappingJarLocations">
    	        <list>
    	            <value>classpath:hbmFiles.jar</value>            
    	        </list>
        	</property>
    </bean>
    But it is not able to find any annotated files . Reading Hibernate documentation, it say I need to speficy it via the
    <property name="annotatedClasses"> and
    <property name="annotatedPackages"> properties...

    But I dont want to list the induvidual class, pkgs one by one... Surely there
    is a way to read from a jar file ?

  • #2
    Originally posted by prash57 View Post
    But it is not able to find any annotated files . Reading Hibernate documentation, it say I need to speficy it via the
    <property name="annotatedClasses"> and
    <property name="annotatedPackages"> properties...

    But I dont want to list the induvidual class, pkgs one by one... Surely there
    is a way to read from a jar file ?
    I think this is how you have to do it, have you tried wildcards?
    Last edited by karldmoore; Aug 29th, 2007, 11:47 AM.

    Comment


    • #3
      Extension of AnnotatedSessionFactoryBean

      well,

      I searched all thro the forum and didnt come across any solution to this problem.. So I decided to extend the AnnotatedSessionFactoryBean to add the functionality I wanted...

      Below is the attached file for anyone who wants it...
      Basically it takes in a list of jar files and extracts the annotated classes out of it , and adds it to the list of annotatedClasses...

      let me know if anyone has any better way of achieving this...

      Code:
      /**
       * 
       */
      package mypkg;
      
      import java.io.File;
      import java.util.ArrayList;
      import java.util.Enumeration;
      import java.util.Iterator;
      import java.util.List;
      import java.util.jar.JarEntry;
      import java.util.jar.JarFile;
      
      import javax.persistence.Entity;
      
      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import org.springframework.beans.factory.InitializingBean;
      import org.springframework.core.io.ClassPathResource;
      import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
      
      /**
       * @author Prashanth Chandran
       *
       */
      public final class MyAnnotationSessionFactoryBean extends
      		AnnotationSessionFactoryBean implements InitializingBean{
      
      	private static Log log = LogFactory.getLog(MyAnnotationSessionFactoryBean.class);
      	
      	/**
      	 * Contains the List of strings representing the jar locations 
      	 * This will be supplied by the enduser via Spring beans 
      	 */
      	private List<String> annotatedMyJarLocations = null;
      	
      	/**
      	 * Contains the List of AnnotatedClasses
      	 * This will be populated by this class, after processing the entries in the annotatedMyJarLocations
      	 */
      	private List<Class> annotatedMyClasses = new ArrayList<Class>();
      	
      	/**
      	 * Sets the list of jar File locations containing all the Hibernate Annotated Classes
      	 * @param jarFileNames
      	 */
      	public void setAnnotatedMyJarLocations(List<String> jarFileNames){
      		annotatedMyJarLocations = jarFileNames;
      	}
      	
      	/**
      	 * 
      	 * @return List of jar file names that contains the Hibernate Annotated classes
      	 * @throws Exception
      	 */
      	public List<String> getAnnotatedMyJarLocations() throws Exception {
      		return annotatedMyJarLocations;
      	}
      	
      	/**
      	 * Reads the entries from the specified jarFileName
      	 * and calls the processJarEntry for processing the entry
      	 * @param jarFileName the jarFileName available as a classpathResource
      	 * @throws Exception
      	 */
      	public void readEntriesFromJarFile(String jarFileName) throws Exception {
      		
      		File f = new ClassPathResource(jarFileName).getFile();
      		JarFile jarFile = new JarFile(f);
      		Enumeration<JarEntry> entries = jarFile.entries();
      	       while (entries.hasMoreElements()) {
      	         processJarEntry(entries.nextElement());
      	       }
      	}
      	
      	/**
      	 * Processes each class entry from the jarFile
      	 * Ignores all inner classes
      	 * @param jarEntry
      	 * @throws Exception
      	 */
      	public void processJarEntry(JarEntry jarEntry) throws Exception {
      		String name = jarEntry.getName();
      		if (name.endsWith(".class") && !name.contains("$")){
      			checkAndAddAnnotatedClasses(name);
      		}
      	}
      	
      	/**
      	 * Checks if the specified class name is a Hibernate annotated Class
      	 * If so, it adds it to the list of AnnotatedClasses
      	 * 
      	 * @param name
      	 * @throws Exception
      	 */
      	public void checkAndAddAnnotatedClasses(String name) throws Exception {
      		
      		// since the name is in the format com/abcd/efg/ClassA.class  , we need to change it to 
      		// com.abcd.efg.ClassA before we can use it 
      		name = name.replace("/", ".");
      		name = name.substring(0, name.length()-6);
      		
      		Class c = Class.forName(name);
      		if (c.isAnnotationPresent(Entity.class)){
      			annotatedMyClasses.add(c);
      			log.debug(c+" Annotations Present");
      		} else {
      			log.debug(c+" Annotations NOT Present");
      		}
      	}
      	
      	
      	/**
      	 * Launch pad for processing the annotations in the jar files
      	 * @throws Exception
      	 */
      	public void processAnnotations() throws Exception {
      		Iterator<String> iter = annotatedMyJarLocations.iterator();
      		while(iter.hasNext()){
      			String jarFileName = iter.next();
      			readEntriesFromJarFile(jarFileName);
      		}
      		
      		// if the annotatedMyClasses is not empty, set the AnnotatedClasses in the superclass 
      		if (!annotatedMyClasses.isEmpty()){
      			Class[] annotatedClassArray =new Class[annotatedMyClasses.size()];
      			annotatedMyClasses.toArray(annotatedClassArray);
      			setAnnotatedClasses(annotatedClassArray);
      		}
      		
      	}
      	
      	@Override
      	public void afterPropertiesSet() throws Exception {
      		processAnnotations();
              super.afterPropertiesSet();
              
          }
      	
      	
      	/**
      	 * main Method - Testing purposes only 
      	 * @param args
      	 */
      	public static void main(String[] args) throws Exception {
      		
      		MyAnnotationSessionFactoryBean test = new MyAnnotationSessionFactoryBean();
      		List<String> jarList = new ArrayList<String>();
      		jarList.add("annotated.jar");
      		test.setAnnotatedMyJarLocations(jarList);
      		test.processAnnotations();
      		
      
      	}
      
      	
      	
      }

      and the bean definition for this...

      Code:
      <bean id="sessionFactory-Annotation" class="mypkg.MyAnnotationSessionFactoryBean" 
      	 parent="abstractSessionFactory"   >
      		<property name="dataSource" ref="dataSource" />
      
      
      		<!-- 
      		<property name="annotatedClasses">
      	     <list>
      	       <value>junk.CountryJunkVO</value>
      	       
      	  
      	     </list>
      	   </property>
      		-->
      	
      		<property name="annotatedMyJarLocations">
      			<list>
      		       <value>annotations.jar</value>
      		    </list>
      		</property>
      		
      		
      	   
      	</bean>
      Last edited by prash57; Jul 6th, 2007, 01:47 PM.

      Comment

      Working...
      X