All Projects → spring-guides → Gs Accessing Data Jpa

spring-guides / Gs Accessing Data Jpa

Licence: apache-2.0
Accessing Data with JPA :: Learn how to work with JPA data persistence using Spring Data JPA.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Gs Accessing Data Jpa

Market
Simple web-market: Spring, JSP, REST, Hibernate (under modernization)
Stars: ✭ 47 (-68.03%)
Mutual labels:  spring-data-jpa
Jplusone
Tool for automatic detection and asserting "N+1 SELECT problem" occurences in JPA based Spring Boot Java applications and finding origin of JPA issued SQL statements in general
Stars: ✭ 91 (-38.1%)
Mutual labels:  spring-data-jpa
Springboot Restful Angular
springBoot,restful,jwt,angular4 搭建的前后端分离后台管理系统
Stars: ✭ 121 (-17.69%)
Mutual labels:  spring-data-jpa
Springboard
Spring Boot based production grade starter kit.
Stars: ✭ 59 (-59.86%)
Mutual labels:  spring-data-jpa
Springboot Projects Fullstack
Spring Boot, JDBC, ORM, JPA, Hibernate, H2, MySQL, Oracle
Stars: ✭ 76 (-48.3%)
Mutual labels:  spring-data-jpa
Spring Blog
Spring Boot base Blog
Stars: ✭ 109 (-25.85%)
Mutual labels:  spring-data-jpa
Springbootrestapihibernatejpamysqltutorial
Stars: ✭ 32 (-78.23%)
Mutual labels:  spring-data-jpa
Fenix
This is an extension library to the Spring Data JPA complex or dynamic SQL query. 这是一个比 MyBatis 更加强大的 Spring Data JPA 扩展库,为解决复杂动态 JPQL (或 SQL) 而生。https://blinkfox.github.io/fenix
Stars: ✭ 138 (-6.12%)
Mutual labels:  spring-data-jpa
Spring Boot Shopping Cart
Simple shopping cart web app made using Spring Boot + Thymeleaf
Stars: ✭ 85 (-42.18%)
Mutual labels:  spring-data-jpa
Spring Mvc Tutorial
Spring MVC 5 Tutorial - Guide to spring mvc framework
Stars: ✭ 121 (-17.69%)
Mutual labels:  spring-data-jpa
Rsql Jpa Specification
Java Library to Translate RSQL into Spring Data JPA Specification and QueryDSL Predicate
Stars: ✭ 62 (-57.82%)
Mutual labels:  spring-data-jpa
Spring Examples
SpringBoot Examples
Stars: ✭ 67 (-54.42%)
Mutual labels:  spring-data-jpa
Spring Cloud Flycloud
🔥🔥🔥FlyClould 微服务实战项目框架,在该框架中,包括了用 Spring Cloud 构建微服务的一系列基本组件和框架,对于后台服务框架的搭建有很大的参考价值,大家可以参考甚至稍加修改可以直接应用于自己的实际的项目开发中,该项目没有采用Maven进行项目构建,Maven通过xml进行依赖管理,导致整个配置文件太过臃肿,另外灵活性也不是很强,所以我采用Gradle进行项目构建和依赖管理,在FlyTour项目中我们见证了Gradle的强大,通过简单的一些配置就可以轻松的实现组件化的功能。该项目共有11个Module工程。其中10个位微服务工程,这10个微服务工程构成了一个完整的微服务系统,微服务系统包含了8个基础服务,提供了一整套微服务治理功能,他们分别是配置中心module_c…
Stars: ✭ 1,514 (+929.93%)
Mutual labels:  spring-data-jpa
Tianti
java轻量级的CMS解决方案-天梯。天梯是一个用java相关技术搭建的后台CMS解决方案,用户可以结合自身业务进行相应扩展,同时提供了针对dao、service等的代码生成工具。技术选型:Spring Data JPA、Hibernate、Shiro、 Spring MVC、Layer、Mysql等。
Stars: ✭ 1,053 (+616.33%)
Mutual labels:  spring-data-jpa
Spring Boot Blog
Simple blog web app made using Spring Boot + Thymeleaf
Stars: ✭ 121 (-17.69%)
Mutual labels:  spring-data-jpa
Springboot Project
这是一个用SpringBoot+JPA打造的基于微信服务号平台的点餐系统的后端,前端是一个使用Vue.js构建的运行于微信公众号的WebApp。前后端完全分离,前后端之间通过RESTful风格的接口相连。
Stars: ✭ 1,034 (+603.4%)
Mutual labels:  spring-data-jpa
Api
每天大红包 · 服务端
Stars: ✭ 103 (-29.93%)
Mutual labels:  spring-data-jpa
Javadevjournal
Source code for the tutorials published on the Javadevjournal site.
Stars: ✭ 141 (-4.08%)
Mutual labels:  spring-data-jpa
Registration Login Spring Xml Maven Jsp Mysql
Registration and Login Example with Spring MVC, Spring Security, Spring Data JPA, XML Configuration, Maven, JSP, and MySQL.
Stars: ✭ 134 (-8.84%)
Mutual labels:  spring-data-jpa
Cli Spring Boot Scaffold
command line for generate crud and configs for spring boot projects
Stars: ✭ 113 (-23.13%)
Mutual labels:  spring-data-jpa

