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

查看:204
本文介绍了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将您传入的类型映射到数据库可以理解的任何类型.运行本机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天全站免登陆