PageRenderTime 23ms CodeModel.GetById 15ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 1<?xml version='1.0' encoding='UTF-8'?>
 2
 3<!--
 4  ~ Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 5  ~
 6  ~ Licensed under the Apache License, Version 2.0 (the "License");
 7  ~ you may not use this file except in compliance with the License.
 8  ~ You may obtain a copy of the License at
 9  ~
10  ~ http://www.apache.org/licenses/LICENSE-2.0
11  ~
12  ~ Unless required by applicable law or agreed to in writing, software
13  ~ distributed under the License is distributed on an "AS IS" BASIS,
14  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ~ See the License for the specific language governing permissions and
16  ~ limitations under the License.
17  -->
18
19<sect1 xml:id="SpringJPA" version="5.0" xmlns="http://docbook.org/ns/docbook"
20       xmlns:xi="http://www.w3.org/2001/XInclude"
21       xmlns:xlink="http://www.w3.org/1999/xlink"
22       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23       xsi:schemaLocation="http://docbook.org/ns/docbook http://www.docbook.org/xml/5.0/xsd/docbook.xsd
24       http://www.w3.org/1999/xlink http://www.w3.org/1999/xlink.xsd">
25    <title>Spring Data - JPA</title>
26    <para>
27        Hazelcast supports JPA persistence integrated with <link xlink:href="http://www.springsource.org/spring-data/jpa">
28        Spring Data-JPA</link> module.
29        Your POJOs are mapped and persisted to your relational database.
30        To use JPA persistence first you should create a Repository interface extending CrudRepository class with object type that you want to persist..
31        <programlisting language="java"><![CDATA[
32package com.hazelcast.jpa.repository;
33
34import com.hazelcast.jpa.Product;
35import org.springframework.data.repository.CrudRepository;
36
37public interface ProductRepository extends CrudRepository<Product, Long> {
38
39}
40]]></programlisting>
41        Then you should add your data source and repository definition to you Spring configuration,
42        <programlisting language="xml"><![CDATA[
43<jpa:repositories
44       base-package="com.hazelcast.jpa.repository" />
45
46    <bean class="com.hazelcast.jpa.SpringJPAMapStore" id="jpamapstore">
47        <property name="crudRepository" ref="productRepository" />
48    </bean>
49
50    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
51        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
52             <property name="url" value="jdbc:mysql://localhost:3306/YOUR_DB"/>
53             <property name="username" value="YOUR_USERNAME"/>
54             <property name="password" value="YOUR_PASSWORD"/>
55    </bean>
56
57    <bean id="entityManagerFactory"
58      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
59      <property name="dataSource" ref="dataSource" />
60      <property name="jpaVendorAdapter">
61        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
62          <property name="generateDdl" value="true" />
63          <property name="database" value="MYSQL" />
64        </bean>
65      </property>
66        <property name="persistenceUnitName" value="jpa.sample" />
67    </bean>
68
69    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
70      id="transactionManager">
71      <property name="entityManagerFactory"
72          ref="entityManagerFactory" />
73      <property name="jpaDialect">
74        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
75      </property>
76    </bean>
77    ]]></programlisting>
78    In the example configuration above, Hibernate and MYSQL is configured,
79        you change them according your ORM and database selection.
80        Also you should define your persistence unit with persistence.xml under META-INF directory.
81
82        <programlisting language="xml"><![CDATA[
83<?xml version="1.0" encoding="UTF-8"?>
84<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">
85	<persistence-unit name="jpa.sample" />
86</persistence>
87]]></programlisting>
88
89        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.
90
91    <emphasis role="italic">For more info see
92        <link xlink:href="http://static.springsource.org/spring-data/data-jpa/docs/current/reference/html/">
93            Spring Data JPA Reference
94        </link>.</emphasis>
95    </para>
96</sect1>