何时以及为什么JPA实体应该实现Serializable接口?

编程入门 行业动态 更新时间:2024-10-19 13:27:04
本文介绍了何时以及为什么JPA实体应该实现Serializable接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 问题在标题中。下面我刚描述了一些我的想法和发现。

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

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

我使用Hibernate作为JPA实现。

我想知道:

  • 它是供应商特定的需求/行为?
  • 我的序列化实体会发生什么?它们应该是可序列化的用于存储还是传输?
  • 在哪一刻,有必要让我的实体可序列化?
  • 解决方案

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

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

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

    But when domain model became more complex I got RuntimeException which said that one of my entities didn't implement Serializable.

    I use Hibernate as a JPA implementation.

    I wonder:

  • Is it vendor-specific requirement/behavior?
  • What happens with my serializable entities? Should they be serializable for storing or for transferring?
  • At which moment it becomes necessary to make my entity serializable?
  • 解决方案

    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接口?

    本文发布于:2023-06-07 15:20:04,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:实体   接口   JPA   Serializable

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!