简体   繁体   English

JPA @ElementCollection注释是否总是产生一对多关系?

[英]Does JPA @ElementCollection annotation always produce an one-to-many relationship?

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
}

Assuming that the enum values are stored in the ordinal form, does JPA always create an extra table for this set? 假设枚举值以序数形式存储,JPA是否总是为此集合创建一个额外的表? Can I alter this in order to make this not to be an one-to-many relationship, ie, using a column instead of an extra table? 我是否可以更改它以使其不成为一对多关系,即使用列而不是额外的表?

Thanks. 谢谢。

  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; 本质上,您要的是基本属性,例如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. 如果要将多个值打包到一个值中,例如当Hibernate保存它时将所有权限手动组合成一个逗号分隔的字符串,则需要编写一个自定义UserType来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM