-1

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!

Amir Pashazadeh
  • 7,170
  • 3
  • 39
  • 69
vmfesta
  • 159
  • 1
  • 1
  • 9
  • Are you using `Spring Data JPA`? You are finding the entity by id, so where are the finders with constraint you mentioned? And what do you mean by working and not working? And are you just exposing the result of DAO directly as a REST web-service? – Amir Pashazadeh Sep 16 '17 at 18:39
  • Yes, i'm using spring. Get all products showing their relation is working. Get all products not showing their relations, is not working. "You are finding the entity by id, so where are the finders with constraint you mentioned?" don't understand your point – vmfesta Sep 16 '17 at 18:40

1 Answers1

0

You need to use Jackson Hibernate datatype integration which takes care of handling lazy loaded relations issues which typically come with Jackson usage with Hibernate entities. You can take a look here at different answers for how to configure in different spring environment.

Shailendra
  • 8,874
  • 2
  • 28
  • 37