Announcement Announcement Module
Collapse
No announcement yet.
Problem with JdbcTemplate using Hibernate Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem with JdbcTemplate using Hibernate

    Hi all and thanks in advance.

    I'm using an unitary test class that inherits from AbstractTransactionalDataSourceSpringContextTests. One test checks if the number of elements returned by an HQL query using JpaTemplate is equals to the value returned using JdbcTemplate and does not work. The other tests works fine!

    Note I'm running Mysql Server with to schemas, "my_schema" and "my_schema_test".

    Whenever I use jdbcTemplate.queryForInt(query) the query is launched against "my_schema", however my configuration specifies "my_schema_test" in the query string!!!!!!!!!

    This is the Spring configuration:
    Code:
    <util:properties id="hbmProperties"
    	location="classpath:/es/uniovi/data/hibernate.properties" />
    
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    	p:jpa-properties-ref="hbmProperties"
    	p:persistence-provider-class="org.hibernate.ejb.HibernatePersistence"
    	p:persistence-unit-name="sies_fw"
    	p:persistence-xml-location="classpath*:META-INF/persistence.xml" />
    
    <bean id="jpaTemplate"
    	class="org.springframework.orm.jpa.JpaTemplate"
    	p:entityManagerFactory-ref="entityManagerFactory" />
    And this one the hibernate.properties file:
    hibernate.connection.url=jdbc:mysql://localhost:3306/my_schema_test
    hibernate.connection.driver_class=com.mysql.jdbc.D river
    hibernate.connection.username=root
    hibernate.connection.password=xxxxxxxxxxx
    hibernate.dialect=org.hibernate.dialect.MySQLDiale ct

    ...

  • #2
    Where is the dataSource configuration? Is that set for the correct schema?
    Last edited by karldmoore; Aug 27th, 2007, 03:18 PM.

    Comment


    • #3
      As far as I'm concerned no datasource declaration is needed, all the configuration parameters are included in the properties file referenced by the bean hbmProperties.

      However, for your information, the application context also includes a datasource bean using the properties above through a PropertiesPlaceholderConfigurer for other purposes.

      The schema used by calls to getJpaTemplate is just the one defined in the properties file, as it's intended. However when I use getJdbcTemplate it seems the system unknown the correct schema and select one ┐randomly?.

      Comment


      • #4
        Ok, I'm slight confused. Where are you getting the dataSource from to give to JdbcTemplate?
        Last edited by karldmoore; Aug 27th, 2007, 03:18 PM.

        Comment


        • #5
          The class AbstractTransactionalDataSourceSpringContextTests needs and application context (getConfigLocations()).

          This application context is the one defined in my previous post.

          So, the connection parameters are defined in a properties file injected to the entityManagerFactory,

          Code:
          <util:properties id="hbmProperties" location="classpath:/es/uniovi/data/hibernate.properties" />
          
          <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
          	p:jpa-properties-ref="hbmProperties"
          	p:persistence-provider-class="org.hibernate.ejb.HibernatePersistence"
          	p:persistence-unit-name="sies_fw"
          	p:persistence-xml-location="classpath*:META-INF/persistence.xml" />
          These are the properties (and any more)
          hibernate.connection.url=jdbc:mysql://localhost:3306/my_schema_test
          hibernate.connection.driver_class=com.mysql.jdbc.D river
          hibernate.connection.username=root
          hibernate.connection.password=xxxxxxxxxxx
          hibernate.dialect=org.hibernate.dialect.MySQLDiale ct


          Thank you very much

          Comment


          • #6
            The method getJdbcTemplate() is provided by AbstractTransactionalDataSourceSpringContextTests, no more configuration is needed.

            Comment


            • #7
              Originally posted by julicrack View Post
              The method getJdbcTemplate() is provided by AbstractTransactionalDataSourceSpringContextTests, no more configuration is needed.
              It does indeed, you're absolutely right. What I don't understand however, is if you haven't injected a dataSource, jdbcTemplate should be null and hence it just wouldn't work. The test does autowiring so I can understand that the dataSource would have been wired for you, what I don't understand is that you said you don't have a dataSource.

              AbstractTransactionalDataSourceSpringContextTests. java
              Code:
              	/**
              	 * Setter: DataSource is provided by Dependency Injection.
              	 */
              	public void setDataSource(DataSource dataSource) {
              		this.jdbcTemplate = new JdbcTemplate(dataSource);
              	}
              
              	/**
              	 * Return the JdbcTemplate that this base class manages.
              	 */
              	public final JdbcTemplate getJdbcTemplate() {
              		return this.jdbcTemplate;
              	}
              Last edited by karldmoore; Aug 27th, 2007, 03:18 PM.

              Comment


              • #8
                The test does autowiring so I can understand that the dataSource would have been wired for you
                I have found the problem! Just In fact there is a datasource with other purposes and the test does autowiring. The connection used is then erroneus.

                I was really confused! There is no magic, just autowiring, thank you very much!!!

                Comment


                • #9
                  Originally posted by julicrack View Post
                  I have found the problem! Just In fact there is a datasource with other purposes and the test does autowiring. The connection used is then erroneus. I was really confused! There is no magic, just autowiring, thank you very much!!!
                  Fantastic, so it's all fixed up then! Glad you got it working!
                  Last edited by karldmoore; Aug 27th, 2007, 03:17 PM.

                  Comment

                  Working...
                  X