1

I am using Hibernate 3.6.7 (Core) and I am using XML Configuration.
I have One Company Master and its Branch Master table. The relation is 1 to many in it. My requirement is when I am inserting the data to Company Master I am getting list of Branches too.
In configuration file, I have defined it

<set name="branches" table="m_company_branch" 
                 inverse="true" lazy="true" fetch="select" cascade="all">
            <key>
                <column name="company_id" not-null="true" />
            </key>
            <one-to-many class="com.my.CompanyBranchStructure" />
        </set>

When I am inserting to the table, it only inserts to primary table i.e m_company but not committed. However, I have passed the list of branches too. and hence I am getting an error "Batch update returns....." since it is not getting the company_id (foreign key of branch).

After reading some contents on Internet, I got to know that I need to do separate Insert/Commit transaction for the purpose.
But then I am unable to understand the mapping in configuration file.
As it treats each table (record) separately, I can do the same without using one-to-many or many-to-many relations too.

Kindly guide.

Naved
  • 5,633
  • 4
  • 28
  • 46
  • I tried, adding cascade="all" attribute too, but still there is the same result. – Naved Aug 08 '15 at 04:40
  • Use annotation...xml configuration is histroy. – Darshan Patel Aug 08 '15 at 06:00
  • @DarshanPatel what is the advantage of using annotation over xml configuration in terms of Performance. If you know the same thing to be done in annotation, please share I'll search for its xml corresponding settings. – Naved Aug 12 '15 at 07:49
  • Refer [hibernate-annotation-or-xml-configuration?](http://stackoverflow.com/questions/7147018/hibernate-annotation-or-xml-configuration) – Darshan Patel Aug 12 '15 at 10:00

1 Answers1

1

I am not sure whether it could be workable for you or not, but in my case I could be able to do the same by using following;

In Primary table (i.e. Company in your case)

<set name="branches" lazy="false" fetch="select" optimistic-lock="false" cascade="all" >
        <key column="company_id" not-null="true" update="false"/>
        <one-to-many class="com.my.CompanyBranchStructure"  />
        </set>

and in Secondary table (i.e. CompanyBranchStructure);

<property name="companyId" insert="false" update="false">
            <column name="company_id" />
        </property>

This was working in my case. I hope this will resolve your problem too.

NavSoft
  • 732
  • 5
  • 9