Reputation: 13
This is my entity class
@Entity
@Data
@Table(name = "wishlist")
public class Wishlist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@OneToOne(targetEntity = Customer.class, fetch = FetchType.EAGER)
@JoinColumn(nullable = false,name = "customer_id")
private Customer customer;
@Column(name = "created_date")
private Date createdDate;
@ManyToOne
@JoinColumn(name = "product_id")
private Products product;
public Wishlist(Customer customer, Products product) {
this.customer = customer;
this.createdDate = new Date();
this.product = product;
}
public Wishlist() {
}
}
This my repository
public interface WishlistRepository extends JpaRepository<Wishlist, Integer> {
List<Wishlist> findAllByCustomerIdOrderByCreatedDateDesc(Integer customerId);
Optional<Wishlist> findByProductId(Integer productId);
}
This my service layer
@Service
@Transactional
public class WishlistServiceImpl implements WishlistService{
@Autowired
WishlistRepository wishlistRepo;
@Override
public void createWishlist(Wishlist wishlist,Integer productId) throws ProductNotExistsException {
Optional<Wishlist> optionalProduct = wishlistRepo.findByProductId(productId);
System.out.println(optionalProduct)
}
}
So I am trying to check if the product is present on my wishlist or not using product id.
But getting the error:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at java.base/java.lang.AbstractStringBuilder.putStringAt(AbstractStringBuilder.java:1720) ~[na:na]
at java.base/java.lang.AbstractStringBuilder.putStringAt(AbstractStringBuilder.java:1724) ~[na:na]
at java.base/java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:583) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:175) ~[na:na]
at java.base/java.lang.StringBuilder.append(StringBuilder.java:87) ~[na:na]
at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:112) ~[na:na]
at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:127) ~[na:na]
at com.ecommerce.entity.Customer.toString(Customer.java:29) ~[classes/:na]
So how to solve this issue?
Upvotes: 0
Views: 741
Reputation: 24
You have a circular dependency between product and wishlist.
@Data bundles the features of @ToString , @EqualsAndHashCode , @Getter / @Setter and @RequiredArgsConstructor together.
You can either exclude fields from hashcode and equals with @EqualsAndHashCode.Exclude (See more information in enter link description here or you can replace @Data with @Getter and @Setter.
Upvotes: 1
Reputation: 583
might be the issue with the cyclic dependencies.
try adding the following annotation:
@ToString.Exclude
or try to override the toString implementation.
Upvotes: 0