无法使用SPRING + HIbernate连接多个PostgreSQL数据库

时间:2014-10-04 06:45:38

标签: spring hibernate

我需要连接多个PostgreSQL数据库。我正在使用属性文件来获取数据库连接属性。

当我运行应用程序时,由于找不到命名查询而导致错误。但是当我删除第二个数据库的配置时,每个东西都运行正常。我也尝试使用hibernate映射的目录属性。但它没有任何结果。

    <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                    <list>
                    <value>classpath:com/*****/config/jdbc.properties</value>
                    <value>classpath:com/*****/config/userLogin.properties</value>
                    </list>
            </property>
            <property name="ignoreUnresolvablePlaceholders" value="false"/>
            <property name="order" value = "1"/>
    </bean>

Hibernate与第一个PostgresSQL数据库的连接

    <bean id="teDaDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
              <property name="driverClassName" value ="${jdbc.driverClassName}"/>
                    <property name="url" value="${jdbc.databaseurl}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value ="${jdbc.password}"/>
    </bean>


    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="teDaDataSource"/>
    <property name ="mappingLocations" >

            <list>

                    <value>classpath:Country.hbm.xml</value>
                    <value>classpath:AccountType.hbm.xml</value>
                    <value>classpath:Stocks.hbm.xml</value>
            </list>
    </property>
    <property name = "hibernateProperties">
            <props>
                <prop key="dialect">${hibernate.dialect}</prop>
                <prop key="show_sql">${hibernate.showsql}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="connection.autocommit">true</prop>
                <prop key="hibernate.connection.release_mode">after_transaction</prop>
                <prop key="transaction.auto_close_session">true</prop>
            </props>
    </property>
    </bean>
            <bean id="abstractDaoTarget" class="com.****.generic.dao.GenericDaoImpl" abstract="true">
            <property name="sessionFactory">
            <ref bean="sessionFactory"/>
            </property>
    </bean>

        <bean id = "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="true">
        <property name="sessionFactory" ref="sessionFactory"/>
        </bean>

        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                    <tx:attributes>
                        <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
                        <tx:method name="*" propagation="REQUIRED"/>
                </tx:attributes>
        </tx:advice>

第二个postgres数据库的Hibernate配置

    <bean id="userLoginDaDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                    <property name="driverClassName" value ="${jdbc.driverClassName}"/>
                    <property name="url" value="${jdbc.databaseurl}"/>
                    <property name="username" value="${jdbc.username}"/>
                    <property name="password" value ="${jdbc.password}"/>
    </bean>

     <bean id="userLoginSessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="userLoginDaDataSource"/>
    <property name ="mappingLocations">
     <list>
            <value>classpath:SecurityQuestion.hbm.xml</value>
    </list>
    </property>
    <property name = "hibernateProperties">
            <props>
                <prop key="dialect">${hibernate.dialect}</prop>
                <prop key="show_sql">${hibernate.showsql}</prop>
                <prop key="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="connection.autocommit">true</prop>
                <prop key="hibernate.connection.release_mode">after_transaction</prop>
                <prop key="transaction.auto_close_session">true</prop>
            </props>
    </property>
    </bean>
            <bean id="abstractDaoTarget" class="com.bhaskar.generic.dao.GenericDaoImpl" abstract="true">
            <property name="sessionFactory">
            <ref bean="userLoginSessionFactory"/>
            </property>
    </bean>

        <bean id = "transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="true">
        <property name="sessionFactory" ref="userLoginSessionFactory" />
        </bean>

        <tx:advice id="txAdvice" transaction-manager="transactionManager">
                    <tx:attributes>
                        <tx:method name="get*" read-only="true" propagation="REQUIRED"/>
                        <tx:method name="*" propagation="REQUIRED"/>
                </tx:attributes>
        </tx:advice>

类表映射文件

    <hibernate-mapping>
    <class name="com.*****.entity.Country"  table="primary_data.country">
    <id name="countryId" type="int">
    <column name="country_id" not-null="true"/>
    <generator class="increment"/>
    </id>
    <property name="countryName" type="string">
    <column name="country_name" length="100" not-null="true"/>
    </property>

    <property name="countryDesc" type="string">
    <column name="country_desc" length="100"/>
    </property>

    <property name="countryCapital" type="string">
    <column name="country_capital" length="100" not-null="true"/>
    </property>
    </class>
    <query name="getAllCountry">from Country </query>
    </hibernate-mapping>

第二个数据库的属性文件配置

    jdbc.driverClassName=org.postgresql.Driver
    jdbc.databaseurl=jdbc\:postgresql\://localhost\:5432/userLogin
    jdbc.username=******
    jdbc.password=******
    hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    hibernate.showsql=false
    hibernate.hbm2ddl.auto=update

第一个数据库的属性文件配置

    jdbc.driverClassName=org.postgresql.Driver
    jdbc.databaseurl=jdbc\:postgresql\://localhost\:5432/*****
    jdbc.username=*******
    jdbc.password=******
    hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    hibernate.showsql=false
    hibernate.hbm2ddl.auto=update

我们将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

错误消息显示:

  

命名查询中的错误:getAllCountry

getAllCountry查询

Select * from Country

这不是有效的HQL。这是SQL。 SQL!= HQL。有效的HQL查询将是

select c from Country c