I have a entity called Product
that has a foreign key for the same entity, to indicate child products.
I need to select all the products in two different cases
First, get all products with relation (Working) Second, get all products without relations (Not working)
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String description;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="product_id")
private Product parentProduct;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Product getParentProduct() {
return parentProduct;
}
public void setParentProduct(Product parent_product_id) {
this.parentProduct = parent_product_id;
}
}
When i try
public Product getProductByIdWithoutRelation(Integer id) {
return productRepository.findById(id);
}
With following method in repository code
Product findById(Integer id);
I got this error
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: model.Product["parentProduct"]->model.Product_$$_jvst726_0["handler"])
Thanks for the help!