Results 1 to 3 of 3

Thread: sessionFactory bean can't read entities from hibernate.cfg.xml when packaged in JAR

  1. #1
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default sessionFactory bean can't read entities from hibernate.cfg.xml when packaged in JAR

    We're having an interesting issue with Spring + Hibernate + Maven in AppFuse 2.x (Maven 2.0.4, Hibernate 3.2.1 and Spring 2.0.1).

    We have a "hibernate" module that contains the following bean definition for its sessionFactory bean.

    Code:
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
            <property name="hibernateProperties">
                <value>
                    hibernate.dialect=${hibernate.dialect}
                    hibernate.hbm2ddl.auto=update
                    hibernate.query.substitutions=true 'Y', false 'N'
                </value>
            </property>
        </bean>
    This sessionFactory is defined in an applicationContext-dao.xml that's packaged in the root of the JAR. It's then loaded with the following in web.xml:

    Code:
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath*:/applicationContext-resources.xml
                classpath*:/applicationContext-dao.xml
                classpath*:/applicationContext-service.xml
                /WEB-INF/applicationContext*.xml
                /WEB-INF/security.xml
            </param-value>
        </context-param>
    For some reason, the sessionFactory can't seem to read the hibernate.cfg.xml in the local project. I get an "Unknown Entity" exception. Using a PostProcessor works (see below), but the hibernate3-maven-plugin requires annotated classes be configured in hibernate.cfg.xml. It does support a <scan-jars/> configuration, but it takes twices as long as using hibernate.cfg.xml.

    Code:
        <bean class="org.appfuse.dao.spring.HibernateExtensionPostProcessor">
            <property name="annotatedClasses">
                <list>
                    <value>org.appfuse.tutorial.model.Person</value>
                </list>
            </property>
        </bean>
    Changing to use classpath*:hibernate.cfg.xml doesn't seem to help. Re-defining the sessionFactory bean in a local context file doesn't work. Registering the class with hibernate.cfg.xml and a PostProcessor is our current solution.

    Any ideas?

    Another issue I'm experiencing is HQL seems to require the fully-qualified class name.

    http://issues.appfuse.org/browse/APF-571

    Thanks,

    Matt

  2. #2
    Join Date
    Aug 2004
    Location
    Denver
    Posts
    249

    Default

    I figured out the first problem - there was a hibernate.cfg.xml packaged in one of my dependent JARs.

    I'm still stumped by the 2nd issue though. I'm assuming that if you use hibernate.cfg.xml with an AnnotationSessionFactoryBean, you have to use FQ class names in your HQL?

    Thanks,

    Matt

  3. #3
    Join Date
    Aug 2004
    Posts
    1,107

    Default

    Matt,

    I saw this comment in the AppFuse JIRA
    I fixed the FQ class name problem by changing @Entity(name="person") to @Entity. The name specified in this annotations creates an alias for HQL. I'll add this to the tutorial.
    If you use
    Code:
    @Entity(name="Person")
    (this should be the same as the default if you don't specify this parameter) can you then refer to the class with "Person" without the FQ name. That's how it works in JPQL and I would assume HQL with annotations should work that way as well.
    Thomas Risberg
    SpringSource by Pivotal
    http://www.springsource.org

Posting Permissions

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