Spring Projects? How to Spring Data JPA Delete and Relationships?

Spring Projects? How to Spring Data JPA Delete and Relationships?

1. What Are Spring Projects?


Learn more about Spring Projects and how they differ from other Spring modules.

There are several important Spring Projects solving the needs of enterprises today. But first, it is essential to understand what are Spring Projects and how are they different from Spring Modules?

What You Will Learn

  • What are Spring Projects?
  • What are some examples of Spring Projects?
  • How are Spring Projects different from Spring Modules?

What Are Spring Projects?

Within the Spring Framework, there are a variety of different Spring modules — JDBC, AOP, Beans, and Context. All Spring Modules share the same release version as the Spring Framework. They are part of the same project.

Apart from the Spring Framework and its various modules, there are other frameworks called Spring Projects. These projects provide solutions to other issues faced by enterprise applications.

These projects are versioned differently from the Spring Framework. For example, the current version of the Spring Framework is 5.x.x and that of Spring Boot (one of the Spring Projects) is 2.x.x.

Spring Boot

Spring Boot is one of the most popular frameworks for developing microservices today. Spring Boot makes it easy to develop applications quickly. It has important features such as starter projects, auto-configuration, and actuator; it is a cakewalk to develop microservices.

Spring Cloud

The world is moving more and more towards the cloud. Everyone wants to deploy their application in the cloud. If you develop a microservice using Spring Boot, you could use Spring Cloud to make it cloud-enabled.

Spring Data

Spring Data provides mechanisms for consistent data access.

A few years earlier, there was only one kind of database that an application could connect to — the SQL-based relational databases. Today, we also have at our disposal a wide variety of databases including the NoSQL databases.

Spring Data ensures that the way we access data from all these sources remains consistent.

Spring Integration

Spring Integration, on the other hand, addresses the issues of application integration.

Spring Integration provides implementations for recommended architecture patterns in Enterprise Application Integration.

Spring Batch

Not all processing is done online, and a lot is also accomplished through batch applications.

Batch applications have their own unique set of requirements. For instance, it is important to be able to restart a batch job from the point where it had failed earlier. It may also be necessary to track down accurately what is happening in the background when a batch job executes.

Spring Batch provides a great option to develop batch applications.

Spring Security

Security is one of the most important non-functional requirements of an application’s development. Any application that you develop, be it a web application, a REST service, or any other, you want it to be secure.

Spring Security provides features for securing the applications that you develop. It has support for basic authentication, OAuth1, and OAuth2 authentication.

Spring HATEOAS

With RESTful services, it is not sufficient if you simply return the data for a resource. It is also recommended to return related actions you can perform on the resource. This is called HATEOAS. Spring HATEOAS enables you to develop HATEOAS compatible REST API.

Do check out our video on the same topic:

Summary

We have looked at seven of the available Spring projects. This is an evolving space and there are new Spring projects every year to solve emerging enterprise problems.


2. How to Spring Data JPA Delete and Relationships?

Overview

In this tutorial, we’ll have a look at how deleting is done in Spring Data JPA.

Sample Entity

As we know from the Spring Data JPA reference documentation, repository interfaces provide us some basic support for entities.

If we have an entity, like a Book:

@Entity
public class Book {
 
    @Id
    @GeneratedValue
    private Long id;
    private String title;
 
    // standard constructors
 
    // standard getters and setters
}

Then, we can extend Spring Data JPA’s CrudRepository to give us access to CRUD operations on Book:

@Repository
public interface BookRepository extends CrudRepository<Book, Long> {}

Delete from Repository

Among others, CrudRepository contains two methods: deleteById and deleteAll.

Let’s test these methods directly from our BookRepository:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class DeleteFromRepositoryUnitTest {
 
    @Autowired
    private BookRepository repository;
 
    Book book1;
    Book book2;
    List<Book> books;
 
    // data initialization
 
    @Test
    public void whenDeleteByIdFromRepository_thenDeletingShouldBeSuccessful() {
        repository.deleteById(book1.getId());
        assertThat(repository.count()).isEqualTo(1);
    }
 
    @Test
    public void whenDeleteAllFromRepository_thenRepositoryShouldBeEmpty() {
        repository.deleteAll();
        assertThat(repository.count()).isEqualTo(0);
    }
}

