Results 1 to 6 of 6

Thread: Hibernate Annotations and Spring

  1. #1
    Join Date
    Aug 2004
    Posts
    10

    Default Hibernate Annotations and Spring

    Hello

    How can I use Hibernate Annotations with Spring ?

    I think that Spring have some configuration for it.
    today I use to:
    Code:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="mappingResources">
          <list>
             <value>com/model/MyModel.hbm.xml</value>
          </list>
       </property>
       .
       .
       .
    and with Annotations, How Can I do ?

    []s

  2. #2
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Last edited by robyn; May 14th, 2006 at 11:21 AM.
    --Jing Xue

  3. #3
    Join Date
    Aug 2004
    Posts
    10

    Default

    I saw your Thread in http://forum.springframework.org/showthread.php?t=13796, I wrote the AnnotationSessionFactoryBean and change my Spring configurations, but now, I'm geting a error in my application startup, see my config files and the ERROR.
    Code:
    import java.util.List;
    import org.hibernate.HibernateException;
    import org.hibernate.MappingException;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.hibernate.cfg.Configuration;
    import org.springframework.context.ApplicationContextException;
    import org.springframework.orm.hibernate3.LocalSessionFactoryBean;
    
    public class AnnotationSessionFactoryBean extends LocalSessionFactoryBean {
    
        private static final Logger LOG = Logger.getLogger(AnnotationSessionFactoryBean.class); 
    
        private List<String> annotatedClasses_; 
        
        /** 
         * @return the classes. 
         */ 
        public List getAnnotatedClasses() { 
            return annotatedClasses_; 
        } 
        
        /** 
         * @param classes The classes to set. 
         */ 
        public void setAnnotatedClasses(List<String> classes) {
            annotatedClasses_ = classes; 
        } 
    
        @Override 
        protected void postProcessConfiguration(Configuration config) throws HibernateException { 
            super.postProcessConfiguration(config); 
    
            if (!(config instanceof AnnotationConfiguration)) { 
                throw new ApplicationContextException("The configuration must be AnnotationConfiguration."); 
            } 
    
            if (annotatedClasses_ == null) { 
                LOG.info("No annotated classes to register with Hibernate."); 
                return; 
            } 
    
            for (String className : annotatedClasses_) {
                try { 
                    Class clazz = config.getClass().getClassLoader().loadClass(className); 
                    ((AnnotationConfiguration)config).addAnnotatedClass(clazz); 
    
                    if (LOG.isDebugEnabled()) { 
                        LOG.debug("Class " + className + " added to Hibernate config."); 
                    } 
                } 
                catch (MappingException e) { 
                    throw new ApplicationContextException("Unable to register class " + className, e); 
                } 
                catch (ClassNotFoundException e) { 
                    throw new ApplicationContextException("Unable to register class " + className, e); 
                } 
            } 
        }
    }
    hibernate.cfg.xml
    Code:
    <!DOCTYPE hibernate-configuration PUBLIC
    	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
    	<session-factory>
    		<mapping class="com.probel.model.sales.PedidoCapa"/>
    		<mapping class="com.probel.model.sales.PedidoItem"/>
    		<mapping class="com.probel.model.sales.TabPreco"/>
        </session-factory>
    </hibernate-configuration>
    part of spring-hibernate.cfg
    Code:
    .
    .
    .
    	<bean id="sessionFactory" class="com.home.core.AnnotationSessionFactoryBean">
            <property name="dataSource"><ref local="dataSource"/></property>
    		<property name="configLocation"><value>/WEB-INF/hibernate.cfg.xml</value></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
                    <prop key="hibernate.show_sql">false</prop>
                </props>
            </property>
    	</bean>
    .
    .
    .
    Error on Application startup
    Code:
     11:54:54,875 ERROR ContextLoader - Context initialization failed
     org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-hibernate.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping clazz="com.probel.model.sales.PedidoCapa"/>
    org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping clazz="com.probel.model.sales.PedidoCapa"/>
    	at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1322)
    	at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1284)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1266)
    	at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1233)
    	at org.hibernate.cfg.Configuration.configure(Configuration.java:1176)
    	at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:448)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1075)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:349)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:257)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:222)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:146)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:285)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:317)
    	at org.springframework.web.context.support.AbstractRefreshableWebApplicationContext.refresh(AbstractRefreshableWebApplicationContext.java:131)
    	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:224)
    	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:150)
    	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:48)
    	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3637)
    	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4073)
    	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:759)
    	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:739)
    	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:524)
    	at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:909)
    	at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:872)
    	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:474)
    	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1106)
    	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:310)
    	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1019)
    	at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
    	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1011)
    	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:440)
    	at org.apache.catalina.core.StandardService.start(StandardService.java:450)
    	at org.apache.catalina.core.StandardServer.start(StandardServer.java:683)
    	at org.apache.catalina.startup.Catalina.start(Catalina.java:537)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:271)
    	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:409)
    Last edited by robyn; May 14th, 2006 at 11:20 AM.

  4. #4
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    try set this property to your session factory bean:
    <property name="configClass"><value>org.hibernate.cfg.Annota tionConfiguration</value></property>

    BTW, if you are already using a separate hibernate.cfg.xml, you don't need to bother with AnnotationSessionFactoryBean. The Hibernate Annotations package has built-in support for adding annotated classes through hibernate.cfg.xml. AnnotationSessionFactoryBean only makes sense if you want to keep everything in spring bean definitions.
    --Jing Xue

  5. #5
    Join Date
    Aug 2004
    Posts
    10

    Default

    very tks buddy, now my application work with hibernate annotations, but a observation, the right is :
    Code:
    <property name="configurationClass">
       <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    and not configClass

    []s

  6. #6
    Join Date
    Mar 2009
    Posts
    21

    Default

    thanks for posting your solution.
    I had a similar problem.

Similar Threads

  1. Closer ties between Spring and AspectJ
    By Rod Johnson in forum Announcements
    Replies: 3
    Last Post: Sep 7th, 2005, 10:26 AM
  2. Replies: 2
    Last Post: Aug 18th, 2005, 12:49 PM
  3. Spring Modules 0.2 Released
    By robh in forum Announcements
    Replies: 0
    Last Post: May 23rd, 2005, 09:29 AM
  4. Spring Framework 1.2RC1 released
    By Colin Sampaleanu in forum Announcements
    Replies: 1
    Last Post: Apr 7th, 2005, 09:50 AM
  5. Spring 1.2 / Annotations
    By mattinger in forum Container
    Replies: 15
    Last Post: Mar 16th, 2005, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •