将JSON对象映射到Hibernate实体 [英] Mapping JSON object to Hibernate entity

查看:326
本文介绍了将JSON对象映射到Hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将开始一个使用Spring和Hibernate管理的REST应用程序的项目.

I'm going to start a project of a REST application managed with Spring and with Hibernate for my model.

我知道Spring允许您从HTTP请求(带有@Consumes(JSON)批注)中获取Java对象.如果此Java对象也是Hibernate实体,是否有冲突?嵌套对象是否起作用(如@ManyToOne关系)?

I know that Spring allows you to get Java object from the HTTP Request (with @Consumes(JSON) annotation). Is there any conflict if this Java object is also a Hibernate entities? And is nested object working (like @ManyToOne relation)?

推荐答案

我们正在使用这种方法来简化设计并摆脱许多dto(我们滥用了很多dto).基本上,它对我们有用.

We were using such approach to simplify design and get rid of many dtos (we were abusing them too much). Basically, it worked for us.

但是,在我们的REST模型中,我们试图不公开对象的其他关系,因为您总是可以创建另一个REST资源来访问它们.

However, in our REST model we were trying to do not expose other relations for an object as you can always create another REST resources to access them.

因此,我们只是将@JsonIgnore批注放置到诸如@OneToMany@ManyToOne的关系映射中,以使其成为瞬态.

So we just put @JsonIgnore annotations to relations mappings like @OneToMany or @ManyToOnemaking them transient.

我看到的另一个问题是,如果您仍然想返回这些关系,则必须对它们使用Join.FETCH策略,或者将事务管理上移,以便在将响应序列化为JSON(在View Pattern中打开会话)时,事务仍然存在. ). 我认为这两种解决方案都不太好.

Another problem I see that if you still like to return these relations you would have to use Join.FETCH strategy for them or move transaction management higher so that transaction still exists when a response is serialized to JSON (Open Session In View Pattern). On my opinion these two solutions are not so good.

这篇关于将JSON对象映射到Hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