Results 1 to 3 of 3

Thread: Getting started with Microsoft JDBC in Hibernate

  1. #1
    Join Date
    Oct 2012
    Posts
    21

    Question Getting started with Microsoft JDBC in Hibernate

    I am a long time .Net programmer who is relativity new to Java, very new to Spring and Hibernate. I am working my way through the book "Pro Spring 3" and got to the 9 chapter where the book uses H2, I would like to use SQL Server 2012. I have the Microsoft JDBC Driver 4.0 for SQL Server installed and added to the class path. What I cannot find is any info on how to modify the book's app-context.xml to use SQL Server. Here is the code from the book:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
        xsi:schemaLocation="
            http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
        <jdbc:embedded-database id="dataSource" type="H2">
            <jdbc:script location="classpath:schema.sql"/>
            <jdbc:script location="classpath:test-data.sql"/>
        </jdbc:embedded-database>
        
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"/>
        </bean>    
        
        <tx:annotation-driven/>
    
        <context:component-scan base-package="com.apress.prospring3.ch9" />
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
            <property name="packagesToScan" value="com.apress.prospring3.ch9.domain"/>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                    <prop key="hibernate.max_fetch_depth">3</prop>
                    <prop key="hibernate.jdbc.fetch_size">50</prop>
                    <prop key="hibernate.jdbc.batch_size">10</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>        
        </bean>
    
    </beans>

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    1. Change datasource to a MSSQLServer one (instead of the embedded)
    2. Change the hibernate dialect
    3. Run it...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2012
    Posts
    21

    Default

    Marten,

    Thank you very much your help. This is what I came up with for creating the datasource, but I am getting an error:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schem...g-jdbc-3.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-3.1.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <bean id="datasource" class="com.microsoft.sqlserver.jdbc.SQLServerDataS ource">
    <property name="user" value="devUser"/>
    <property name="password" value="*****"/>
    <property name="serverName" value="localhost"/>
    <property name="portNumber" value="1433"/>
    <property name="databaseName" value="ProSpringCh8"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:annotation-driven/>

    <context:component-scan base-package="com.accumed.spring_hibernate_test" />

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="datasource"/>
    <property name="packagesToScan" value="com.apress.prospring3.ch9.domain"/>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLS erver2008Dialect</prop>
    <prop key="hibernate.max_fetch_depth">3</prop>
    <prop key="hibernate.jdbc.fetch_size">50</prop>
    <prop key="hibernate.jdbc.batch_size">10</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    </beans>

    The error I am getting is:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'hibernateConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire field: private javax.sql.DataSource com.accumed.spring_hibernate_test.HibernateConfigu ration.dataSource; nested exception is org.springframework.beans.factory.BeanExpressionEx ception: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluation Exception: EL1008Epos 0): Field or property 'dataSource' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpr essionContext'

    Sam

Posting Permissions

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