-->
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.  [ 6 posts ] 
Author Message
 Post subject: Why can not insert into database
PostPosted: Wed Dec 20, 2006 5:57 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
I have the following code:

Code:
   session.save(gp1);
   session.flush();
   session.clear();
    session.close();

               session = sessionFactory.openSession();
    tracks = listGroupRelation(session);
                iiter = tracks.listIterator();
   while ( iiter.hasNext() ) {
     GroupRelation aTrack = (GroupRelation)iiter.next();
     if (aTrack.contains(gp1))
     {
                 System.out.println("The node exist no2");  //check whether the database changed
     }


Where, gp1 is the object instance I want to save to database. I can see, there is printout "The node exist no2" for the above code. However, when I check the database, the gp1 does not be inserted into db. Can anybody tell me why this happens?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 6:09 pm 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
turn on the echo of sql statements generated on your configuration file to show the sql statement generated. It seems your sql instruction isn't behind a session context.

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 6:40 pm 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
No. It is on a session.
Actually, I just got the solution, because it is not in a transaction. The save() must include in a transaction() and commit().

However, when I do the delete operation, another exception produced:

[java] org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.bear.test1.GroupRelation#33775140]
[java] at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:556)
[java] at orgg.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventLis
tener.java:88)
[java] at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventLis
tener.java:49)
[java] at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:766)
[java] at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:744)
[java] at com.bear.test1.QueryTest.initDirectedGraph(Unknown Source)
[java] at com.bear.test1.QueryTest.main(Unknown Source)
[java] Exception in thread "main"
[java] Java Result: 1

Who knows why this happen?

The code is almost the same thing, except I replace the save with delete. This time, I include the delete in a transaction() and commit().


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 20, 2006 6:59 pm 
Beginner
Beginner

Joined: Thu Oct 12, 2006 6:19 pm
Posts: 34
Location: Guatemala
how are you ensuring the object identity?, to say how do you attach the objet to delete to session environment?

_________________
God is Love


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 10:05 am 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
I use the following code:

Code:
//gp1 is the object I want to delete
        //tracks is the collection of mapping objects
    Transaction tranc = session.beginTransaction();
   if (tracks.contains(gp1))
   {
        System.out.println("contrain the result");
                } 
                     tracks.add(gp1);
        gp1 = tracks.get(index);
       session.delete(gp1);
    tranc.commit();
   session.flush();
   session.clear();
    session.close();


I just found the error, and it is the " tracks.add(gp1);" This is another test I added and I forget to remove it.
After I remove this, it works.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 10:18 am 
Newbie

Joined: Thu Nov 23, 2006 5:00 pm
Posts: 19
Now, I have a new problem for saveOrUpdate() method. This time, I want use saveOrUpdate() to insert into table.

Code:
Transaction tranc = session.beginTransaction();
session.saveOrUpdate(gp1);
System.out.println("go to here");
tranc.commit();


The "go to here" can be printout. However, when I do commit, it produced the following exception:

[java] org.hibernate.StaleStateException: Batch update returned unexpected row count from updat
e [0]; actual row count: 0; expected: 1
[java] at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:6
1)
[java] at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:
46)
[java] at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:24)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersi
ster.java:2353)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEnt
ityPersister.java:2257)
[java] at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersi
ster.java:2557)
[java] at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
[java] at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
[java] at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
[java] at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractF
lushingEventListener.java:298)
[java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListen
er.java:27)
[java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
[java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
[java] at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
[java] at com.bear.test1.QueryTest.initDirectedGraph(Unknown Source)
[java] at com.bear.test1.QueryTest.main(Unknown Source)
[java] Exception in thread "main"
[java] Java Result: 1

Why I can not use saveOrUpdate()? The id for the mapping xml, I set unsaved-value="null". Is it right?


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

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.