Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate/Envers unable to read the mapped by attribute
    primarykey
    data
    text
    <p>I am unable to implement many-to-many relation using join table (<a href="https://stackoverflow.com/a/7603036">https://stackoverflow.com/a/7603036</a> &amp; <a href="https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/Java_Persistence/ManyToMany#Mapping_a_Join_Table_with_Additional_Columns</a>). When I try to reproduce it persistence unit is unable to build EntityManagerFactory. It seems that Envers fails to read the mapped by attribute, although it looks good to me. What am I doing wrong? What is the right way™ to implement many-to-many relation with extra columns (and Envers)?</p> <p><strong>Product.java</strong></p> <pre><code>@Entity @Audited public class Product extends UserModel { ... @OneToMany(mappedBy = "product", orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER) public List&lt;ProductForm&gt; forms = new ArrayList&lt;ProductForm&gt;(); ... } </code></pre> <p><strong>Form.java</strong></p> <pre><code>@Entity @Audited public class Form extends UserModel { ... @OneToMany(mappedBy = "form", fetch = FetchType.LAZY) public List&lt;ProductForm&gt; products = new ArrayList&lt;ProductForm&gt;(); ... } </code></pre> <p><strong>ProductForm.java</strong></p> <pre><code>@Entity @Table(name = "Product_Form") @IdClass(ProductForm.ProductFormId.class) public class ProductForm { @Id @JoinColumn(name = "product_id") @ManyToOne public Product product; @Id @JoinColumn(name = "form_id") @ManyToOne public Form form; public ProductForm(Product product, Form form) { this.product = product; this.form = form; } @Embeddable @SuppressWarnings("serial") public class ProductFormId implements Serializable { @Column(name = "product_id") public Long product; @Column(name = "form_id") public Long form; } } </code></pre> <p><strong>Stack trace</strong></p> <pre><code>play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:142) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at scala.Option.map(Option.scala:145) ~[scala-library.jar:na] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:104) ~[play_2.10-2.1.1.jar:2.1.1] at scala.util.Either$RightProjection.flatMap(Either.scala:523) [scala-library.jar:na] Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:930) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10-2.1.1.jar:2.1.1] Caused by: org.hibernate.MappingException: Unable to read the mapped by attribute for products in models.ProductForm! at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.getMappedBy(CollectionMetadataGenerator.java:642) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addOneToManyAttached(CollectionMetadataGenerator.java:187) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addCollection(CollectionMetadataGenerator.java:169) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValueInSecondPass(AuditMetadataGenerator.java:223) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:245) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:258) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] </code></pre> <hr> <p>PS I tried to add <code>@AuditMappedBy(mappedBy = "product")</code> to Product:</p> <pre><code>play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:142) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at scala.Option.map(Option.scala:145) ~[scala-library.jar:na] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:104) ~[play_2.10-2.1.1.jar:2.1.1] at scala.util.Either$RightProjection.flatMap(Either.scala:523) [scala-library.jar:na] Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:930) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10-2.1.1.jar:2.1.1] Caused by: org.hibernate.MappingException: @AuditMappedBy points to a property that doesn't exist: models.ProductForm.product at org.hibernate.envers.configuration.ClassesAuditingData.forcePropertyInsertable(ClassesAuditingData.java:84) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.ClassesAuditingData.updateCalculatedFields(ClassesAuditingData.java:70) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.EntitiesConfigurator.configure(EntitiesConfigurator.java:85) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.AuditConfiguration.&lt;init&gt;(AuditConfiguration.java:114) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.AuditConfiguration.getFor(AuditConfiguration.java:164) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:64) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] </code></pre> <hr> <p><strong>Edit</strong></p> <p>When I add <code>@Audited</code> to <code>ProductForm</code> is still get same error, below is the newest stack trace.</p> <pre><code>play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:142) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at scala.Option.map(Option.scala:145) ~[scala-library.jar:na] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:106) ~[play_2.10-2.1.1.jar:2.1.1] at play.core.ReloadableApplication$$anonfun$get$1.apply(ApplicationProvider.scala:104) ~[play_2.10-2.1.1.jar:2.1.1] at scala.util.Either$RightProjection.flatMap(Either.scala:523) [scala-library.jar:na] Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:930) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:904) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:72) ~[hibernate-entitymanager-4.2.0.Final.jar:4.2.0.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api-1.0.1.Final.jar:1.0.1.Final] at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10-2.1.1.jar:2.1.1] Caused by: org.hibernate.MappingException: Unable to read the mapped by attribute for products in models.ProductForm! at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.getMappedBy(CollectionMetadataGenerator.java:642) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addOneToManyAttached(CollectionMetadataGenerator.java:187) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.CollectionMetadataGenerator.addCollection(CollectionMetadataGenerator.java:169) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValueInSecondPass(AuditMetadataGenerator.java:223) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addValue(AuditMetadataGenerator.java:245) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] at org.hibernate.envers.configuration.metadata.AuditMetadataGenerator.addProperties(AuditMetadataGenerator.java:258) ~[hibernate-envers-4.2.0.Final.jar:4.2.0.Final] </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.
 

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