And even though we are using CrudRepository, note that these same methods exist for other Spring Data JPA interfaces like JpaRepository or PagingAndSortingRepository.

Derived Delete Query

We can also derive query methods for deleting entities. There is a set of rules for writing them, but let’s just focus on the simplest example.

A derived delete query must start with deleteBy, followed by the name of the selection criteria.These criteria must be provided in the method call.

Let’s say that we want to delete Books by title. Using the naming convention, we’d start with deleteBy and list title as our criteria:

@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
    long deleteByTitle(String title);
}

The return value, of type long, indicates how many records the method deleted.

Let’s write a test and make sure that is correct:

@Test
@Transactional
public void whenDeleteFromDerivedQuery_thenDeletingShouldBeSuccessful() {
    long deletedRecords = repository.deleteByTitle("The Hobbit");
    assertThat(deletedRecords).isEqualTo(1);
}

Persisting and deleting objects in JPA requires a transaction, that’s why we should use a@Transactional annotation when using these derived delete queries, to make sure a transaction is running. This is explained in detail in the ORM with Spring documentation.

Custom Delete Query

The method names for derived queries can get quite long, and they are limited to just a single table.

When we need something more complex, we can write a custom query using @Query and @Modifying together.

Let’s check the equivalent code for our derived method from earlier:

@Modifying
@Query("delete from Book b where b.title=:title")
void deleteBooks(@Param("title") String title);

Again, we can verify it works with a simple test:

@Test
@Transactional
public void whenDeleteFromCustomQuery_thenDeletingShouldBeSuccessful() {
    repository.deleteBooks("The Hobbit");
    assertThat(repository.count()).isEqualTo(1);
}

Both solutions presented above are similar and achieve the same result. However, they take a slightly different approach.

The @Query method creates a single JPQL query against the database. By comparison, thedeleteBy methods execute a read query, then delete each of the items one by one.

Delete in Relationships

Let’s see now what happens when we have relationships with other entities.

Assume we have a Category entity, that has a OneToMany association with the Book entity:

@Entity
public class Category {
 
    @Id
    @GeneratedValue
    private Long id;
    private String name;
 
    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Book> books;
 
    // standard constructors
 
    // standard getters and setters
}

The CategoryRepository can just be an empty interface that extends CrudRepository:

@Repository
public interface CategoryRepository extends CrudRepository<Category, Long> {}

We should also modify the Book entity to reflect this association:

@ManyToOne
private Category category;

Let’s now add two categories and associate them with the books we currently have. Now, if we try to delete the categories, the books will also be deleted:

@Test
public void whenDeletingCategories_thenBooksShouldAlsoBeDeleted() {
    categoryRepository.deleteAll();
    assertThat(bookRepository.count()).isEqualTo(0);
    assertThat(categoryRepository.count()).isEqualTo(0);
}

This is not bi-directional, though. That means that if we delete the books, the categories are still there:

@Test
public void whenDeletingBooks_thenCategoriesShouldAlsoBeDeleted() {
    bookRepository.deleteAll();
    assertThat(bookRepository.count()).isEqualTo(0);
    assertThat(categoryRepository.count()).isEqualTo(2);
}

We can change this behavior by changing the properties of the relationship, such as the CascadeType.

Conclusion

In this article, we looked at different ways to delete entities in Spring Data JPA. Thank for reading !

Originally published on https://www.baeldung.com

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Set up Web App with Spring Boot and Spring Security

Download the Spring Boot Web App Example Project. Run the Initial Web App. Add Project Dependencies for Your Spring Boot + Spring Security Web App. Understand Your Spring Boot App. Set Up Okta for OAuth 2.0 Single Sign-On. Configure Your Spring Boot App for Single SignOn (SSO) Refine Our Permissions

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.