-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate many to many to many relationship mapping issue
PostPosted: Tue Aug 18, 2015 2:10 am 
Newbie

Joined: Tue Aug 18, 2015 1:22 am
Posts: 1
I am facing an issue with Hibernate mappings for many to many and many to many relationships.
My Entities are : 1) Services 2) ServiceProvider 3) Territory
A service provider can offer multiple services and can belong to multiple territories.
I am using xml based configuration.

Please read below error which i am getting :-

Code:
INFO 2015-08-18 11:08:14,159 [localhost-startStop-1] (SettingsFactory.java:276) (buildSettings) - Optimize cache for minimal puts: disabled
INFO 2015-08-18 11:08:14,160 [localhost-startStop-1] (SettingsFactory.java:285) (buildSettings) - Structured second-level cache entries: disabled
INFO 2015-08-18 11:08:14,166 [localhost-startStop-1] (SettingsFactory.java:305) (buildSettings) - Echoing all SQL to stdout
INFO 2015-08-18 11:08:14,167 [localhost-startStop-1] (SettingsFactory.java:314) (buildSettings) - Statistics: disabled
INFO 2015-08-18 11:08:14,167 [localhost-startStop-1] (SettingsFactory.java:318) (buildSettings) - Deleted entity synthetic identifier rollback: disabled
INFO 2015-08-18 11:08:14,168 [localhost-startStop-1] (SettingsFactory.java:333) (buildSettings) - Default entity-mode: pojo
INFO 2015-08-18 11:08:14,168 [localhost-startStop-1] (SettingsFactory.java:337) (buildSettings) - Named query checking : enabled
INFO 2015-08-18 11:08:14,224 [localhost-startStop-1] (SessionFactoryImpl.java:187) (<init>) - building session factory
INFO 2015-08-18 11:08:14,539 [localhost-startStop-1] (SessionFactoryObjectFactory.java:105) (addInstance) - Not binding factory to JNDI, no JNDI name configured
INFO 2015-08-18 11:08:14,549 [localhost-startStop-1] (SchemaExport.java:226) (execute) - Running hbm2ddl schema export
INFO 2015-08-18 11:08:14,550 [localhost-startStop-1] (SchemaExport.java:251) (execute) - exporting generated schema to database
[b]ERROR 2015-08-18 11:08:15,690 [localhost-startStop-1] (SchemaExport.java:348) (create) - Unsuccessful: alter table mystore.service_serviceprovider add index FKB9D75A1C5090FC26 (serviceproviderid), add constraint FKB9D75A1C5090FC26 foreign key (serviceproviderid) references mystore.serviceprovider (serviceproviderid)
ERROR 2015-08-18 11:08:15,691 [localhost-startStop-1] (SchemaExport.java:349) (create) - Can't create table 'mystore.#sql-358_e3' (errno: 150)
ERROR 2015-08-18 11:08:16,447 [localhost-startStop-1] (SchemaExport.java:348) (create) - Unsuccessful: alter table mystore.territory_serviceprovider add index FKFA1C11815254B0B5 (serviceproviderid), add constraint FKFA1C11815254B0B5 foreign key (serviceproviderid) references mystore.territory (territoryid)
ERROR 2015-08-18 11:08:16,447 [localhost-startStop-1] (SchemaExport.java:349) (create) - Can't create table 'mystore.#sql-358_e3' (errno: 150)[/b]
INFO 2015-08-18 11:08:16,764 [localhost-startStop-1] (SchemaExport.java:268) (execute) - schema export complete
INFO 2015-08-18 11:08:16,951 [localhost-startStop-1] (ContextLoader.java:214) (initWebApplicationContext) - Root WebApplicationContext: initialization completed in 4223 ms
INFO 2015-08-18 11:08:16,951 [localhost-startStop-1] (SpringApplicationContextListener.java:31) (contextInitialized) - Inside SpringApplicationContextListener / contextInitialized


