简体   繁体   中英

Spring Security JDBC and Hibernate JPA

I'm writing a spring webapp that uses spring security with jdbc and jpa/hibernate (I'm 100% newbie with spring). I have made it work by configuring the database either in a "dataSource" bean for spring security and repeating it in persistence.xml. I tried to use the datasource inside the persistence, but I had no luck. How can I reuse my datasource configuration inside persistence.xml? My working configuration is:

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>

Any hint is appreciated.

You can drop your whole persistence.xml and only use the spring context to create your entityManager. It would be something like this...

    <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-- I'm referencing the datasource here but for me it's in another context file. You would need to add yours here as well.

<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--

Just leave your persistence.xml empty then. Like...

<?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>

then define your JPA properties inside spring.

You can actually ask spring to handle that for you using LocalContainerEntityManagerFactoryBean :

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

Hence you don't need to rewrite your datasource config on persistence.xml. However this is normally used if you container do not already come with its own EntityManagerFactory (eg: tomcat, jetty). Some profiles of JBoss, Glassfish, Websphere have their own EntityManagerFactory.

I have a blog post on setting up spring and jpa if you like to see more, however this is just one of many other method on how you can setup hibernate / JPA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM