Announcement Announcement Module
Collapse
No announcement yet.
null identifier exception while saving Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • null identifier exception while saving

    I have two objects, ObjectA and ObjectB.. ObjectA has one to one association with ObjectB. Please find the mappings and exception in detail below.

    All operations(save, update, get) works fine if i get Session using,
    Session session = HibernateSessionFactory.getSession();
    ==================
    Save operation not working when i try to get session using spring sessionFactory (other operations work get, update) ..

    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource ("applicationContext.xml", new Test().getClass()));

    SessionFactory factory = (SessionFactory)bf.getBean("sessionFactory");
    Session session = factory.openSession();
    ==================
    Exception in thread "Main Thread" org.springframework.dao.InvalidDataAccessApiUsageException:

    The given object has a null identifier: com.abc.mod.entity.BImpl; nested exception is org.hibernate.TransientObjectException:
    The given object has a null identifier: com.abc.mod.entity.BImpl
    org.hibernate.TransientObjectException:
    The given object has a null identifier: com.abc.mod.entity
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.getUpdateId(DefaultSaveOrUpdateEventListene r.java:227)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsDetached(DefaultSaveOrUpdateEventLi stener.java:214)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.performSaveOrUpdate(DefaultSaveOrUpdateEven tListener.java:93)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList ener.java:70)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(Se ssionImpl.java:531)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(Sessio nImpl.java:523)
    at org.hibernate.engine.CascadingAction$1.cascade(Cas cadingAction.java:134)
    at org.hibernate.engine.Cascade.cascadeToOne(Cascade. java:213)
    at org.hibernate.engine.Cascade.cascadeAssociation(Ca scade.java:157)
    at org.hibernate.engine.Cascade.cascadeProperty(Casca de.java:108)
    at org.hibernate.engine.Cascade.cascade(Cascade.java: 248)
    at org.hibernate.event.def.AbstractSaveEventListener. cascadeAfterSave(AbstractSaveEventListener.java:41 0)
    at org.hibernate.event.def.AbstractSaveEventListener. performSaveOrReplicate(AbstractSaveEventListener.j ava:299)
    at org.hibernate.event.def.AbstractSaveEventListener. performSave(AbstractSaveEventListener.java:167)
    at org.hibernate.event.def.AbstractSaveEventListener. saveWithGeneratedId(AbstractSaveEventListener.java :114)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.saveWithGeneratedOrRequestedId(DefaultSaveO rUpdateEventListener.java:186)
    at org.hibernate.event.def.DefaultSaveEventListener.s aveWithGeneratedOrRequestedId(DefaultSaveEventList ener.java:33)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.entityIsTransient(DefaultSaveOrUpdateEventL istener.java:175)
    at org.hibernate.event.def.DefaultSaveEventListener.p erformSaveOrUpdate(DefaultSaveEventListener.java:2 7)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventLi stener.onSaveOrUpdate(DefaultSaveOrUpdateEventList ener.java:70)
    at org.hibernate.impl.SessionImpl.fireSave(SessionImp l.java:559)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.ja va:547)
    at org.hibernate.impl.SessionImpl.save(SessionImpl.ja va:543)
    ==================================

    <hibernate-mapping package="com.abc.mod.entity.impl">
    <class name="AImpl" table="XXX" optimistic-lock="version" >
    <id name="id" column="ID" type="long">
    <generator class="sequence">
    <param name="sequence">SQ_XXX</param>
    </generator>
    </id>

    ....

    <one-to-one name="b"
    class="com.abc.mod.entity.impl.BImpl"
    cascade="all"
    lazy="false"/>

    .....

    </class>
    </hibernate-mapping>

    <hibernate-mapping package="com.abc.mod.entity.impl">
    <class name="BImpl" table="YYY" optimistic-lock="version">
    <id name="id" column="ID" type="long">
    <generator class="foreign">
    <param name="property">a</param>
    </generator>
    </id>
    ....
    ....
    <one-to-one name="a" class="com.abc.mod.entity.impl.AImpl"
    constrained="true" />
    </class>
    </hibernate-mapping>

    =============================================

    <beans>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>oracle.jdbc.driver.OracleDriver</value>
    </property>
    .....
    .....
    </bean>

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

    <!-- mapping files. -->
    <property name="mappingResources">
    <list>
    <value>com/abc/mod/entity/impl/A.hbm.xml</value>
    <value>com/abc/mod/entity/impl/B.hbm.xml</value>
    </list>
    </property>

    <!-- -->
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.Orac le9Dialect</prop>
    <prop key="transaction.factory_class">org.hibernate.tran saction.JDBCTransactionFactory</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate .cache.EhCacheProvider</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.query.factory_class">org.hibernate. hql.classic.ClassicQueryTranslatorFactory</prop>
    <prop key="hibernate.cglib.use_reflection_optimizer">fal se</prop>
    </props>
    </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.Hibernat eTransactionManager">
    <property name="sessionFactory">
    <ref local="sessionFactory" />
    </property>
    </bean>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.Hibernat eTemplate">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    <!-- service beans -->
    <bean id="AService" class="com.abc.mod.service.test.TestServiceImpl">
    <property name="transactionManager">
    <ref bean="transactionManager" />
    </property>
    <property name="testDAO">
    <ref bean="testDAO" />
    </property>
    </bean>

    <!-- dao beans -->
    <bean id="testDAO" class="com.abc.mod.dao.test.TestDAOImpl">
    <property name="hibernateTemplate">
    <ref local="hibernateTemplate" />
    </property>
    </bean>
    </beans>
    Last edited by prem.rs; Oct 16th, 2006, 09:29 AM.
Working...
X