java - Spring Security JDBC 和 Hibernate JPA

标签 java spring hibernate jpa spring-security

我正在编写一个 spring web 应用程序,它使用 spring security 和 jdbc 和 jpa/hibernate (我是 spring 的 100% 新手)。我通过在 Spring Security 的“dataSource”bean 中配置数据库并在 persistence.xml 中重复它来使其工作。我尝试在持久性中使用数据源,但没有成功。如何在 persistence.xml 中重用我的数据源配置?我的工作配置是:

datasource.xml:

<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"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://host:3306/db"/>
    <property name="username" value="user"/>
    <property name="password" value="pass"/>
</bean>

persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://xmlns.jcp.org/xml/ns/persistence
    http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="GCGastosPersistence">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.mypackage.MyFirstClass</class>
    <class>com.mypackage.MyAnotherClass</class>
    <properties>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="pass"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://host:3306/db"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.schema-generation.database.action" value="none"/>
        <property name="hibernate.connection.charset" value="utf8"/>
        <property name="hibernate.connection.charsetEncoding" value="utf8"/>
        <property name="hibernate.connection.useUnicode" value="true"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    </properties>
</persistence-unit>

任何提示表示赞赏。

最佳答案

实际上,您可以使用 LocalContainerEntityManagerFactoryBean 要求 spring 为您处理该问题:

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
  <property name="persistenceUnitName" value="persistenceUnit"/>
  <property name="dataSource" ref="dataSource"/>
</bean>

因此,您不需要重写 persistence.xml 上的数据源配置。但是,如果您的容器尚未附带自己的 EntityManagerFactory(例如:tomcat、jetty),通常会使用此方法。 JBoss、Glassfish、Websphere 的一些配置文件都有自己的 EntityManagerFactory。

我有a blog post on setting up spring and jpa如果您想了解更多信息,但这只是有关如何设置 hibernate/JPA 的许多其他方法之一。

关于java - Spring Security JDBC 和 Hibernate JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22703161/

相关文章:

java - 如何管理微服务架构中的多对多关系?

java - Spring Roo : How to get the attributes of the model passed to the view

java - 为 GWT/Spring/Hibernate/PostgreSQL 生成服务/dao 层

java - Hibernate Annotations/JPA 映射集合

java - Spring Data JPA/Hibernate - @Repository 中的 findByY(String y) 方法,其中 Y 可通过对象 X 访问

Java模块通信

java - Validate接口(interface)的继承设计

java - Spring引导(JPA测试): Unable to find a @SpringBootConfiguration when doing a JpaTest

hibernate - JBoss 5.1 : Hibernate with JPA

java - 在 Vaadin 7 中,将数据/参数传递给 BrowserWindowOpener 的新 UI 实例?