Doubt how to configure Spring Data with Hibernate

Asked

Viewed 1,104 times

7

How to set up Spring Data + Hibernate

Hello everyone,

I’m new here in stackoverflow and also in java web development. I am etudando some things of Spring Frameork and I am with a doubt of how I can configure Spring Data in my Dynamic Project.

Below follows my acquisitions of configuration and architecture. If you could help me, I’d really appreciate it.

Architecture

inserir a descrição da imagem aqui

persistence.xml

<persistence-unit name="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 to 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 could help me out and give me a tip I’d really appreciate it

  • 2

    Are you having any specific question/error?

  • I recommend you take a look at this link for being a new user: http://answall.com/tour

  • Did you find a solution? Poste as an answer to help other people.

2 answers

3

Wellington,

If the project is still early, I strongly advise you to use Spring Boot with Spring Data JPA, it abstracts a lot the settings of Spring and it is very easy to configure Spring Data JPA, take a look at this link: https://spring.io/guides/gs/accessing-data-jpa/

If you are interested and have doubts I can help you.

Hugs

0

Related to 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: http://projects.spring.io/spring-data-jpa/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.