0

I've faced the problem, which is a little bit close to this issue, but when I've done all the steps, i still have such an exception:

org.codehaus.jackson.map.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0]->com.myPackage.SomeEntity["mainEntity"]->com.myPackage.MainEntity["subentity1"]->com.myPackage.Subentity1_$$_javassist_8["handler"])

here is code with my entities:

@JsonAutoDetect
public class MainEntity {

    private Subentity1 subentity1;

    private Subentity2 subentity2;

    @JsonProperty
    public Subentity1 getSubentity1() {
        return subentity1;
    }

    public void setSubentity1(Subentity1 subentity1) {
        this.subentity1 = subentity1;
    }

    @JsonProperty
    public Subentity2 getSubentity2() {
        return subentity2;
    }

    public void setSubentity2(Subentity2 subentity2) {
        this.subentity2 = subentity2;
    }
}



@Entity
@Table(name = "subentity1")
@JsonAutoDetect
public class Subentity1 {

    @Id
    @Column(name = "subentity1_id")
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 100)
    private String name;

    @JsonIgnore
    @OneToMany(mappedBy = "subentity1")
    private List<Subentity2> subentities2;

     @JsonProperty
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonProperty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //here I didin't add @JsonProperty, cause it leads to cycling during serialization
    public List<Subentity2> getSubentity2s() {
        return subentity2s;
    }

    public void setSubentity2s(List<Subentity2> subentity2s) {
        this.subentity2s = subentity2s;
    }

}

@Entity
@Table(name = "subentity2")
@JsonAutoDetect
public class Subentity2 {
    @Id
    @Column(name = "subentity2_id")
    @GeneratedValue
    private Long id;

    @Column(name = "name", length = 50)
    private String name;

    @ManyToOne
    @JoinColumn(name = "subentity1_id")
    private Subentity1 subentity1;

    @JsonProperty
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @JsonProperty
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty
    public Subentity1 getSubentity1() {
        return subentity1;
    }

    public void setSubentity1(Subentity1 subentity1) {
        this.subentity1 = subentity1;
    }

here is code of my method for transformation:

 private String toJSON(Object model) {
        ObjectMapper mapper = new ObjectMapper();
        String result = "";
        try {
            result = mapper.writeValueAsString(model);
        } catch (JsonGenerationException e) {
            LOG.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
        return result;
    }

I'll very grateful for any help, pieces of advice or code :)

UPD

alsp, I forgot to add piece of code from my controller:

String result = "";
        List<SomeEntity> entities = someEntityService.getAll();
        Hibernate.initialize(entities);
        for (SomeEntity someEntity : entities) {
            Hibernate.initialize(someEntity.mainEntity());
            Hibernate.initialize(someEntity.mainEntity().subentity1());
            Hibernate.initialize(someEntity.mainEntity().subentity2());
        }
        result = this.toJSON(entities);

I can't ignore any fields, cause I need them

3
  • Are you using Jackson Hibernate module? (github.com/FasterXML/jackson-module-hibernate)
    – StaxMan
    Commented Nov 20, 2012 at 1:10
  • nope, cause I'm using jackson 1.x version
    – John Smith
    Commented Nov 20, 2012 at 8:23
  • Ok. There was an earlier version of the module (that works with 1.9) as well (there's a branch, 1.9.0 version on Maven repo) for what that's worth.
    – StaxMan
    Commented Nov 21, 2012 at 5:55

3 Answers 3

1

Basically some of your fields are wrapped into lazy hibernate proxies. Call Hibernate.initialize(model) before serializing your object, it will load your lazy collections and references.

But I would not mix database and view models, this is a bad practice. Create set of classes for your restful model and convert database entities to them before serialization.

5
  • 1
    have you tried @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) advice from your previous question?
    – hoaz
    Commented Nov 19, 2012 at 16:22
  • the actual problem is that I need all these fields
    – John Smith
    Commented Nov 19, 2012 at 16:38
  • why do you need proxy fields?
    – hoaz
    Commented Nov 19, 2012 at 17:01
  • i think it's rather useless question :) i need them, they are part of main entity and I need them for operation on the client-side
    – John Smith
    Commented Nov 19, 2012 at 17:06
  • handler is not part of your model, it is a field added by Javassist proxy, you don't need it and can ignore
    – hoaz
    Commented Nov 19, 2012 at 18:23
0

I created a Bean with plane fields(String, Boolean, Double, etc.), which are in my classes and made method for transformation

0

If you are using lazy loading add this

@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})

Not the answer you're looking for? Browse other questions tagged or ask your own question.