-->
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.  [ 11 posts ] 
Author Message
 Post subject: Delete doesn't work... Why? :(
PostPosted: Tue Nov 18, 2003 6:23 pm 
Newbie

Joined: Tue Nov 18, 2003 6:09 pm
Posts: 7
Location: Macei
Hi all,

Today I have tried to use hibernate to update my persistence mechanism. Everything was going ok, implemented some basic methods to save/update and load objects in my PersistenceBroker and so on... However, when I tried to implement a method to remove an instance of a PERSISTENT object, nothing happened. I tried to use the code bellow with and without the transaction scope, but nothing still haven't happened... WHAT'S GOING ON WITH MY CODE? :'( Suppose that user is an object instance that is correctly mapped as well as it has been loaded and saved correctly...

Code:
   Transaction tx = null;
   Session session = null;
   
   try {
      Session ss = PersistenceMechanism.getInstance().getSession();
      tx = session.beginTransaction();
   
      ss.delete(user); //<<<------ It doesn't work...??? Why?

      tx.commit();
   
   } catch (HibernateException he) {
      if (tx != null)
         tx.rollback();
      throw he;
   
   } finally {
      session.close();
      
   }

_________________
Marcello Sales
) Java J2EE Programmer
(( Home-Work
|''|-. Rua Gon


Top
 Profile  
 
 Post subject: Re: Delete doesn't work... Why? :(
PostPosted: Tue Nov 18, 2003 7:10 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
javaman wrote:
Hi all,

Today I have tried to use hibernate to update my persistence mechanism. ..


My delete's work fine. Are you instanciating a SessionFactory correctly? Where are you loading the class instance to delete? Looks to me you are not loading the instance in the right place. Try loading it before the delete.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 12:40 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I think, that perhaps you need a ss.flush() after your delete? Not sure on that, as I don't delete records.

Reason for that is, you have no real way of checking the history of records then. On many of the projects I've work on, it has been specified that there be an "Active" field in each DB table, which is set to, e.g. 1, if the record is "deleted". That way, you still have your records, just you can specify in your queries to: Active <> 1.

Oh, and you're using 2 different sessions?

Session session, and Session ss, so when you call tx.commit, it's not actually committing your ss.delete()

-G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 19, 2003 12:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No he doesn't need to flush.


Perhaps you need to check your unsaved-value mapping. Maybe Hibernate thinks the object is transient.


Top
 Profile  
 
 Post subject: Now it's working
PostPosted: Wed Nov 19, 2003 7:37 pm 
Newbie

Joined: Tue Nov 18, 2003 6:09 pm
Posts: 7
Location: Macei
Hi All,

Thank you for your attention and replies... First of all the code I put here was indeed wrong... It contains 2 sessions... :)

The code that worked was the one with flush() after the delete...

Code:
Session ss = PersistenceMechanism.getInstance().getSession();
ss.delete(user);
ss.flush();


My SessionFactory is created inside my singleton PersistenceMechanism, and it's through that instance is that I use getSession() to get the session instance...

The mapping of the class has the following unsaved-value in its id tag:

Code:
<id name="id" type="string" unsaved-value="null">


As Gavin told that it wasn't necessary to flush after delete, I realize that it could be the mapping about the unsaved-value... So, if anyone has a suggestion, please let me know...

regards,

_________________
Marcello Sales
) Java J2EE Programmer
(( Home-Work
|''|-. Rua Gon


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 1:43 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Can you post the full mapping for that object? Your code, as we've seen, looks fine, so the problem may be in the mapping.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 3:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Stupid tip but... The user you delete has id=null ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 20, 2003 12:05 pm 
Newbie

Joined: Tue Nov 18, 2003 6:09 pm
Posts: 7
Location: Macei
Hi guys,

Brannor, the following is the user mapping... and epbernard, sure it wasn't with an id null... :)

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping.dtd">

<hibernate-mapping>
   <class name="br.ufal.cryptonline.User" table="user">

      <id name="id" type="string" unsaved-value="null">
            <column name="OID" sql-type="char(32)" not-null="true"/>
            <generator class="uuid.hex"/>
      </id>
      <property name="firstName">
         <column name="firstName" sql-type="varchar(20)" not-null="true"/>         
      </property>
      
      <property name="lastName">
         <column name="lastName" sql-type="varchar(20)" not-null="true"/>               
      </property>
      
      <property name="email">
         <column name="email" sql-type="varchar(100)" not-null="true"/>               
      </property>
      
      <property name="password">
         <column name="password" sql-type="varchar(15)" not-null="true"/>               
      </property>
      
      <property name="publicKeyN">
         <column name="publicKeyN" sql-type="varchar(20)" not-null="true"/>               
      </property>
      
      <property name="publicKeyE">
         <column name="publicKeyE" sql-type="varchar(20)" not-null="true"/>               
      </property>
      
      <property name="privateKeyD">
         <column name="privateKeyD" sql-type="varchar(20)" not-null="true"/>               
      </property>
   </class>
</hibernate-mapping>


The problem is that I have to flush after delete an object...

thank u for your attention...

_________________
Marcello Sales
) Java J2EE Programmer
(( Home-Work
|''|-. Rua Gon


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:57 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
I went reading through the reference doc, and came across this:

Quote:
Session.delete() will remove an object's state from the database. Of course, your application might still hold
a reference to it. So it's best to think of delete() as making a persistent instance transient.

Now, I know that this probably isn't the issue, but it's something worth considerring.

Your mapping looks fine though, from what we can see. All I can think of would be maybe to try evict the user from the session after your delete. Dunno really. :/

-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 1:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Show a debug-level Hibernate log.


Top
 Profile  
 
 Post subject: Working with Flush() method to delete...
PostPosted: Fri Nov 21, 2003 2:20 am 
Newbie

Joined: Tue Nov 18, 2003 6:09 pm
Posts: 7
Location: Macei
Brannor,

I have also read this part of the doc and it seems to me just fine... The only thing is that on some examples it's not necessary to use it... I have just finished to implement a persistence mechanism adapter for Hibernate... I was involved in a project that used OJB about six months ago and now I'm responsible for update the persistence layer with Hibernator... So, I'm still just using Junit to test the implementation... The UML diagram is as bellow...

[img]http://www.mycgiserver.com/~marcellojunior/projects/cryptonline/doc/images/PersistenceLayerFactory.gif
[/img]

So, I'll be using it to and try to get it running with no problems... I'll be still using the flush method... :)

Thank you for your attention...

_________________
Marcello Sales
) Java J2EE Programmer
(( Home-Work
|''|-. Rua Gon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.