2012-11-19 61 views
0

期間,我所面臨的問題,這是一點點接近this issue,但是當我已經做了所有的步驟,我仍然有這樣的例外:傑克遜例外系列化

org.codehaus。 jackson.map.JsonMappingException:未找到 類org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer和 沒有屬性串行發現創建BeanSerializer(避免異常, 禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))(通過 參考鏈: java.util.ArrayList [0] - > com.myPackage.SomeEntity [「mainEntity」] - > com.myPackage.MainEntity [「sub使用實體 「] - > com.myPackage.Subentity1 _ $$ _ javassist_8 [」 處理程序「])

這裏是代碼我的實體:

@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; 
    } 

這裏是我的方法的代碼轉換:

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; 
    } 

我會爲任何幫助非常感謝,建議或代碼:)件

UPD

ALSP,我忘了從我的控制器添加一段代碼:

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); 

我不能忽視任何領域,因爲我需要他們

+0

你在使用Jackson Hibernate模塊嗎? (https://github.com/FasterXML/jackson-module-hibernate) – StaxMan

+0

nope,因爲我使用的是jackson 1.x版本 –

+0

好的。有一個早期版本的模塊(與1.9一起工作)(有一個分支,Maven回購1.9.0版本),這是值得的。 – StaxMan

回答

0

我創建了一個Bean與平面字段(字符串,布爾值,雙擊等),這是在我的課,並進行改造

1

基本上你的一些字段被包裝成懶休眠代理。 在序列化對象之前調用Hibernate.initialize(model),它會加載您的懶惰集合和引用。

但我不會混合的數據庫和視圖模型,這是一個不好的做法。爲您的寧靜模型創建一組類,並在序列化之前將數據庫實體轉換爲它們。

+0

看到更新的問題 –

+1

你嘗試過@JsonIgnoreProperties({「hibernateLazyInitializer」,「處理程序」})從你以前的問題的意見? – hoaz

+0

實際的問題是我需要所有這些字段 –

0

取得方法如果使用的是延遲加載添加此

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