I'm working on a Spring Boot project using JpaRepository. When I try to read records from controller, it gives me weird huge JSON response.
I am sure this is caused by mappings.
Here is my code:
Entities: Customer
com.arj.entity.Customer
@Entity
@Table(name = "customers")
public class Customer {
@SuppressWarnings("unused")
private static final long serialVersionUID = 4910225916550731446L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "name", length = 256)
private String name;
@Column(name = "email", length = 256)
private String email;
@Column(name = "phone_number", length = 256)
private String phoneNumber;
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private Set<CustomerAddress> customerAddresses = new HashSet<CustomerAddress>();
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
private List<Order> orders;
@Column(name = "date_created")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "is_active", columnDefinition = "BIT default 1", length = 1, nullable = false)
private boolean isActive;
getters and setters ...
}
Entities: Dealer
com.arj.entity.Dealer
@Entity
@Table(name = "dealers")
public class Dealer {
@SuppressWarnings("unused")
private static final long serialVersionUID = 4910225916550731446L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "name", length = 256)
private String name;
@Column(name = "phone_number", length = 20)
private String phoneNumber;
@OneToMany(mappedBy = "dealer", cascade = CascadeType.ALL)
private Set<Order> orders = new HashSet<Order>();
@Column(name = "date_created")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "is_active", columnDefinition = "BIT default 1", length = 1, nullable = false)
private boolean isActive;
getters and setters ...
}
Entities: Order
com.arj.entity.Order
@Entity
@Table(name = "orders")
public class Order {
@SuppressWarnings("unused")
private static final long serialVersionUID = 4910225916550731446L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
@Column(name = "date_created")
@Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
@Column(name = "is_active", columnDefinition = "BIT default 1", length = 1, nullable = false)
private boolean isActive;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private Customer customer;
@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinColumn(name = "dealer_id", nullable = true)
private Dealer dealer;
getters and setters ...
}
OrderRepository
com.arj.repository.OrderRepository
@RepositoryRestResource
public interface OrderRepository extends JpaRepository<Order, Long> {
@Query(value = "SELECT t FROM Order t WHERE t.status = :status")
public List<Order> getAllOrdersByStatus(@Param("status") String status);
}
OrderController
com.arj.controller.api.OrderController
@RestController
@RequestMapping("/api/v1/order")
public class OrderController {
@Autowired
OrderRepository orderRepository;
@RequestMapping(value = "/type/{status}", method = RequestMethod.GET)
public ResponseEntity<List<Order>> getAllOrdersByStatus(@PathVariable("status") String status) {
List<Order> orders = orderRepository.getAllOrdersByStatus(status);
if (orders.isEmpty()) {
System.out.println("Orders empty");
return new ResponseEntity<List<Order>>(HttpStatus.NO_CONTENT);
}
return new ResponseEntity<List<Order>>(orders, HttpStatus.OK);
}
}
I am getting following message:
"status":200,"error":"OK","message":"Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.aata.entity.Order[\"customer\"]->com.aata.entity.Customer_$$_jvsteb5_c[\"orders\"]->org.hibernate.collection.internal.PersistentBag[0]->com.aata.entity.Order[\"customer\"]->com.aata.entity.Customer_$$_jvsteb5_c[\"orders\"
Customer have OneToMany
for Order
Order have ManyToOne
for customer
If I pick customer
, I want to pick orders
as well
If I pick order
, I want to show customer
as well