JPA @ElementCollection注释是否总是产生一对多关系? [英] Does JPA @ElementCollection annotation always produce an one-to-many relationship?

查看:232
本文介绍了JPA @ElementCollection注释是否总是产生一对多关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于此问题,请考虑以下示例:

For this question, consider the following sample:

@Entity
public class File {
    public static enum Permission { READABLE, WRITEABLE, EXECUTABLE }

    @ElementCollection
    @Enumerated(EnumType.ORDINAL)
    Set<Permission> permissions;

    // Omitted
}

假设枚举值以序数形式存储,那么JPA是否总是为此集创建一个额外的表?我可以更改它以使其不成为一对多关系,即使用列而不是额外的表吗?

Assuming that the enum values are stored in the ordinal form, does JPA always create an extra table for this set? Can I alter this in order to make this not to be an one-to-many relationship, i.e., using a column instead of an extra table?

谢谢.

推荐答案

  1. 一对多"是一种实体关联.这是值的集合,因此不能一对多.
  2. 从物理上讲,不可能在单个行的单个字段中存储多个值,所以不能,您不能让它这样做.
  3. 基本上,您要的是基本属性,例如private String permissions;.那将使用同一表中的一列.
  4. 如果您想将多个值打包为一个值,例如在Hibernate保存时将所有权限手动组合成一个逗号分隔的字符串,则需要编写
  1. "one-to-many" is a type of entity association. This is a collection of values, so it can't be a one-to-many.
  2. It's physically impossible to store multiple values in a single field in a single row, so no, you can't make it do that.
  3. Essentially what you're asking for is a basic property, like private String permissions;. That would use a single column in the same table.
  4. If you're wanting to pack multiple values into a single value, like manually combining all the permissions into a comma-delimited string when Hibernate saves it, you'll want to write a custom UserType to do that.

这篇关于JPA @ElementCollection注释是否总是产生一对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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