One-to-many mapping inside component mapping creates a many-to-many table
Description
Environment
Activity
Roger December 23, 2015 at 11:31 AM
Your issue is not related to having a component or not. If the collection is bidirectional, normally you would map the collection with "inverse=true". If you do, no joined table will be created for the audited collection.
Willem Burgers November 20, 2015 at 8:07 AMEdited
I'm sorry, yes I mean nhibernate, not hibernate. I work in both Java and .Net these days, but in this case, it is the .Net version.
Sorry for the confusion.
The attributes are custom indeed, but I thought it would provide a mapping example.
I forgot to mention in the example that PhoneNumber has a many-to-one relation back to Person.
Therefore it is bi-directional.
Again, when I place the PhoneNumber list in the Person Class, the link table is not created, but when put the list in the ContactInformation (mapped as a component) class, the link table is created.
Here is a sample nhibernate mapping:
<?xml version="1.0" encoding="utf-8"?>
<!--Generated from NHibernate.Mapping.Attributes on 2015-11-20 08:49:27Z.-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Models.DataModels.ContactInformation.PhoneNumber, MyProj" table="PhoneNumber" schema="PersonRegistration">
<id name="Id" type="System.Int64">
<generator class="native" />
</id>
<property name="Nummer" />
<property name="NummerSoort" />
<many-to-one name="Person" />
</class>
<class name="Models.DataModels.Person, MyProj" table="Person" schema="PersonRegistration">
<id name="Id" type="System.Int64">
<generator class="native" />
</id>
<component name="ContactInformation">
<bag name="PhoneNumbers" table="PhoneNumber" lazy="false" cascade="all-delete-orphan">
<key column="Person" />
<one-to-many class="Models.DataModels.ContactInformation.PhoneNumber, MyProj" />
</bag>
</component>
</class>
</hibernate-mapping>
Roger November 19, 2015 at 4:43 PM
You write hibernate but I guess we're talking nhibernate (.net) here and not Java version - right?
Don't know what the attributes you have in your snippet means but... In a unidirectional one-to-many relationship, a link table will be created for the audited entities.
If you think you have encountered a bug, please include minimal mapping to reproduce the issue here (or even better - create a pull request).
If you have user related questions, post a question on SO or nhibernate user mailing list.
I have a bag one-to-many collection inside of a component.
[Class] public class Person() { [ComponentProperty] ContactInformation ci; } [Component] public class ContactInformation { [Bag] public IList<PhoneNumber> phonenumbers; }
This collection is correctly mapped by hibernate, but envers creates a many-to-many link table next to the one-to-many table.
When I move the collection up to the entity class instead of the component, the many-to-many link table is not created.
Why is the link table created when the collection is inside of a component object?
The link table should not be necessary, as it is not necessary when the collection is in the entity. The component mapping should make no difference right?