Spring Cfg+Hibernate Cfg和注解

Spring Cfg+Hibernate Cfg and Anotations

我想用 HQL 做一个测试,但是映射表是用 Annotations 的。 Hibernate 配置文件在 /WEB-INF/spring-config-ws.xml :::>

<!-- Activate transaction declarations with annotations -->
<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- Property files application uses -->
<bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>

<!-- JNDI DataSource -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="${dataSource.jndiName}" />
    <property name="lookupOnStartup" value="false"/>
    <property name="cache" value="true"/>
    <property name="proxyInterface" value="javax.sql.DataSource"/>
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    <property name="packagesToScan">
        <list>
            <value>es.sergas.rprof.profesional.domain</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.generate_statistics">true</prop>
            <prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
            <prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop>
        </props>
    </property>
</bean>

<!-- Transaction manager for Hibernate SessionFactory -->
<bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

当我 运行 一个列表时,我发现你没有找到 hibernate cfg: hibernate.cfg.xml 未找到

我只想列出带有注释的映射 class,但是 HQL

我觉得我的英语水平很低。谢谢

确保你的 hibernate.cfg.xmlsrc/main/resources 里面,如果文件不在里面你需要指定正确的位置,所以把它放在这个文件夹里面你的问题就会解决。

Note that we don’t have to explicitly mention the mapping or configuration or properties files, because the Hibernate runtime looks for default filenames, such as hibernate. cfg.xml or hibernate.properties, in the classpath and loads them. If we have a nondefault name, make sure you pass that as an argument—like configure("my-hibcfg. xml"), for example.