1) ServiceProviderBean.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.dd.app.module.serviceproviders.bean.ServiceProviderBean"
      table="serviceprovider">
      <id name="serviceProviderId" type="java.lang.Long">
         <column name="serviceproviderid" precision="22" scale="0" />
         <generator class="identity" />
      </id>

      <property name="firstName" type="string">
         <column name="firstname" length="100" />
      </property>
      <property name="lastName" type="string">
         <column name="lastname" length="100" />
      </property>
      <property name="gender" type="string">
         <column name="gender" length="10" />
      </property>
      
      <set name="serviceBeans" table="service_serviceprovider"
            inverse="false" lazy="false" fetch="select" cascade="refresh">
            <key column="serviceproviderid" />
            <many-to-many column="serviceid" class="com.dd.app.module.services.bean.ServiceBean" />
         </set>
         
         <set name="territories" table="territory_serviceprovider"
            inverse="false" lazy="false" fetch="select" cascade="refresh">
           <key column="serviceproviderid" />
            <many-to-many column="territoryid" class="com.dd.app.module.territory.bean.TerritoryBean" />
        </set>
    </class>
</hibernate-mapping>


2) ServiceBean.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.dd.app.module.services.bean.ServiceBean"
      table="service">
      <id name="serviceId" type="java.lang.Integer">
         <column name="serviceid" precision="22" scale="0" />
         <generator class="identity"/>
      </id>
      <property name="serviceName" type="string">
         <column name="servicename" length="200" />
      </property>
      <property name="serviceDesc" type="string">
         <column name="servicedesc" length="500" />
      </property>
      
      <set name="serviceProviderBeans" table="service_serviceprovider"
           inverse="true" lazy="true" fetch="select" cascade="refresh" >
            <key column ="serviceproviderid"  />
            <many-to-many column="serviceid" class="com.dd.app.module.services.bean.ServiceBean" />
        </set>
      
   </class>
</hibernate-mapping>


3) TerritoryBean.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.dd.app.module.territory.bean.TerritoryBean"
      table="territory">
      <id name="territoryId" type="java.lang.Integer">
         <column name="territoryid" precision="22" scale="0" />
         <generator class="identity"/>
      </id>
      <property name="territoryCode" type="string">
         <column name="territorycode" length="500" />
      </property>
      <property name="territoryDesc" type="string">
         <column name="territorydesc" length="200" />
      </property>
           
        <set name="serviceProviderTerritoryBeans" table="territory_serviceprovider"
           inverse="true" lazy="true" fetch="select" cascade="refresh" >
            <key column ="serviceproviderid"  />
            <many-to-many column="territoryid" class="com.dd.app.module.territory.bean.TerritoryBean" />
        </set>
   </class>
</hibernate-mapping>


4) database-config.xml

Code:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
   <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver" />
      <property name="url" value="jdbc:mysql://localhost:3306/mystore" />
      <property name="username" value="root" />
      <property name="password" value="root" />
   </bean>
   
    <!-- Hibernate SessionFactory -->
   <bean id="mySessionFactory"
      class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="dataSource">
         <ref local="dataSource" />
      </property>
      <property name="mappingResources">
         <list>
            <value>com/dd/app/module/services/bean/ServiceBean.hbm.xml</value>
            <value>com/dd/app/module/serviceproviders/bean/ServiceProviderBean.hbm.xml</value>
            <value>com/dd/app/module/city/bean/CityBean.hbm.xml</value>
            <value>com/dd/app/module/territory/bean/TerritoryBean.hbm.xml</value>
         </list>
      </property>

      <property name="hibernateProperties">
         <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
<!--          <prop key="hibernate.hbm2ddl.auto">validate</prop> -->
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="format_sql">true</prop>
            <prop key="hibernate.enable_lazy_load_no_trans">true</prop>
            <prop key="hibernate.current_session_context_class">thread</prop>
            <prop key="hibernate.default_schema">mystore</prop>
         </props>
      </property>
   </bean>
   
   <bean id="hibernateTemplate"
       class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
   
   
   <bean id="transactionInterceptor"
       class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager" />
   <property name="transactionAttributes">
      <props>
      <prop key="save">PROPAGATION_REQUIRED</prop>
      </props>
   </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
     <property name="dataSource" ref="dataSource" />
     <property name="sessionFactory" ref="mySessionFactory" />
    </bean>
   
   
</beans>


After such configurations, my applications works fine & successfully inserts records into masters and mapping tables.
But sometimes i get jdbc batch update exception saying cannot update child error due to foreign key constraint.

Thanks for help

Kind regards,
Karan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.