'java.lang.StackOverflowError: null [Spring Boot, Hibernate]

I have two classes User.java and Address.java and there is a one-to-one bi-directional mapping between them.

But when I try to get the address using the User class I get an "java.lang.StackOverflowError: null" exception.

The same thing happens when I try to get the User from the Address class.

User.java

@Entity
@Table(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    private String name;
    private String email;
    private String phone;
    private String password;
    private String imageUrl;
    
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "address")
    private Address address;

Address.java

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "address")
    private User user;
    
    private String country;
    private String state;
    private String city;
    private String street;
    private String pincode;

MainController.java

@Controller
public class MainController {
    @Autowired
    private UserDao userDao;
    
    @Autowired
    private AddressDao addressDao;
    
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        User user = new User();
        user.setName("name");
        user.setEmail("email");
        user.setPhone("phone");
        user.setPassword("password");
        user.setImageUrl("imageUrl");
        
        Address address = new Address();
        address.setCountry("country");
        address.setState("state");
        address.setCity("city");
        address.setStreet("street");
        address.setPincode("123456");
        
        user.setAddress(address);
        userDao.save(user);
        
        return "working";
    }
    
    @RequestMapping("/fetch")
    @ResponseBody
    public String fetch() {
        User user = userDao.getById((long) 1);
        System.out.println(user.getAddress());
        
        return "working";
    }
}

I am using the test() function to put data in the database and it is working fine. database image

But when I call the fetch() function I am getting the following error

java.lang.StackOverflowError: null
    at org.hibernate.proxy.pojo.BasicLazyInitializer.invoke(BasicLazyInitializer.java:58) ~[hibernate-core-5.6.5.Final.jar:5.6.5.Final]
    at org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor.intercept(ByteBuddyInterceptor.java:43) ~[hibernate-core-5.6.5.Final.jar:5.6.5.Final]
    at 

Updated MainController.java

package com.demo.controller;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.demo.dao.AddressDao;
import com.demo.dao.UserDao;
import com.demo.entity.Address;
import com.demo.entity.User;

@Controller
public class MainController {
    @Autowired
    private UserDao userDao;
    
    @Autowired
    private AddressDao addressDao;
    
    @RequestMapping("/test")
    @ResponseBody
    public String test() {
        User user = new User();
        user.setName("name");
        user.setEmail("email");
        user.setPhone("phone");
        user.setPassword("password");
        user.setImageUrl("imageUrl");
        
        userDao.save(user);
        
        Address address = new Address();
        address.setCountry("country");
        address.setState("state");
        address.setCity("city");
        address.setStreet("street");
        address.setPincode("123456");
        
        addressDao.save(address);
        
        user.setAddress(address);
        userDao.save(user);
        
        return "working";
    }
    
    @RequestMapping("/fetch")
    @ResponseBody
    public String fetch() {
        Optional<User> op = userDao.findById((long) 1);
        User user = op.get();
        
        // working
        System.out.println(user.getName() + " " + user.getEmail() + " " + user.getPhone());
    
        // java.lang.StackOverflowError:null
        System.out.println(user.getAddress());
        
        return "working";
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source