简体   繁体   English

是否可以使用 hibernate 实体类作为 GSON 的 POJO?

[英]Is it possible to use hibernate entity classes as POJOs for GSON?

I'm working on a project where we have a spring boot application that uses spring data and hibernate.我正在开发一个项目,我们有一个 spring 数据和 hibernate 启动应用程序。 Now I want to use the GSON library to work with JSON files.现在我想使用 GSON 库来处理 JSON 文件。 I've read a tutorial where it becomes clear that it is possible to create POJO classes and convert JSON files into objects from those classes.我读过一个教程,很明显可以创建 POJO 类并将 JSON 文件转换为这些类中的对象。 The same thing happens with hibernate. hibernate 也会发生同样的事情。

Now my question: is it possible to design the POJO (or entity) classes in a way that they work both for hibernate and GSON.现在我的问题是:是否可以设计 POJO(或实体)类,使其同时适用于 hibernate 和 GSON。 Can problems arise, if it is possible and I do it it that way?是否会出现问题,如果可能的话,我会这样做吗?

Thanks in advance!提前致谢!

Edit: Here is the tutorial where I read about POJOs for GSON: tutorials point - GSON编辑:这是我阅读有关 GSON 的 POJO 的教程: 教程点 - GSON

It is possible, but it is not a good design.这是可能的,但这不是一个好的设计。 If you use it for example serialize rest interface data, then it will hard couple your rest endpoint with database or it can lead for security issues (serialize sensitive data) etc. So it can lead hard coupling, and will be harder to decouple it latter.如果您使用它,例如序列化 rest 接口数据,那么它将您的 rest 端点与数据库硬耦合,或者它可能导致安全问题(序列化敏感数据)等。因此它可能导致硬耦合,并且以后将难以解耦它. It is always good to create separate model to db and other interfaces.为 db 和其他接口创建单独的 model 总是好的。 You can use mapping libraries (for example mapstuct) to mapping between the models easily.您可以使用映射库(例如 mapstuct)轻松地在模型之间进行映射。

You actually should not design Entity or POJO class based on which library we use ( GSON or Jackson API).您实际上不应该根据我们使用的库(GSON 或 Jackson API)来设计实体或 POJO class。

You can refer here for a clear explanation.您可以参考此处以获得清晰的解释。 https://www.geeksforgeeks.org/convert-java-object-to-json-string-using-jackson-api/ https://www.geeksforgeeks.org/convert-java-object-to-json-string-using-jackson-api/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用Hibernate工具在没有EJB的情况下使用Hibernate注释生成POJO? - Is it possible to use Hibernate Tools to generate POJOs with Hibernate Annotations without EJB? 从SQL Server生成存储过程到IntelIJ中的Hibernate POJO类 - generate stored procedure from SQL server to Hibernate POJOs classes in IntelIJ 是否可以在同一方法中创建带有 AND 而不带实体类的查询? (JPA + 休眠) - Is it possible to create queries with AND without entity classes in the same method? (JPA + Hibernate) 是否可以在没有服务器的情况下使用实体类和EntityManager? - Is it possible to use entity classes and EntityManager without a server? 如何在Gson中使用泛型类 - How to use generic classes with Gson 如何在没有实体类的情况下使用Hibernate Criteria API? - How to use Hibernate Criteria API without Entity classes? Hibernate使用Equals生成POJO - Hibernate generate POJOs with Equals 休眠生成的POJO - Hibernate generated POJOs Maven 2中的Pojos,过滤类 - Pojos in Maven 2, filtering classes 同时使用Simple XML和Gson库的REST客户端:使用常见的POJO或适配器模式? - REST client using both Simple XML and Gson libraries: Use common POJOs or adapter pattern?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM