2014-05-05 95 views
2

使用Spring,3.2.7,Hibernate 4.2.12,Jackson 2.3.3 並使用JsonIdentityInfo在傑克遜中試驗引用。傑克遜的JsonIdentityInfo生成純數字

我有一個實體taxanomy,與父母和子女,都提到了taxanomy自我。

@Entity 
@Table(name="taxanomy") 
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 
public class Taxanomy implements Serializable { 
... 

父:

@ManyToOne(fetch=FetchType.EAGER) 
@Cascade({org.hibernate.annotations.CascadeType.ALL}) 
@JoinColumn(name="parent_id", nullable=true) 
public Taxanomy getParent() { 

... 

孩子們:

@OneToMany(mappedBy="parent", fetch=FetchType.EAGER) 
@Cascade({org.hibernate.annotations.CascadeType.ALL}) 
public Set<Taxanomy> getChildren() { 

我檢索所有Taxanomy對象,用的findAll方法。這將返回所有對象樹。根對象通過子關係包含所有其他的taxanomy對象。我想要列表中的所有對象,但在客戶端,返回的列表包含2個根對象和其餘數字(ids)。

taxanomy = []; 
function getTaxanomy() { 
    $.ajax({ url: 'taxanomy/all' 
    , dataType : 'json'}).done(function(data) { 
     taxanomy = data; 
    }); 
} 


>> taxanomy 
>> [>Object, 1001, >Object, 1005, 1005, 1003, 1007, 1006, 1002] 
      ^   ^ ^ ^ ^ ^ ^
       |______________|_____|_____|_____|_____|_____|_ want real objects? 

我如何在客戶端強制生成所有對象而不是ID引用?我知道我可以循環(或遞歸)樹和對象與ID映射

回答

1

嘗試註釋:

@JsonTypeInfo(use = JsonTypeInfo.Id.NONE) 
class YourClassName{ 
    .... 
}