【问题标题】:Spring Security JDBC and Hibernate JPASpring Security JDBC 和 Hibernate JPA
【发布时间】:2023-04-08 07:12:01
【问题描述】:

我正在编写一个 spring webapp,它使用带有 jdbc 和 jpa/hibernate 的 spring 安全性(我是 spring 的 100% 新手)。我通过在“dataSource”bean 中配置数据库以实现 spring 安全性并在 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>

感谢任何提示。

【问题讨论】:

    标签: java spring hibernate jpa spring-security


    【解决方案1】:

    您实际上可以使用 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,如果您想了解更多信息,但这只是设置休眠/JPA 的众多其他方法之一。

    【讨论】:

    • 我决定走这条路。由于我的设置以及我需要通过事务解决的一些问题,需要进一步配置。谢谢!
    【解决方案2】:

    您可以删除整个 persistence.xml 并仅使用 spring 上下文来创建您的 entityManager。应该是这样的……

        <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:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="com.app.domain" />
            <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"/>
            <property name="persistenceUnitName" value="persUnit" />
            <property name="mappingResources" value="META-INF/orm.xml"/>
            <property name="jpaProperties">
                <props><prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</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>
    
    </beans>
    

    --EDIT-- 我在这里引用数据源,但对我来说它在另一个上下文文件中。您还需要在此处添加您的。

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

    --EDIT2--

    然后将您的 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>
    

    然后在 spring 中定义你的 JPA 属性。

    【讨论】:

    • 好吧,我决定不这样解决问题。我更喜欢使用persistence.xml 文件,以使Eclipse 中的JPA 方面满意。无论如何感谢您的回复! (抱歉,我是新人,我无法按我的意愿投票支持您的回复)
    • @sebastian 看看我的 EDIT 2 然后
    猜你喜欢
    • 2012-12-04
    • 2020-08-06
    • 2017-07-17
    • 1970-01-01
    • 2018-01-13
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    相关资源
    最近更新 更多