有没有办法让 jackson jsonize 只获取延迟加载的对象
我知道很多地方肯定都提到了这一点。但我找不到以简单方式覆盖它的地方。
在控制器中,当我返回人员对象时,杰克逊尝试序列化案例。我不需要将案例表发送到这里。我可以做 person.setCase(null) 但我认为必须有更好的方法来做到这一点。有没有办法告诉杰克逊如果尚未获取案例就不要序列化案例?
我有一堆像 hbopojogen 创建的具有一对多关系的 Pojo,
@OneToMany( fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "person" )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false )
@JsonManagedReference
public Set<Case> getCase() {
return this.case;
}
@ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name = "PERSON_ID", nullable = true )
@JsonBackReference
public Person getPerson() {
return this.person;
}
public @ResponseBody Person searchPersonByCode(@RequestParam String codeString){
int code = Integer.parseInt(codeString);
Patient person = this.personDao.getByCode(code );
return person;
}
谢谢,
I know this must have been covered on many places. But I can't find a place where it is covered in a simple way.
In the controller when I return the person object Jackson tries to serialize case. I don't need case table to be sent here. I could do person.setCase(null) but I think there must be a better way of doing this. Is there a way to tell Jackson not to serialize case if it hasn't been fetched?
I have a bunch of Pojos with one-to-many relations like these created by hbopojogen
@OneToMany( fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "person" )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@Column( nullable = false )
@JsonManagedReference
public Set<Case> getCase() {
return this.case;
}
@ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY )
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@JoinColumn(name = "PERSON_ID", nullable = true )
@JsonBackReference
public Person getPerson() {
return this.person;
}
public @ResponseBody Person searchPersonByCode(@RequestParam String codeString){
int code = Integer.parseInt(codeString);
Patient person = this.personDao.getByCode(code );
return person;
}
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否检查过 Jackson Hibernate 模块?核心 jackson 不能对 lib/framework 特定的功能做太多事情,但扩展模块可以——而且这个模块特别支持不解析惰性引用的概念。
Have you checked out Jackson Hibernate module? Core jackson can not do much about lib/framework-specific features, but extension modules can -- and this one specifically supports concept of not resolving lazy references.
我不会直接回答你的问题。相反,我会尝试说服您改变您认为可以/应该完成的方式。
您的问题是您希望防止在未显式获取案例时对其进行序列化。我猜测当 Jackson 序列化你的对象时你会得到 LazyInitializationException 。不要试图避免这个问题,请尝试认为以下两个解决方案之一不是更好:
解决方案 1:设置“在视图中打开会话”。杰克逊将永远能够连载“案件”。
解决方案2:使用@JsonIgnore注释“case”
无论您做什么,都尽量保持一致,不要试图让您的应用程序在特殊情况下以不同的方式工作。它将给您的设计带来不明显性,并使您的应用程序更难以维护。
干杯
I won't answer your question directly. Instead I will try to convince you to change the way you think it may/should be done.
Your problem is that you want to prevent case being serialized when it was not fetched explicitly. I am guessing that you are getting LazyInitializationException when Jackson serializes your object. Instead of trying to avoid this problem please try to think is one of below two solutions isn't better:
Solution 1: Make "open session in view". Jackson will be able to serialize "case" always.
Solution 2: Annotate "case" with @JsonIgnore
Whatever you do try to be consistent and don't try to make your application work differently in special occasions. It will introduce nonobviousness into your design and make your application harder to maintain.
Cheers