【问题标题】:Not able to connect multiple PostgreSQL database using SPRING +HIbernate无法使用 SPRING +HIbernate 连接多个 PostgreSQL 数据库
【发布时间】:2023-04-04 10:59:01
【问题描述】:

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

当我运行应用程序时,它会抛出一个错误,因为找不到命名查询。但是当我删除第二个数据库的配置时,一切都运行良好。我也尝试过使用休眠映射的 catalog 属性。但它没有产生任何结果。

    <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>

休眠连接到第一个 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 数据库的休眠配置

    <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

我们将不胜感激

【问题讨论】:

  • 请同时发布完整的堆栈跟踪。
  • 在类路径资源 [com/****/config/applicationContext-base.xml] 中定义名称为“handlerMapping”的 bean 创建错误:bean 初始化失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:调用 init 方法失败;嵌套异常是 org.hibernate.HibernateException:命名查询中的错误:getAllCountry
  • 这不是完整的堆栈跟踪。编辑您的问题,并发布完整的堆栈跟踪。

标签: spring hibernate


【解决方案1】:

错误信息说:

命名查询中的错误:getAllCountry

getAllCountry 查询是

Select * from Country

这不是有效的 HQL。是 SQL。 SQL!= HQL。一个有效的 HQL 查询应该是

select c from Country c

【讨论】:

  • 我尝试过仅使用来自 Country 的产品。它工作,但多个数据库连接没有工作
  • 您需要意识到“没有工作”是对问题的非常糟糕的描述。正如已经被问过两次一样,发布您在问题中遇到的错误的完整堆栈跟踪。
  • 完整堆栈跟踪太大,粘贴在这里
猜你喜欢
  • 2011-06-20
  • 2015-06-11
  • 2022-01-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2018-12-24
  • 1970-01-01
  • 2017-11-27
相关资源
最近更新 更多