-
Jan 12th, 2011, 01:22 AM
#1
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?
-
Jan 12th, 2011, 11:51 AM
#2
you can of course have both, are you using org.springframework.orm.hibernate3.annotation.Anno tationSessionFactoryBean?
-
Jan 18th, 2011, 01:21 AM
#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">
<value>classpath:/hibernate.cfg.xml</value>
</property>
<property name="annotatedClasses">
<list>
<value>pack.MyClass</value>
</list>
</property>
</bean>
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(Configuration.java:1258)
at org.hibernate.cfg.Configuration.secondPassCompile( Configuration.java:1176)
at org.hibernate.cfg.AnnotationConfiguration.secondPa ssCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Conf iguration.java:1121)
at org.springframework.orm.hibernate3.LocalSessionFac toryBean.buildSessionFactory(LocalSessionFactoryBe an.java:674)
at org.springframework.orm.hibernate3.AbstractSession FactoryBean.afterPropertiesSet(AbstractSessionFact oryBean.java:211)
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.invokeInitMethods(Abstr actAutowireCapableBeanFactory.java:1288)
Is it possible to annotate class B alone without annotating Class A?
-
Jan 20th, 2011, 03:58 AM
#4
Can any expert clarify my doubts?
-
Jan 24th, 2011, 03:23 AM
#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,
http://j2eereference.com/2011/01/one...ing-hibernate/
-
Nov 10th, 2011, 05:50 AM
#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" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="school" name="erp.domain.School" table="efe_school">
<id column="SCHOOL_ID" name="schoolId" type="integer">
<generator class="native"/>
</id>
<property name="schoolName">
<column name="SCHOOL_NAME"/>
</property>
<property name="principalId">
<column name="PRINCIPAL_ID"/>
</property>
<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>
<property name="phone2">
<column name="PHONE_2"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
<property name="fax">
<column name="FAX"/>
</property>
<property name="website">
<column name="WEBSITE"/>
</property>
</class>
</hibernate-mapping>
and for Address; address.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="address" name="erp.domain.Address" table="efe_address">
<id column="ADDRESS_ID" name="addressId" type="integer">
<generator class="native"/>
</id>
<property name="addressLine1">
<column name="ADDRESS_LINE_1"/>
</property>
<property name="addressLine2">
<column name="ADDRESS_LINE_2"/>
</property>
<property name="village">
<column name="VILLAGE"/>
</property>
<property name="town">
<column name="TOWN"/>
</property>
<property name="city">
<column name="CITY"/>
</property>
<property name="state">
<column name="STATE"/>
</property>
<property name="country">
<column name="COUNTRY"/>
</property>
<property name="pincode">
<column name="PINCODE"/>
</property>
<property name="modifiedDate">
<column name="MODIFIED_DATE"/>
</property>
</class>
</hibernate-mapping>
and My spring config file looks like..
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schem...ing-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="loginDao" class="erp.daoservice.LoginDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="schoolDao" class="erp.daoservice.SchoolDaoImp">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource" abstract="true">
</bean>
<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>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources" >
<list>
<value>login.hbm.xml</value>
<value>school.hbm.xml</value>
<value>address.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!--
<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>
</props>
</property>
</bean>
</beans>
I was stuck with this for a long time please help me, if anybody can
-
Nov 18th, 2011, 10:51 PM
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules