Doubt how to configure Spring Data with Hibernate

7

How to configure Spring Data + hibernate

Hello everyone,

I'm new here in stackoverflow and also in developing java Web. I'm learning some Spring Frame stuff and am wondering how I can set up Spring Data in my Dynamic Project.

Below is my configuration and architecture files. If you can help me, thank you very much.

Architecture

persistence.xml

<persistence-unitname="cleanUpUP" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>

        <property name="dialect" value="org.hibernate.dialect.MySQLDialect" />

        <!-- Configuracoes do Hibernate -->
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
        <property name="hibernate.format_sql" value="true" />
        <property name="org.hibernate" value="info" />
        <property name="org.hibernate.SQL.level" value="debug" />
        <property name="org.hibernate.type.level" value="debug" />

    </properties>

</persistence-unit>

spring.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.0.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/task/spring-context-3.0.xsd">

<!-- Basic Configurations -->
<context:annotation-config/>

<context:component-scan base-package="br.com.cleanUp.model"/>
<context:component-scan base-package="br.com.cleanUp.repository"/>
<context:component-scan base-package="br.com.cleanUp.service"/>
<context:component-scan base-package="br.com.cleanUp.controller"/>

<!-- SpringMVC -->
<import resource="spring-mvc.xml"/>

<!-- SpringData -->
<import resource="spring-jpa.xml"/>

spring-mvc

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

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<!-- i18n -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/i18n"/>
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

<!-- View Handler -->
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="favorPathExtension" value="true"/>
    <property name="mediaTypes">
        <map>
            <entry key="xml" value="text/xml"/>
            <entry key="json" value="application/json"/>
            <entry key="html" value="text/html"/>
            <entry key="less" value="text/html"/>
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
            </bean>
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
        </list>
    </property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
        </list>
    </property>
</bean>

spring-jpa subject of doubt

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee"
   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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/task/spring-context-3.0.xsd">

<!-- JPA Configurations -->

<!-- MINHA DÚVIDA É AQUI -->

<!-- Spring Data -->
<jpa:repositories base-package="br.com.cleanUp.repository"
                  entity-manager-factory-ref="entityManagerFactory"
                  transaction-manager-ref="transactionManager"/>

If you can help me and give me a tip, I would appreciate it a lot

    
asked by anonymous 24.08.2014 / 16:42

2 answers

3

Wellington,

If the project is still early, I strongly advise you to use Spring Boot with Spring Data JPA, it abstracts Spring's configuration a lot and it's very easy to configure Spring Data JPA, take a look at this link: link

If you are interested and have questions, I can help you.

Hugs

    
16.07.2015 / 04:09
0

Related to the JPA, are the "dependencies" (in Portuguese):

DEPENDENCY MANAGEMENT SYSTEM

 <dependencies> 
    <dependency> 
        <groupId> org.springframework.data </groupId> 
        <artifactId> spring-data-jpa </artifactId> 
        <version> 1.7.1.RELEASE </version> 
    </dependency> 
</dependencies>

Source: link

    
29.12.2014 / 19:43