何时以及为何 JPA 实体应该实现 Serializable 接口? [英] When and why JPA entities should implement the Serializable interface?

查看:25
本文介绍了何时以及为何 JPA 实体应该实现 Serializable 接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题在标题中.下面我只是描述了我的一些想法和发现.

The question is in the title. Below I just described some of my thoughts and findings.

当我有一个非常简单的域模型(3 个没有任何关系的表)时,我所有的实体都没有实现 Serializable 接口.

When I had a very simple domain model (3 tables without any relations), all my entities did NOT implement the Serializable interface.

但是当域模型变得更加复杂时,我得到了一个 RuntimeException,说我的一个实体没有实现 Serializable.

But when the domain model became more complex, I got a RuntimeException, saying that one of my entities didn't implement Serializable.

我使用 Hibernate 作为 JPA 实现,我想知道:

I use Hibernate as a JPA implementation, and I wonder:

  1. 这是特定于供应商的要求/行为吗?
  2. 我的可序列化实体会发生什么?它们应该可序列化以进行存储还是传输?
  3. 什么时候需要使我的实体可序列化?

推荐答案

如果您混合使用 HQL 和本机 SQL 查询,通常会发生这种情况.在 HQL 中,Hibernate 将您传入的类型映射到 DB 理解的任何类型.当您运行本机 SQL 时,您必须自己进行映射.如果不这样做,那么默认映射是序列化参数并将其发送到数据库(希望它确实理解它).

This usually happens if you mix HQL and native SQL queries. In HQL, Hibernate maps the types you pass in to whatever the DB understands. When you run native SQL, then you must do the mapping yourself. If you don't, then the default mapping is to serialize the parameter and send it to the database (in the hope that it does understand it).

这篇关于何时以及为何 JPA 实体应该实现 Serializable 接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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