Note that there are some explanatory texts on larger screens.

plurals
  1. POMapping a java.util.map<entity, entity> in hibernate using JPA annotations
    text
    copied!<p>I seem to be having an issue with mapping a collection in hibernate where the key and the resource are both entities to be mapped by hibernate.</p> <p>Whereas from the documentation (my main resource for this issue has been this, but I of course welcome any others: <a href="http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html" rel="nofollow">http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/collections.html</a>) it would appear that this should be a relatively simple task, I just can't seem to get the key of the map to persist.</p> <p>My code as it stands looks like this:</p> <p>Parent class:</p> <pre><code>@Entity public class Parent { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @MapKey @MapKeyClass(Key.class) @ManyToMany(cascade = CascadeType.ALL) private Map&lt;Key, Resource&gt; map; </code></pre> <p>Key class:</p> <pre><code>@Table(name="ParentKey") @Entity public class Key implements Comparable&lt;Key&gt; { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "hierarchyKey") private int key; </code></pre> <p>The resource class is a relatively simple POJO and persists fine, so I would assume that the issue is not in that class.</p> <p>I did come across the annotation @MapKeyManyToMany in some documentation on the hibernate forums, but I believe this has since been deprecated.</p> <p>The schema of the database is unimportant at this stage, the main concern is simply making sure everything persists to the database. We're using MySQL for the underlying database.</p> <p>As I'm sure you can probably guess, any search involving the words map, hibernate, entity and key return a large amount of questions not related to this issue, though I'm sure this must be a situation that occurs quite often in terms of mapping situations.</p> <p>I don't often submit questions, but I am an active reader, so please let me know if any more info is required.</p>
 

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