nhibernate / nhibernate-core

NHibernate Object Relational Mapper
https://nhibernate.info
GNU Lesser General Public License v2.1
2.12k stars 924 forks source link

Error in join tables #2400

Open galvin1234 opened 4 years ago

galvin1234 commented 4 years ago

I am getting an error when i try to retrieve all records from Cont Entity. here is a valid mapping and entities.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="DEO" namespace="DEO" xmlns="urn:nhibernate-mapping-2.2">
  <class name="Cont" table="CONT" dynamic-insert="true" dynamic-update="true" select-before-update="true" batch-size="1000" lazy="false">
    <cache usage="nonstrict-read-write" region="cont" />
    <id name="Id" type="String">
      <column name="CONTID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      <generator class="assigned" />
    </id>
    <property name="Name" type="String">
      <column name="CONTNAME" not-null="false" length="255" sql-type="VARCHAR2(255 CHAR)" />
    </property>
    <bag name="Digi_I" inverse="true" access="field.pascalcase-underscore" generic="true">
      <cache usage="nonstrict-read-write" region="digi_is" />
      <key>
        <column name="CONTID" />
      </key>
      <one-to-many class="Digi_I" />
    </bag>
  </class>

  <class name="Digi_I" table="DIGI_I" dynamic-insert="true" dynamic-update="true" select-before-update="true" lazy="false">
    <cache usage="nonstrict-read-write" region="digi_i" />
    <id name="Id" type="String">
      <column name="ID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      <generator class="assigned" />
    </id>
    <property name="Comment" type="String">
      <column name="REMARK" not-null="false" length="2000" sql-type="VARCHAR2(2000 CHAR)" />
    </property>
    <join table="DIGI_I_EXT">
      <key>
        <column name="ID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      </key>
      <property name="Name" type="String">
        <column name="NAME" not-null="false" />
      </property>
      <many-to-one name="Cont" class="Cont" access="field.pascalcase-underscore" not-found="ignore">
        <column name="CONTID" not-null="false" />
      </many-to-one>
    </join>
  </class>
</hibernate-mapping>

What would be the right way to achieve above mapping. all mappings are correct but NH throws an error when i try to get all records from cont entity. Join is not getting applied properly with expected tables.

I will be helpful if NH codes modified properly to handle this kind is mappings. because mapping is correct there is no issue at all. but it throws an error because join not applied properly.

Else would like to know the right way to do that.

testmapping.hbm.txt

galvin1234 commented 4 years ago

EntityA is pointing to TableA. EntityB is pointing to TableB EnittyB is having join table to TableC.

TableA of EntityA is trying to join with TableC of EntityB which is joined with TableB or EntityB.

EntityA is having a bag collection which is having one-to-many join/relationship with TableC of EntityB.

TableA of EnittyA and TableC of EntityB contains join column, TableB doesn't contain column for joining with TableA or EntityA.

TableB or EntityB is base whereas TableC is extended table of TableB.

When I try to get all from EntityA then it throws an error with joining the tables, because joins are not getting generated properly.

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="DEO" namespace="DEO" xmlns="urn:nhibernate-mapping-2.2">
  <class name="EntityA" table="TableA" dynamic-insert="true" dynamic-update="true" select-before-update="true" batch-size="1000" lazy="false">
    <id name="Id" type="String">
      <column name="CONTID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      <generator class="assigned" />
    </id>
    <property name="Name" type="String">
      <column name="CONTNAME" not-null="false" length="255" sql-type="VARCHAR2(255 CHAR)" />
    </property>
    <bag name="EntityAttr" inverse="true" generic="true">
      <key>
        <column name="CONTID" />
      </key>
      <one-to-many class="EntityB" />
    </bag>
  </class>

  <class name="EntityB" table="TableB" dynamic-insert="true" dynamic-update="true" select-before-update="true" lazy="false">
    <id name="Id" type="String">
      <column name="ID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      <generator class="assigned" />
    </id>
    <property name="Comment" type="String">
      <column name="REMARK" not-null="false" length="2000" sql-type="VARCHAR2(2000 CHAR)" />
    </property>
    <join table="TableC">
      <key>
        <column name="ID" not-null="true" length="40" sql-type="VARCHAR2(40 CHAR)" />
      </key>
      <property name="Name" type="String">
        <column name="NAME" not-null="false" />
      </property>
      <many-to-one name="EntityAttr" class="EntityA" not-found="ignore">
        <column name="CONTID" not-null="false" />
      </many-to-one>
    </join>
  </class>
</hibernate-mapping>

testmapping1.hbm.txt

galvin1234 commented 4 years ago

Any Acknowledgement on this? Or required more information on this ?