jpa - JPA 是否为数据库中为空的集合属性返回 "null"或空集合?

标签 jpa collections null language-lawyer

如果我有一个包含集合字段的 JPA 实体,例如

public class Post {    
  // [...]
  @OneToMany
  private List<String> tags;

相应的数据库模式通常包含一个单独的表来存储标签,其中每条记录包含一个标签和对“Post”表的外键引用。

现在,当我从没有标签的数据库中加载“Post”实体(即不存在相应的“标签”记录)时,Post.tags 的值是多少?它会被设置为 null ,还是一个空的 List ?或者这是未定义的?

我在 JPA 规范中找不到这个。

注意:这是与 Does Hibernate return null or empty collection if table in relation is empty? 相同的问题,仅针对 JPA 规范而不是专门针对 Hibernate。

我知道大多数 JPA 实现(至少 Hibernate 和 EclipseLink)将返回一个空集合 - 我对是否在任何地方指定这很感兴趣。

最佳答案

根据 JPA 2.1 spec :

If there are no associated entities for a multi-valued relationship of an entity fetched from the database, the persistence provider is responsible for returning an empty collection as the value of the relationship.

关于jpa - JPA 是否为数据库中为空的集合属性返回 "null"或空集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50860058/

相关文章:

hibernate - 我应该使用 Kotlin 数据类作为 JPA 实体吗?

java - 在流上使用 Collections.toMap() 时如何保持 List 的迭代顺序?

java - 如何在初始化时定义 map 内容?

java - 如何通过检查元素的值从 ArrayList 中删除元素?

ios - 购买应用程序后,应用程序在启动时崩溃。产品标识符=无?

sql-server - 案例 null SQL Server 2008

c# - ASP.NET WebAPI 2 : How to pass empty string as parameter in URI

Spring JPA事务在@Transactional注解的方法结束之前提交

java - Spring in Action 第 6 章,项目未启动

java - 当我进行联接时,为什么必须指定实际的列名称而不是字段名称?