如果我有一个包含集合字段的 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/