Announcement Announcement Module
No announcement yet.
Hibernate Mapping Many-to-One using Annotations Issue Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Mapping Many-to-One using Annotations Issue

    I have a many to one relationship between 2 classes A and B(a->B). I want to use annotations unfortunately there are many classes to be converted in my project( hibernate + spring integrated ). I ended up changing only Class B to annotations and class A will have the old hbm mapping file itself..

    While doing so i get an exception creating sessionFactory "An association from the table A refers to an unmapped class: B"

    Is it not possible to use annotation and hbm configuration mixed?

  • #2
    you can of course have both, are you using org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean?


    • #3
      hi bdangubic,

      sorry for the delayed response!

      I am using AnnotationSessionFactoryBean.. Shown below is my context file.
      <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
      <property name="configLocation">
      <property name="annotatedClasses">

      I was successfully annotating MyClass. Class A is having many-to-one association with Class B. Now i try to annotate class B and class A will have hbm mapping. I am not able to do it.
      When deploying i get the error message as below:::

      Caused by: org.hibernate.MappingException: An association from the table my_table1 refers to an unmapped class: pack.ClassB
      at org.hibernate.cfg.Configuration.secondPassCompileF oreignKeys(
      at org.hibernate.cfg.Configuration.secondPassCompile(
      at org.hibernate.cfg.AnnotationConfiguration.secondPa ssCompile(
      at org.hibernate.cfg.Configuration.buildMappings(Conf
      at org.springframework.orm.hibernate3.LocalSessionFac toryBean.buildSessionFactory(LocalSessionFactoryBe
      at org.springframework.orm.hibernate3.AbstractSession FactoryBean.afterPropertiesSet(AbstractSessionFact
      at AutowireCapableBeanFactory.invokeInitMethods(Abstr

      Is it possible to annotate class B alone without annotating Class A?


      • #4
        Can any expert clarify my doubts?


        • #5
          One to many using Hibernate .

          If you want to make the one-to-many / Manay-to-one relation between two tables, You have to define two classes for each Enity. As you mentioned . Both class A and B should be Annotation class. You can not use HBM for one and Annotation for other class if you want to make relation between those classes,

          Because in class A you have to mention the targetEntity which is nothing but the other class.

          You can refer this site for getting idea on this,


          • #6
            Hi i am new to spring and Hibernate, I have three classes presently in my project, one is Login which is working fine, The next is School and Address, I first tested Login and School(without address) and was working fine.

            But when I add (association) the Address class to the School, I'm getting this exception always like :

            org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'loginDao' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\EduERP\WEB-INF\cfg\beans.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sessionFactory' defined in file [C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\EduERP\WEB-INF\cfg\beans.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An association from the table efe_school refers to an unmapped class: erp.domain.Address

            "this loginDao" is my Dao class for retriving the Login object, I have a dao class for school too"

            my mapping for School, school.hbm.xml

            <?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "">
            <class entity-name="school" name="erp.domain.School" table="efe_school">
            <id column="SCHOOL_ID" name="schoolId" type="integer">
            <generator class="native"/>

            <property name="schoolName">
            <column name="SCHOOL_NAME"/>

            <property name="principalId">
            <column name="PRINCIPAL_ID"/>

            <many-to-one name="address" class="erp.domain.Address"
            column="ADDRESS_ID" unique="true"
            cascade="all" not-null="true"/>

            <property name="phone1">
            <column name="PHONE_1"/>

            <property name="phone2">
            <column name="PHONE_2"/>

            <property name="email">
            <column name="EMAIL"/>

            <property name="fax">
            <column name="FAX"/>

            <property name="website">
            <column name="WEBSITE"/>


            and for Address; address.hbm.xml

            <?xml version="1.0"?>
            <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            <class entity-name="address" name="erp.domain.Address" table="efe_address">
            <id column="ADDRESS_ID" name="addressId" type="integer">
            <generator class="native"/>

            <property name="addressLine1">
            <column name="ADDRESS_LINE_1"/>

            <property name="addressLine2">
            <column name="ADDRESS_LINE_2"/>

            <property name="village">
            <column name="VILLAGE"/>

            <property name="town">
            <column name="TOWN"/>

            <property name="city">
            <column name="CITY"/>

            <property name="state">
            <column name="STATE"/>

            <property name="country">
            <column name="COUNTRY"/>

            <property name="pincode">
            <column name="PINCODE"/>

            <property name="modifiedDate">
            <column name="MODIFIED_DATE"/>

            and My spring config file looks like..

            <?xml version="1.0" encoding="UTF-8"?>
            <beans xmlns=""


            <bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
            <property name="dataSource" ref="dataSource"/>

            <tx:annotation-driven transaction-manager="txManager"/>

            <bean id="loginDao" class="erp.daoservice.LoginDaoImp">
            <property name="sessionFactory" ref="sessionFactory"/>

            <bean id="schoolDao" class="erp.daoservice.SchoolDaoImp">
            <property name="sessionFactory" ref="sessionFactory"/>

            <bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource" abstract="true">

            <bean id="dataSource" parent="parentDataSource" destroy-method="close" >
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="username" value="root"/>
            <property name="password" value="root"/>
            <property name="url" value="jdbc:mysql://localhost:3306/erpforeducation"/>

            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
            <property name="dataSource" ref="dataSource"/>

            <property name="mappingResources" >

            <property name="hibernateProperties">
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQL Dialect</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQ L5Dialect</prop>
            <prop key="hibernate.jdbc.batch_size">50</prop>
            <prop key="hibernate.cache.use_second_level_cache">false </prop>
            <!-- <property name="hibernate.jdbc.batch_versioned_data">true</property> -->
            <prop key="hibernate.order_updates">true</prop>
            <prop key="hibernate.order_inserts">true</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.charSet">UTF-8</prop>

            I was stuck with this for a long time please help me, if anybody can


            • #7
              Problem solved.

              I removed entity-name="address" from address.hbm.xml . Thing is that if we gave a name to an entity we should use it always.