0

我正在使用 Spring data JPA、Hibernate 和 MS SQL Server 设置一个应用程序,不幸的是,我的配置搞砸了。

我希望这里的代码能让事情更清楚:。

这是我的 mvc-dispatcher.xml,它是应用程序上下文:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

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

<context:property-placeholder location="classpath:db.properties"/>


<!--  DATA BASE -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="SQL_SERVER" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="punit" />
</bean>

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

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<jpa:repositories base-package="com.yyy.yyy.yyy.repository"/>

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

现在,我还有一个 persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="punit">     
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.connection.url" value="${jdbc.url}" />
            <property name="hibernate.connection.driver_class" value="${jdbc.driverClass}" />
            <property name="hibernate.connection.username" value="${jdbc.user}" />
            <property name="hibernate.connection.password" value="${jdbc.pwd}" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
        </properties>
    </persistence-unit>
</persistence>

我不明白我应该把数据库连接数据放在哪里?而且我也无法连接到数据库(我的连接被拒绝) - 我在服务器启动时遇到异常。

我的项目在 Tomcat 上运行,我使用https://github.com/SpringSource/spring-data-jpa-examples作为模板。

编辑 我设法克服了这些问题并成功发布到 tomcat,方法是删除持久性 XML 文件并仅使用 spring 上下文文件:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="         http://www.springframework.org/schema/beans              http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/context          http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/data/jpa          http://www.springframework.org/schema/data/jpa/spring-jpa.xsd         http://www.springframework.org/schema/tx          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
   <context:component-scan base-package="yyy.yyy.yyy" />
   <context:property-placeholder location="classpath:db.properties" />
   <!-- DATA BASE -->
   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="yyy.yyy.yyy.yyy.domain" />
      <property name="jpaVendorAdapter">
         <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="false" />
            <property name="showSql" value="true" />
            <property name="database" value="SQL_SERVER" />
            <property name="databasePlatform" value="org.hibernate.dialect.SQLServerDialect" />
         </bean>
      </property>
   </bean>
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver" />
      <property name="url" value="jdbc:jtds:sqlserver://localhost;databaseName=db" />
      <property name="username" value="yyy" />
      <property name="password" value="yyyyy" />
   </bean>
   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory" ref="entityManagerFactory" />
      <property name="dataSource" ref="dataSource" />
   </bean>
   <tx:annotation-driven transaction-manager="transactionManager" />
   <jpa:repositories base-package="yyy.yyy.yyy.yyy.repository" />
   <!-- MVC -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
         <value>/WEB-INF/pages/</value>
      </property>
      <property name="suffix">
         <value>.jsp</value>
      </property>
   </bean>
</beans>

该应用程序现在加载正常,但是当我使用我的存储库并调用保存时,我得到连接被拒绝的异常。

有谁知道为什么?

请帮忙。伊多布

4

1 回答 1

1

您在db.properites这里指的是文件:<context:property-placeholder location="classpath:db.properties"/> 它在项目中的位置?

于 2013-01-15T09:22:03.183 回答