0

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.

Carlos Melo
  • 3,052
  • 3
  • 37
  • 45

1 Answers1

2
  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.
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • My bad. I meant one-to-many, not one-to-one. @ElementCollection does it at the background (DB level). I was expecting as a result something like the answer 4. Thanks. – Carlos Melo Sep 29 '11 at 04:02
  • Well, same thing goes for one-to-many. It's for building entity relationships, not mapping simple values. I'm glad you found what you were looking for, though. – Ryan Stewart Sep 29 '11 at 04:04