1

I have two entities. Address and Employee. When I try to persist using CommandLineRunner everything is working as expected but when I try the same from REST endpoint, the data is saved in DB but I am getting the following error

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)address"]

CommandLineRunner (This works fine)

@SpringBootApplication
@RequiredArgsConstructor
public class OneToOneApplication  implements CommandLineRunner {

    private final EmployeeRepository employeeRepository;

    public static void main(String[] args) {
        SpringApplication.run(OneToOneApplication.class, args);
    }

    @Override
    @Transactional
    public void run(String... args) throws Exception {

        Employee employee = new Employee();
        employee.setAge(10);
        employee.setName("SomeName");

        Address address = new Address();
        address.setEmployee(employee);
        address.setStreet("SomeStreet");

        employee.setAddress(address);
        employeeRepository.save(employee);

    }
}

This is not working. POST http://localhost:9191/save

{
    "name": "somename",
    "age": 15,
    "address": {
        "street": "somestreet"
    }
}

Address.java

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Address")
public class Address {

    @Id
    @Column(name = "employee_id")
    private Long id;

    private String street;

    @MapsId
    @OneToOne
    @JoinColumn (name = "employee_id")
    private Employee employee;
}

Employee.java

@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Employee")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;
    private String name;
    private int age;

    @OneToOne(mappedBy = "employee", cascade = CascadeType.ALL)
    private Address address;
}

EmployeeRepository.java

@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

EmployeeService.java

@Service
public class EmployeeService {
    @Autowired
    EmployeeRepository employeeRepository;

    public Employee saveEmployee(Employee employee) {

        Address address = employee.getAddress();
        address.setEmployee(employee);
        return employeeRepository.save(employee);

    }
}

I guess I am doing some mistake in EmployeeService.java.

  1. How to make it work?
  2. Do I really need AddressRepository?
Thiagarajan Ramanathan
  • 1,035
  • 5
  • 24
  • 32
  • Does this answer your question? [Infinite Recursion with Jackson JSON and Hibernate JPA issue](https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – xerx593 Mar 22 '23 at 20:26
  • 1. "Hack" with `@Json[Back]Reference`, better: Don't use entities, use dto (layer)! 2. If you don't need it, why should you!? – xerx593 Mar 22 '23 at 20:31
  • But the (inf-rec) problem would also arise with (mis-modeled) "pojo"/dto s, when they are *bi-directionally associated*.. – xerx593 Mar 22 '23 at 20:38

0 Answers0