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

给你一囗甜甜゛ 提交于 2019-12-12 02:40:10

问题


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

Thanks.


回答1:


  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.


来源:https://stackoverflow.com/questions/7592242/does-jpa-elementcollection-annotation-always-produce-an-one-to-many-relationshi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!