-->
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.  [ 5 posts ] 
Author Message
 Post subject: Entity only is inserted,never updated(Interceptor problem)
PostPosted: Wed Jun 27, 2007 1:23 pm 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
I have got a talbe that contains movements of modifications. So, this object/class can be updated never. How Can I indicate this situation to NHibernate?

So, This is, when I add a movement into a entity collection I want that it always is inserted when I execute SaveOrUpdate method from entity.

Example -->

entity.movemets.Add(new Movement(...));
session.SaveOrUpdate(entity);

result is-->
UPDATE ENTITY WHERE...
UPDATE MOVEMENTS WHERE ...

I would like me that the result is:
UPDATE ENTITY WHERE...
INSERT INTO MOVEMENTS VALUES....

the problem is that movement class has a composite-id primary key. So, I can't set unsaved-value. I've tried with version attribute, however, when I execute session.SaveOrUpdate(entity) hibernate only generates:

INSERT INTO MOVEMENTS...
it doesn't generates an UPDATE ENTITY sentence.

What about this situation, please?

As you can understand movement object only can be inserted.

Thanks for all.


Last edited by jeusdi on Thu Jun 28, 2007 3:05 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 27, 2007 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Just an idea (and a bit of a hack)... I would think you can use the <sql-update> element in your class mapping to override the default update SQL generated; just make sure the statement you give in <sql-update> does not actually update anything. I have not tried this myself.

There are other things you can do to the objects to make sure the properties are not updatable. That is, create a public parameterized constructor, a private default constructor (for NHibernate), and make all properties are get-only; this makes sure any retrieved objects are not updatable.

_________________
Karl Chu


Top
 Profile  
 
 Post subject: Interceptor problem
PostPosted: Thu Jun 28, 2007 3:00 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
I've token the decision of implement a Interceptor as:

Code:
      public object IsUnsaved(object entity)
      {
         if (entity.GetType().Equals(typeof(Model.Moviment)))
         {
            Model.Moviment moviment = (Model.Moviment)entity;
            return moviment.Decision_value == -1;
         }

         return null;
      }


So, I believe that if IsUnsaved function returns

true -> tells NH to do an insert
false -> tells NH to do an update
null -> use default ("is-unsaved" value)

The problem is that when an entity is savedOrUpdated doesn't generates SQL statement. I thought that if IsUnSaved method returns null, NHibernate will use unsaved-value decision.

For example,
Code:
  entity = session.get_entity(id);
  entity.Location = other_location; <-- So, entity need to be updated!!
  entity.movements.Add(new movement(...)); <-- So, movement must be inserted into MOVEMENTS table.


when I execute session.saveOrUpdate(entity), hibernate only generates
INSERT INTO MOVEMENTS VALUES(...) and It should generates :

UPDATE ENTITY ... and an INSERT INTO MOVEMENTS statements

And if I don't add a movement into entity, entity is updated correctly,
So, if I execute
entity = session.get_entity(id);
entity.Location = new Location;
session.saveOrUpdate(entity);

NHibernate generates an UPDATE ENTITY SET ... statement correctly.

What Do I do wrong?

Thanks for all.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
Can you post your mapping file please?

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 12:28 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
NHibernate shouldn't actually generate any SQL on SaveOrUpdate(), only on Flush. The reason you see some INSERTs is probably that your Moviments use identity identifier generation so NHibernate has to insert them right away but this is an exception, not the rule.


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