Over a million developers have joined DZone.

Spring Data JPA and Database Relationships

· Database Zone

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

How to implement database relationship using spring data JPA?

There are a lot of real world objects which impact a business case. Off those a few of them impact a specific use case of a business. Once we identify the real world objects that impact the specific business use case, then a database model is created. To be more precise an entity relationship diagram is made from that information. There would be a lot of relationship amongst those business objects or entities to be more precise, which have to be implemented in the software. 

A relationship works by matching data in key columns — usually columns with the same name in both tables. In most cases, the relationship matches the primary key from one table, which provides a unique identifier for each row, with an entry in the foreign key in the other table.

Let’s say we are working for a telecommunication vendor, who wants to track his customers who have been shifting from one location to another and asking for a new connection, every time they move to a new location. So in this case a customer will be associated with an account. This account is tied to a specific location, which has the zip code. This seems to be like one customer can have only one account at a given location. If the same customer is allowed by business to have multiple accounts at a location, then it means that one customer can have multiple accounts at a location. This is an example of One to many relationship. Similarly that location can have multiple accounts. This leads to a many to one relationship in between account and location. Since the same customer is tied to the location so it is a one to one relationship in between them. There are three types of relationships between tables. The type of relationship that is created depends on how the related columns are defined.

1.  One-to-Many Relationship

A one-to-many relationship is the most common type of relationship. In this type of relationship, a row in table A can have many matching rows in table B, but a row in table B can have only one matching row in table A. In the above example, a customer can have many accounts for a given location. So it’s one to many relationships.

2.  Many-to-Many Relationships

In a many-to-many relationship, a row in table A can have many matching rows in table B, and vice versa. In the above example one customer can have many accounts at one location. So there can be many to many relationships in between accounts and customer, provided new customer id is given for a new account and is not tied to the same customer id. One can also maintain such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B. This might not be really required.

3.  One-to-One Relationships

In a one-to-one relationship, a row in table A can have no more than one matching row in table B, and vice versa. A one-to-one relationship is created if both of the related columns are primary keys or have unique constraints. So in the above example one customer can be tied to one location.

There are multiple ways of solving this in java space. Following are the technologies:

1.  Plain JDBC using a JDBC driver.

2.  Spring Data JPA using Hibernate.

3.  Spring JDBC which is essentially plain JDBC in itself.

4.  Giving the objects to RDBMS with data to persist it. Essentially it’s RDBMS layer that can take care of the persistence.

In this article I would be talking about how to persist this using spring data.


Step 1: Use tools like STS, which has built in provision to create an entity objects.


Then you would see 


Enter “JPA Project” to create a JPA Project. Here you can enter the value as per your understanding.


Click next unless otherwise you want to do some configuration.


Try to add a connection a new connection.


Pick the database type.


Enter the connection details.



Upon completion of this, Spring data JPA will create the java files for you which are required for the execution of the project. These java files will have the entity and the column mappings. Please make sure that you have the @Id mapping, or else you will have hibernate exception.

Now, in order to implement the one to many relationships, make sure that you have the line defined in the customer class.

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true,mappedBy="account")

Here the @OneToMany relationship makes sure that the one customer has many account. Now in the account class make sure that you have:

  @ManyToOne(cascade=CascadeType.ALL)

  @JoinColumn(name=" ACCOUNT_ID",nullable=false)

  private Customer customer;

This ensures that the many to one relationship is established.

If you are creating a new child record for an existing parent, it’s essential to associate the child with the parent. One can create a record in child table by giving a parent id. But Spring Data JPA asks for the parent record, so as to be able to create the child.

Transactions and Spring Data JPA

It’s quite essential to understand how transactions and spring data JPA work together. Transactions have the following propagation levels:

·  MANDATORY

Support a current transaction; throw an exception if none exists.

·  NESTED

Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else.

·  NEVER

Execute non-transactionally, throw an exception if a transaction exists.

·  NOT_SUPPORTED

Execute non-transactionally, suspend the current transaction if one exists.

·  REQUIRED

Support a current transaction, create a new one if none exists.

·  REQUIRES_NEW

Create a new transaction, and suspend the current transaction if one exists.

·  SUPPORTS

Support a current transaction, execute non-transactionally if none exists.

Transactions have the following isolation levels:

·  DEFAULT

Use the default isolation level of the underlying datastore.

·  READ_COMMITTED

A constant indicating that dirty reads are prevented; non-repeatable reads and phantom reads can occur.

·  READ_UNCOMMITTED

A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur.

·  REPEATABLE_READ

A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur.

·  SERIALIZABLE

A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.

 Now when you are wanting to call the transactions, please make sure that the isolation levels which has been used are correct. Also, make sure that the propagation level over the method is done correctly.

How to achieve the batch insert using spring data JPA?

The JpaRepository has methods which have the provision to accept a collection of objects. This collection can be used to persist the records. Please do not assume that this is a batch insert. No it’s not. The <S extends T> List<S> save(Iterable<S> entities) actually iterates through the collection and saves record one by one.

Conclusion:


After going through this article, one can replicate the Entity relationship diagram into Spring Data JPA entities and appropriately use the transactions to save them into database. By using spring data JPA, one leaves the responsibility of database with Spring JPA API. This way, one can focus on the business functionality more than on technical complexities of the database relationships.

Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:
java ,opinion ,theory ,big data

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}