Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate/JPA: map<Object, Enum> manytomany is it possible?
    primarykey
    data
    text
    <p>More specifically <code>(map&lt;Course, Role&gt;)((User) u).courses</code></p> <p>I have many <code>Course</code>s and many <code>User</code>s who have <code>Role</code>s in courses. A user might be staff in one course, and a student in another. </p> <p>I have User entities with ID and course entities with ID, and a Role Enum type </p> <p>I want a map belonging to user in order to query his role in a particular class:</p> <pre><code>public enum Role{ STUDENT, STAFF } public class User ... { ... @ElementCollection(targetClass=Role.class) @CollectionTable(name="COURSE_ROLE") @MapKeyJoinColumn(name="COURSE_ID") @Column(name="ROLE_NAME") @Enumerated(EnumType.STRING) private Map&lt;Course, Role&gt; courses; ... </code></pre> <p>I have looked at several resources. ProJPA has the following:</p> <pre><code>@Entity public class Department { @Id private int id; private String name; // ... @ElementCollection @CollectionTable(name="EMP_SENIORITY") @MapKeyJoinColumn(name="EMP_ID") @Column(name="SENIORITY") private Map&lt;Employee, Integer&gt; seniorities; // ... </code></pre> <p>if we consider a department as my user, and employee as my course this almost works, but I don't know how to handle the transition from their Integer to my Enum. </p> <p>Please help me with the semantics of this table definition with annotations.</p> <p>edit: To clarify, I had tried without the argument to ElementCollection(Role.class), and many other permutations of options but I don't feel I am grasping the situation. Currently, with this annotation combination I get an exception:</p> <pre><code>org.hibernate.MappingException: Could not determine type for: java.util.Map, at table: USER, for columns: [org.hibernate.mapping.Column(ROLE_NAME)] </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload