DEV Community

Cover image for Spring Boot One-To-One Mapping Demo With H2 database
Atharva Siddhabhatti
Atharva Siddhabhatti

Posted on

6 1

Spring Boot One-To-One Mapping Demo With H2 database

There are three basic entity relationships:-

  • One-to-One
  • One-to-Many/Many-to-one
  • Many-to-Many

Installation

Clone project from here:- Click Here
Import Maven based project in any of your Favourite IDE.

./mvnw spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Output

Open in Browser

Access H2 Database.

http://localhost:8080/h2-console
Enter fullscreen mode Exit fullscreen mode

Usage

MainClass.java

run() method is run when the application is started. The data is added in the database.

    @Override
    public void run(String... args) throws Exception {
        Customer customer = new Customer();
        customer.setName("Atharva Siddhabhatti");
        customer.setEmail("atharvasiddhabhatti@gmail.com");
        Item item = new Item();
        item.setName("Macbook");
        item.setQty(1);
        customer.setItem(item);
        item.setCustomer(customer);
        customerRepository.save(customer);

Enter fullscreen mode Exit fullscreen mode

Customer.java

Following annotations are used in the Customer Entity class to join two tables.Check the class file to view other entities.

@OneToOne(fetch =FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "item_id")
    private Item item;
Enter fullscreen mode Exit fullscreen mode

Item.java

@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "item")
    private Customer customer;
Enter fullscreen mode Exit fullscreen mode

Output

Output1
Output2

Configuration

application.properties

spring.jpa.show-sql = true

# Enabling H2 Console
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true
spring.h2.console.enabled=true

# Enable Web Access
spring.h2.console.settings.web-allow-others=true
Enter fullscreen mode Exit fullscreen mode

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Quadratic

Python + AI + Spreadsheet

Chat with your data and get insights in seconds with the all-in-one spreadsheet that connects to your data, supports code natively, and has built-in AI.

Try Quadratic free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay