-->
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.  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: session.save() - data not reflected in the DB.
PostPosted: Wed Dec 19, 2007 3:23 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
Hi, i am a newbie to Hibernate and face a problem which i am sure must be quite trivial to others out here but please do help me out. when i do a insert using session.save(), my app returns a success message with an id generated. However, the DB doesnt reflect the inserted record. I am able to see the inserted record on my app ( on a view screen). However when i restart my server, the app doesnt contain the record either.



this is my code snippet
Code:
Transaction tx = hibSession.beginTransaction();

         
         tx.begin();
         PlannedCrewMove plannedCrewMove = new PlannedCrewMove();
         plannedCrewMove
               .setCrewMoveName(crewMoveDetailsTO.getCrewMoveName());

         
         CrewMoveType crewMoveType = new CrewMoveType();
         crewMoveType = (CrewMoveType) hibSession.load(CrewMoveType.class,
               crewMoveDetailsTO.getCrewMoveType());
         plannedCrewMove.setCrewMoveType(crewMoveType);
         plannedCrewMove.setFromBrqId(crewMoveDetailsTO.getFromBrqId());

         
         plannedCrewMove.setToBrqId(crewMoveDetailsTO.getToBrqId());
         Plan plan = new Plan();
         plan = (Plan) hibSession.load(Plan.class, crewMoveDetailsTO
               .getPssId());
         plannedCrewMove.setPlan(plan);

         
         plannedCrewMove.setNumToMove(new Long(crewMoveDetailsTO
               .getCrewMoved()));
         plannedCrewMove.setMoveDate(crewMoveDetailsTO.getFromDate());
         Initiative initiative = new Initiative();
         
         initiative = (Initiative) hibSession.load(Initiative.class, "XXX");
         plannedCrewMove.setInitiative(initiative);
         Long msgId = (Long) hibSession.save(plannedCrewMove);
         hibSession.flush();
         tx.commit();


this is the hbm.xml file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-Nov-2007 09:01:25 by Hibernate Tools 3.2.0.b9 -->
<hibernate-mapping>
    <class name="com.baplc.ccmpp.services.dao.beans.PlannedCrewMove" table="PLANNED_CREW_MOVE">
        <id name="crewMoveId" type="long">
            <column name="CREW_MOVE_ID" precision="10" scale="0" />
            <generator class="seqhilo" >
         <param name="sequence">PLAN_CM_SEQ</param>
         <param name="max_lo">0</param>
         </generator>
        </id>
        <many-to-one name="crewMoveType" class="com.baplc.ccmpp.services.dao.beans.CrewMoveType" fetch="select">
            <column name="CREW_MOVE_TYPE" length="3" not-null="true" />
        </many-to-one>
        <many-to-one name="plan" class="com.baplc.ccmpp.services.dao.beans.Plan" fetch="select">
            <column name="PLAN_ID" precision="10" scale="0" not-null="true" />
        </many-to-one>
        <many-to-one name="ccCmStatus" class="com.baplc.ccmpp.services.dao.beans.CcCmStatus" fetch="select">
            <column name="CM_STATUS_CODE" length="3" not-null="true" />
        </many-to-one>
        <many-to-one name="initiative" class="com.baplc.ccmpp.services.dao.beans.Initiative" fetch="select">
            <column name="INITIATIVE_CODE" length="10" />
        </many-to-one>
        <property name="crewMoveName" type="string">
            <column name="CREW_MOVE_NAME" length="50" />
        </property>
        <property name="fromBrqId" type="java.lang.Long">
            <column name="FROM_BRQ_ID" precision="10" scale="0" />
        </property>
        <property name="toBrqId" type="java.lang.Long">
            <column name="TO_BRQ_ID" precision="10" scale="0" />
        </property>
        <property name="moveDate" type="date">
            <column name="MOVE_DATE" length="7" not-null="true" />
        </property>
        <property name="tempPermInd" type="char">
            <column name="TEMP_PERM_IND" length="1" not-null="true" />
        </property>
        <property name="numToMove" type="java.lang.Long">
            <column name="NUM_TO_MOVE" precision="10" not-null="true" />
        </property>
        <property name="femaleCrewToMove" type="java.lang.Short">
            <column name="FEMALE_CREW_TO_MOVE" precision="3" scale="0" />
        </property>
        <property name="hce" type="big_decimal">
            <column name="HCE" precision="10" />
        </property>
        <property name="updateDatetime" type="timestamp">
            <column name="UPDATE_DATETIME" length="11" not-null="true" />
        </property>
        <set name="cmTrainingCourseDateds" inverse="true">
            <key>
                <column name="CM_COURSE_ID" precision="10" scale="0" not-null="true" unique="true" />
            </key>
            <one-to-many class="com.baplc.ccmpp.services.dao.beans.CmTrainingCourseDated" />
        </set>
        <set name="plannedContractChanges" inverse="true">
            <key>
                <column name="CREW_MOVE_ID" precision="10" scale="0" />
            </key>
            <one-to-many class="com.baplc.ccmpp.services.dao.beans.PlannedContractChange" />
        </set>
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 8:11 am 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
remove
Code:
hibSession.flush();

