问题
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:
- "one-to-many" is a type of entity association. This is a collection of values, so it can't be a one-to-many.
- 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.
- Essentially what you're asking for is a basic property, like
private String permissions;
. That would use a single column in the same table. - 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