/hazelcast-documentation/src/main/docbook/manual/content/spring/SpringJPA.xml
https://bitbucket.org/gabral6_gmailcom/hazelcast · XML · 96 lines · 69 code · 12 blank · 15 comment · 0 complexity · cc772efc50dbebfde6ac18cf9458d110 MD5 · raw file
- <?xml version='1.0' encoding='UTF-8'?>
- <!--
- ~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
- ~
- ~ Licensed under the Apache License, Version 2.0 (the "License");
- ~ you may not use this file except in compliance with the License.
- ~ You may obtain a copy of the License at
- ~
- ~ http://www.apache.org/licenses/LICENSE-2.0
- ~
- ~ Unless required by applicable law or agreed to in writing, software
- ~ distributed under the License is distributed on an "AS IS" BASIS,
- ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ~ See the License for the specific language governing permissions and
- ~ limitations under the License.
- -->
- <sect1 xml:id="SpringJPA" version="5.0" xmlns="http://docbook.org/ns/docbook"
- xmlns:xi="http://www.w3.org/2001/XInclude"
- xmlns:xlink="http://www.w3.org/1999/xlink"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
- http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd">
- <title>Spring Data - JPA</title>
- <para>
- Hazelcast supports JPA persistence integrated with <link xlink:href="http://www.springsource.org/spring-data/jpa">
- Spring Data-JPA</link> module.
- Your POJOs are mapped and persisted to your relational database.
- To use JPA persistence first you should create a Repository interface extending CrudRepository class with object type that you want to persist..
- <programlisting language="java"><![CDATA[
- package com.hazelcast.jpa.repository;
- import com.hazelcast.jpa.Product;
- import org.springframework.data.repository.CrudRepository;
- public interface ProductRepository extends CrudRepository<Product, Long> {
- }
- ]]></programlisting>
- Then you should add your data source and repository definition to you Spring configuration,
- <programlisting language="xml"><![CDATA[
- <jpa:repositories
- base-package="com.hazelcast.jpa.repository" />
- <bean class="com.hazelcast.jpa.SpringJPAMapStore" id="jpamapstore">
- <property name="crudRepository" ref="productRepository" />
- </bean>
- <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://localhost:3306/YOUR_DB"/>
- <property name="username" value="YOUR_USERNAME"/>
- <property name="password" value="YOUR_PASSWORD"/>
- </bean>
- <bean id="entityManagerFactory"
- class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="jpaVendorAdapter">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
- <property name="generateDdl" value="true" />
- <property name="database" value="MYSQL" />
- </bean>
- </property>
- <property name="persistenceUnitName" value="jpa.sample" />
- </bean>
- <bean class="org.springframework.orm.jpa.JpaTransactionManager"
- id="transactionManager">
- <property name="entityManagerFactory"
- ref="entityManagerFactory" />
- <property name="jpaDialect">
- <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
- </property>
- </bean>
- ]]></programlisting>
- In the example configuration above, Hibernate and MYSQL is configured,
- you change them according your ORM and database selection.
- Also you should define your persistence unit with persistence.xml under META-INF directory.
- <programlisting language="xml"><![CDATA[
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
- <persistence-unit name="jpa.sample" />
- </persistence>
- ]]></programlisting>
- By default, the key is expected to be the same with id of the JPA object. You can change this behaviour and customize MapStore implementation extending SpringJPAMapStore class.
- <emphasis role="italic">For more info see
- <link xlink:href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/">
- Spring Data JPA Reference
- </link>.</emphasis>
- </para>
- </sect1>