【问题标题】:Spring+JPA+Hibernate+Postgres configurationSpring+JPA+Hibernate+Postgres 配置
【发布时间】:2015-06-25 01:59:16
【问题描述】:

我有适用于 MySQL 的配置,但我现在想切换到 Postgres。我想保留它没有特定于休眠的类和配置文件,例如 SessionFactory 和 hibernate.cfg.xml。而且我想保持我的 persistence.xml 最小化。 当应用程序在 Tomcat 上启动时,使用下面的配置,我没有自动创建表。数据库 FitnessTracker 存在,Postgres 服务器正在运行。 如何测试我的配置?非常欢迎在此阶段就更详细的调试信息提出任何建议。

我的配置:

web.xml

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/jpaContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>    

<servlet>
<servlet-name>fitTrackerServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>....</web-app>

jpaContext.xml (Postgres)

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

<context:annotation-config />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitName" value="punit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="com.pluralsight.model"/>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true"/>
        </bean>
    </property>

    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <entry key="hibernate.hbm2ddl.auto" value="create"/>
            <entry key="hibernate.format_sql" value="true"/>
        </map>
    </property>
 </bean>

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

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost:5432/fitnessTracker"/>
    <property name="username" value="postgres"/>
    <property name="password" value="admin"/>
</bean>

META-INF/persistence.xml

<persistence 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"
version="2.0">

我的实体类

package com.pluralsight.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Activity {

@Id
@GeneratedValue
private Long id;

private String desc;

...getters and setters

}

【问题讨论】:

  • 您的具体问题是什么?如果您的问题是“我如何测试我的配置?”,那么您可以启动您的应用程序,使用它,看看它是否按预期工作。或者你编写自动化测试。

标签: spring hibernate postgresql jpa configuration


【解决方案1】:

配置是正确的,我的问题是在结果战争中没有 maven 资源文件夹。

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 2020-08-06
    • 2016-09-17
    • 2011-09-02
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    相关资源
    最近更新 更多