Are you using spring?

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 8:50 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
nope i am not using spring... removing session.flush doesnt help either. I am stuck... hopelessly.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 19, 2007 9:57 am 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
try
Code:
<one-to-one name="person" cascade="persist"/>
for mapping Initiative.

and do check if you are saving proper data for all the columns that are marked as not-null

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 2:36 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
yeah, i am giving proper data for all not-null fields... The App doesnt throw an error. So, doesnt it mean that the Query is proper and has been parsed? Moreover, I get a primary Id in return. However the values are not reflected in the DB.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 4:36 am 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
Are you sure that tx.commit() is done sucessfully?
Check with a try/catch that no exception appears, and check that the transaction is done (or remove it to check if the problem does not come from it.)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 4:41 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Also enable Hibernate logging (see Chapter 3.5 of the on-line docs: http://www.hibernate.org/hib_docs/v3/re ... on-logging) and if your database server supports it, monitor the activity at the database end.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 5:12 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
tx.commit is successful. No exceptions are thrown. Moreover i tried inserting into a stand-alone table with just two columns (both take strings), i get the same result. The table doesnt contain any dependencies or relationships to other tables.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 20, 2007 9:18 am 
Expert
Expert

Joined: Thu May 26, 2005 9:19 am
Posts: 262
Location: Oak Creek, WI
Can you post your hibernate.cfg.xml file here? Your code looks perfect...Normally when you flush the session, the data would be persisted..and you also do transaction commit...This is really strange..i had these kind of problem while using MySQL and SQLServer..

_________________
RamnathN
Senior Software Engineer
http://www.linkedin.com/in/ramnathn
Don't forget to rate.


Top
 Profile  
 
 Post subject: Re: session.save() - data not reflected in the DB.
PostPosted: Thu Dec 20, 2007 10:55 am 
Newbie

Joined: Tue May 08, 2007 4:37 pm
Posts: 2
aquarianguy16 wrote:
I am able to see the inserted record on my app ( on a view screen). However when i restart my server, the app doesnt contain the record either.


Are you sure you are not using
Code:
<property name="hibernate.hbm2ddl.auto"
          value="create-drop"/>
?


Top
 Profile  
 
 Post subject: hibernatre.cfg.xml
PostPosted: Fri Dec 21, 2007 12:28 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
Code:
<session-factory name="java:hibernate/SessionFactory">
      <property name="hibernate.session_factory_name">java:hibernate/SessionFactory</property>
      <property name="hibernate.jndi.url">t3://localhost:7001</property>
      <property name="hibernate.jndi.class">weblogic.jndi.WLInitialContextFactory</property>
      <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
      <property name="hibernate.connection.url">jdbc:oracle:thin:@bl2x2bc01:1521:ABCD</property>
      <property name="hibernate.connection.username">ABC</property>
      <property name="hibernate.connection.password">xyz</property>
      <property name="hibernate.connection.pool_size">500</property>
      <property name="hibernate.cache.use_second_level_cache">false</property>
      <property name="show_sql">true</property>
      <property name="format_sql">true</property>
      <property name="use_sql_comments">true</property>
      <property name="hibernate.bytecode.use_reflection_optimizer">true</property>
      <property name="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>     
     
      <!--<property name="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</property>
      --><property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
      <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
      <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 3:19 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
I can see from your config file that you have Hibernate logging the SQL statements it emits. So, does Hibernate actually issue a SQL insert statement to your database?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 4:13 am 
Newbie

Joined: Wed Dec 19, 2007 3:08 am
Posts: 8
here is the particular log snippet.... i hope this is what you were referring to.

Code:
Hibernate:
    /* insert com.dao.beans.MoveType
        */ insert
        into
            MOVE_TYPE
            (MOVE_TYPE_DESC, MOVE_TYPE)
        values
            (?, ?)
   2007-12-21 13:40:12,235         DEBUG     
   MESSAGE      :msg id is AC2       


here, Msg id is the generated Id after the call to session.save();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 6:07 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
I guess that establishes Hibernate issuing the insert to Oracle. So the next possible culprit is the commit. You are not by any chance running a nested transaction, i.e. the transaction your are starting and finishing is the only one active at the time? You are using a container (Weblogic?). Could that have started an outer transaction for you due to requested resource injection or other EJB mechanisms?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 12:46 pm 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
In using spring, if you issue a
Code:
session.commit()
, the transaction is committed, then immediately rolled back.
you have to use the
Code:
hibernateTemplate().commit()
for committing data.

The behavior here is strikingly similar.

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 19 posts ]  Go to page 1, 2  Next

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.