:toc: :tabsize: 4 :icons: font :source-highlighter: prettify :project_id: gs-accessing-data-jpa

This guide walks you through the process of building an application that uses Spring Data JPA to store and retrieve data in a relational database.

== What You Will build

You will build an application that stores Customer POJOs (Plain Old Java Objects) in a memory-based database.

== What You need

:java_version: 1.8 include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/prereq_editor_jdk_buildtools.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/how_to_complete_this_guide.adoc[]

[[scratch]] == Starting with Spring Initializr

If you use Maven, visit the https://start.spring.io/#!type=maven-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=accessing-data-jpa&name=accessing-data-jpa&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.accessing-data-jpa&dependencies=data-jpa,h2[Spring Initializr] to generate a new project with the required dependencies (JPA and H2).

The following listing shows the pom.xml file created when you choose Maven:

==== [src,xml]

include::complete/pom.xml[]

====

If you use Gradle, visit the https://start.spring.io/#!type=gradle-project&language=java&platformVersion=2.4.3.RELEASE&packaging=jar&jvmVersion=1.8&groupId=com.example&artifactId=demo&name=demo&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.demo&dependencies=data-jpa,h2[Spring Initializr] to generate a new project with the required dependencies (JPA and H2).

The following listing shows the build.gradle file created when you choose Gradle:

==== [src,gradle]

include::complete/build.gradle[]

====

This project is configured to fit the examples in this tutorial.

=== Manual Initialization (optional)

If you want to initialize the project manually rather than use the links shown earlier, follow the steps given below:

. Navigate to https://start.spring.io. This service pulls in all the dependencies you need for an application and does most of the setup for you. . Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java. . Click Dependencies and select Spring Data JPA and then H2 Database. . Click Generate. . Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

NOTE: If your IDE has the Spring Initializr integration, you can complete this process from your IDE.

[[initial]] == Define a Simple Entity

In this example, you store Customer objects, each annotated as a JPA entity. The following listing shows the Customer class (in src/main/java/com/example/accessingdatajpa/Customer.java):

==== [source,java,tabsize=2,indent=0]

include::complete/src/main/java/com/example/accessingdatajpa/Customer.java[]

====

Here you have a Customer class with three attributes: id, firstName, and lastName. You also have two constructors. The default constructor exists only for the sake of JPA. You do not use it directly, so it is designated as protected. The other constructor is the one you use to create instances of Customer to be saved to the database.

The Customer class is annotated with @Entity, indicating that it is a JPA entity. (Because no @Table annotation exists, it is assumed that this entity is mapped to a table named Customer.)

The Customer object's id property is annotated with @Id so that JPA recognizes it as the object's ID. The id property is also annotated with @GeneratedValue to indicate that the ID should be generated automatically.

The other two properties, firstName and lastName, are left unannotated. It is assumed that they are mapped to columns that share the same names as the properties themselves.

