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