0

I have two entities Projet and FinancementExt this is the Projet:

@Entity public class Projet implements Serializable{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_projet;
@Column(name="ref",unique=true)
private String ref;
private String description;
@OneToMany(mappedBy="projet",fetch=FetchType.LAZY)
private Collection <FinancementExt> fe;
public void setFe(Collection <FinancementExt> fe)
{
    this.fe = fe;
}
public Collection<FinancementExt> getFe()
{
    return fe;
}

FinancementExt entity :

    @Entity public class FinancementExt implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id_fe;
private String ref_fe;
private double montant;
private double taux_change;
private String monnaie;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id_prj")
private Projet projet;
public long getid_prj()
{
    return projet.getId_projet();
}
public void setId_projet(long id_prj)
{
    projet.setId_projet(id_prj);
}
public Projet getProjet() {
    return projet;
}
public void setProjet(Projet projet) {
    this.projet = projet;
}

when I call the the restService listfe it starts an infinite loop this is the restservice code for listfe:

@RequestMapping(value="/listfe",method=RequestMethod.GET)
public Page<FinancementExt> listFE(int page, int size) {
    return FeMetier.listFE(page, size);
}

FeMetier :

public interface FeMetier {
public FinancementExt addFE(FinancementExt fe);
public void deleteFE(FinancementExt fe);
public FinancementExt editFE(FinancementExt fe);
public Page<FinancementExt> listFE(int page,int size);
public Page<FinancementExt> listFEByPrj(long id_prj,int page, int size);

}

FeMetierImp is the service :

@Service public class FeMetierImp implements FeMetier{
@Autowired
FinancementExtRepository FeRepository;
@Override
public FinancementExt addFE(FinancementExt fe) {
    return FeRepository.save(fe);   
}
@Override
public void deleteFE(FinancementExt fe) {
    FeRepository.delete(fe);
}
@Override
public FinancementExt editFE(FinancementExt fe) {
    return FeRepository.save(fe);
}
@Override
public Page<FinancementExt> listFE(int page,int size) {
    return FeRepository.findAll(new PageRequest(page, size));
}
@Override
public Page<FinancementExt> listFEByPrj(long id_prj, int page, int size) {
    return FeRepository.findAllByIdprojet(id_prj, new PageRequest(page, size));
}

}

this is the error I'm getting:

at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:690) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112) ~[jackson-databind-2.8.6.jar:2.8.6]
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25) ~[jackson-databind-2.8.6.jar:2.8.6]

this is the error I get in case I use @JsonIgnore:

2017-04-11 15:04:16.645  WARN 10852 --- [nio-8088-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: 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: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.projet.model.FinancementExt["projet"]->com.projet.model.Projet_$$_jvstbac_93["handler"]); 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: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.projet.model.FinancementExt["projet"]->com.projet.model.Projet_$$_jvstbac_93["handler"])
oui oui
  • 27
  • 7
  • please post the complete stacktrace – Jens Apr 11 '17 at 12:43
  • Also can you post the code for your service FeMetier – CrazyMac Apr 11 '17 at 12:48
  • 4
    Possible duplicate of [Infinite Recursion with Jackson JSON and Hibernate JPA issue](http://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue) – Nicolas Labrot Apr 11 '17 at 12:51
  • @CrazyMac I just edited the post check it – oui oui Apr 11 '17 at 12:57
  • 2
    I see more options: 1) The reference added by @NicolasLabrot - `@JsonIgnore`, `@JsonManagedReference`, `@JsonBackReference` - to fix serialization; 2) Depending on your app, you can use CQRS as a pattern, which adds additional complexity but simplifies complex scenarios, therefore is not for everybody - [link](https://martinfowler.com/bliki/CQRS.html) – andreim Apr 11 '17 at 12:58
  • yes I forgot to mention that I used @JsonIgnore in the Projet entity on top of getFe but when I call listfe I get an error – oui oui Apr 11 '17 at 13:05
  • Did you try @JsonBackReference ? – CrazyMac Apr 11 '17 at 13:08
  • yes I've tried it. wether I JsonIgnore or JsonBackReference I get the same error – oui oui Apr 11 '17 at 13:12
  • ok Which variable did you annotate with @JsonBackReference – CrazyMac Apr 11 '17 at 13:14
  • I used it in the projet entity `@JsonBackReference private Collection fe; public void setFe(Collection fe) { this.fe = fe; } //@JsonBackReference public Collection getFe() { return fe; }` – oui oui Apr 11 '17 at 15:51

0 Answers0