The convenient toString() method print outs the customer's properties.

== Create Simple Queries

Spring Data JPA focuses on using JPA to store data in a relational database. Its most compelling feature is the ability to create repository implementations automatically, at runtime, from a repository interface.

To see how this works, create a repository interface that works with Customer entities as the following listing (in src/main/java/com/example/accessingdatajpa/CustomerRepository.java) shows:

==== [source,java,tabsize=2]

include::complete/src/main/java/com/example/accessingdatajpa/CustomerRepository.java[]

====

CustomerRepository extends the CrudRepository interface. The type of entity and ID that it works with, Customer and Long, are specified in the generic parameters on CrudRepository. By extending CrudRepository, CustomerRepository inherits several methods for working with Customer persistence, including methods for saving, deleting, and finding Customer entities.

Spring Data JPA also lets you define other query methods by declaring their method signature. For example, CustomerRepository includes the findByLastName() method.

In a typical Java application, you might expect to write a class that implements CustomerRepository. However, that is what makes Spring Data JPA so powerful: You need not write an implementation of the repository interface. Spring Data JPA creates an implementation when you run the application.

Now you can wire up this example and see what it looks like!

== Create an Application Class

Spring Initializr creates a simple class for the application. The following listing shows the class that Initializr created for this example (in src/main/java/com/example/accessingdatajpa/AccessingDataJpaApplication.java):

==== [source,java,tabsize=2]

include::initial/src/main/java/com/example/accessingdatajpa/AccessingDataJpaApplication.java[]

====

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/spring-boot-application-new-path.adoc[]

Now you need to modify the simple class that the Initializr created for you. To get output (to the console, in this example), you need to set up a logger. Then you need to set up some data and use it to generate output. The following listing shows the finished AccessingDataJpaApplication class (in src/main/java/com/example/accessingdatajpa/AccessingDataJpaApplication.java):

==== [source,java,tabsize=2]

include::complete/src/main/java/com/example/accessingdatajpa/AccessingDataJpaApplication.java[]

====

The AccessingDataJpaApplication class includes a demo() method that puts the CustomerRepository through a few tests. First, it fetches the CustomerRepository from the Spring application context. Then it saves a handful of Customer objects, demonstrating the save() method and setting up some data to work with. Next, it calls findAll() to fetch all Customer objects from the database. Then it calls findById() to fetch a single Customer by its ID. Finally, it calls findByLastName() to find all customers whose last name is "Bauer". The demo() method returns a CommandLineRunner bean that automatically runs the code when the application launches.

NOTE: By default, Spring Boot enables JPA repository support and looks in the package (and its subpackages) where @SpringBootApplication is located. If your configuration has JPA repository interface definitions located in a package that is not visible, you can point out alternate packages by using @EnableJpaRepositories and its type-safe basePackageClasses=MyRepository.class parameter.

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_mainhead.adoc[]

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/build_an_executable_jar_with_both.adoc[]

When you run your application, you should see output similar to the following:

====

== Customers found with findAll(): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=2, firstName='Chloe', lastName='O'Brian'] Customer[id=3, firstName='Kim', lastName='Bauer'] Customer[id=4, firstName='David', lastName='Palmer'] Customer[id=5, firstName='Michelle', lastName='Dessler']

== Customer found with findById(1L): Customer[id=1, firstName='Jack', lastName='Bauer']

== Customer found with findByLastName('Bauer'): Customer[id=1, firstName='Jack', lastName='Bauer'] Customer[id=3, firstName='Kim', lastName='Bauer']

====

== Summary

Congratulations! You have written a simple application that uses Spring Data JPA to save objects to and fetch them from a database, all without writing a concrete repository implementation.

NOTE: If you want to expose JPA repositories with a hypermedia-based RESTful front end with little effort, you might want to read link:/guides/gs/accessing-data-rest[Accessing JPA Data with REST].

== See Also

The following guides may also be helpful:

include::https://raw.githubusercontent.com/spring-guides/getting-started-macros/master/footer.adoc[]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].