-->
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.  [ 7 posts ] 
Author Message
 Post subject: Update triggered when executing update
PostPosted: Wed Jun 06, 2012 12:06 pm 
Newbie

Joined: Wed Jun 06, 2012 11:47 am
Posts: 6
Hi,

I'm using Hibernate 3. I have several entities mapped to a data base but in this problem i'm using the SQL native. So i have a table 'A' and create a native sql query to update a value on that table. Depending if i execute that update from a standalone or if i execute it inside a server (Tomcat) y get diferent behaviour. Both executions are loading the same xml configuration files.

In the case of the standalone execution i get the database update and all ok.
But in the second case (inside a server) i see the native query execution (with the option show sql = true) as expected but inmediatly after that i see and update to another table wich is causing and exception (a Cast Exception), this exception is thrown in transaction.commit().

I don't want to get in why is the Cast Exception thrown, rather i want to know wich could be the causes for the different behaviour between both executions, and what could cause hibernate to trigger an update other than the one in the native update.

The native update looks like this:
Hibernate: update A set STAT = ? where ID = 111 and ALIA = ?

The extra update executed in server:
Hibernate: update B set C=?, D=? where E=? and F=?

My level of Hibernate is very newie, i have used it a bit but always found mappings and examples of querying previously in code. I also know that with this information migth be dificult to anser, but i don't know wich info would be usefull, i could post it if i 'm told wich part is of interest.

TIA
Javier
Regards.


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Wed Jun 06, 2012 1:18 pm 
Beginner
Beginner

Joined: Mon Jun 04, 2012 12:31 pm
Posts: 20
I'm not sure if this of any assistance, but if you make a SQL update you have to explicitly refresh those objects in question in the hibernate session or hibernate doesn't know that it was changed.


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Thu Jun 07, 2012 2:24 am 
Newbie

Joined: Wed Jun 06, 2012 11:47 am
Posts: 6
Thanks for replying.

But the problem is not having in memory a not synchronized version of data with the DataBase, the problem is that when comiting the native SQL the DataBase is not updated (due to an exception raised as described en post 1). I have executed that same SQL update in the database engine (a MSSQL Server) and all went good, i'm confused of why a second update is triggered when using Hibernate (i can understand that the memoery object won't reflect the DB state, but the thing is that the DB is not being updated).
I also understand that is an object has properties mapped to other tables, these ones may be updated in a saveOrUpdate operation of hibernate, BUT doing a native SQL, i don't get why that happend.

Regards
Javi


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Thu Jun 07, 2012 2:36 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
So... you probably have loaded a 'B' object and made some changes to it. Then, when you commit the transaction Hibernate will automatically try to store the modified 'B' object, and for some reason that fails. Hard to say why since you have not posted any stack trace or code.


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Thu Jun 07, 2012 3:37 am 
Newbie

Joined: Wed Jun 06, 2012 11:47 am
Posts: 6
nordborg wrote:
So... you probably have loaded a 'B' object and made some changes to it. Then, when you commit the transaction Hibernate will automatically try to store the modified 'B' object, and for some reason that fails. Hard to say why since you have not posted any stack trace or code.


Seeing the traces i have indeed loaded a 'B' object. I have written some useless code but ii causes the error. Basically a retieve an object (wich queries various tables) and make an update without modifiying the onject.

Could it be that the data originally in the DB is not expected to be store that way?, i mean hibernate can retrive it, but since it can't be stored as it was retrived, could it be that there is a restriction in the hibernate configuration that doesn't allow a value and the DB has that same value?.

I'm still confused why if in a session i retrieve an object and store it the same way an update is triggered, As said in post 1 i have executed this same code standalone and in server, the standalone does not trigger the update neither the error, but when executing in server yes, Could it be that another thread is modifying that same object?, an easy way to verify this?


Thanks.
Javi

The code:


private void testUpdate(){

Session hSession = SessionFactory.getSession(0); // This SessionFactory is a custom implementation, it just have several hibernate
//SessionFactory objects, one for each database we connect to. In this example
// only one database is used. The returned Session is a hibernate session.


try{

CEquip equip = null;

Query q2 = hSession.createQuery("from CEquip as ce "
+ " where ce.CEquipType.equiTypeId = 111 "
+ " and ce.equiAlia = :alias")
.setString("alias", "equip");

List<CEquip> lEquips = q2.list();

logger.trace("Number of equips retrived: " + lEquips.size());

if(lEquips.size() > 0){
equip = lEquips.get(0);
}

Transaction t = hSession.beginTransaction();

hSession.update(equip);

t.commit();

}catch (Exception e) {

logger.error("Error updating equip state", e);

}finally{
hSession.close();
}
}

The traces:

Hibernate: select cequip0_.EQUI_ID as EQUI1_20_, cequip0_.EQUI_STAT_ID as EQUI2_20_, cequip0_.EQUI_TYPE_ID as EQUI3_20_, cequip0_.VISIB as VISIB20_, cequip0_.EQUI_ALIA as EQUI5_20_, cequip0_.EQUI_FORE_ID as EQUI6_20_ from C_EQUIP cequip0_ where cequip0_.EQUI_TYPE_ID=111 and cequip0_.EQUI_ALIA=?

Hibernate: select cequipstat0_.EQUI_STAT_ID as EQUI1_21_0_, cequipstat0_.PICT_ID as PICT2_21_0_, cequipstat0_.EQUI_TYPE_ID as EQUI3_21_0_, cequipstat0_.EQUI_STAT_DESC as EQUI4_21_0_, cequipstat0_.EQUI_STAT_FORE_ID as EQUI5_21_0_ from C_EQUIP_STATUS cequipstat0_ where cequipstat0_.EQUI_STAT_ID=?

Hibernate: select cpicts0_.PICT_ID as PICT1_6_0_, cpicts0_.PICT_NAME as PICT2_6_0_, cpicts0_.PICT_PATH as PICT3_6_0_, cpicts0_.PICT_WIDT as PICT4_6_0_, cpicts0_.PICT_HEIG as PICT5_6_0_ from C_PICTS cpicts0_ where cpicts0_.PICT_ID=?

Hibernate: select cequiptype0_.EQUI_TYPE_ID as EQUI1_19_0_, cequiptype0_.PICT_ID as PICT2_19_0_, cequiptype0_.EQUI_TYPE_DESC as EQUI3_19_0_, cequiptype0_.EQUI_TYPE_NAME as EQUI4_19_0_, cequiptype0_.EQUIP_GROUP as EQUIP5_19_0_ from C_EQUIP_TYPE cequiptype0_ where cequiptype0_.EQUI_TYPE_ID=?

Hibernate: select cequipmaps0_.EQUI_ID as EQUI1_1_, cequipmaps0_.MAP_TYPE_ID as MAP2_1_, cequipmaps0_.EQUI_ID as EQUI1_24_0_, cequipmaps0_.MAP_TYPE_ID as MAP2_24_0_, cequipmaps0_.LON as LON24_0_, cequipmaps0_.LATI as LATI24_0_, cequipmaps0_.ZOOM_MAXI as ZOOM5_24_0_, cequipmaps0_.ZOOM_MINI as ZOOM6_24_0_, cequipmaps0_.DRAW_PX_DISP_X as DRAW7_24_0_, cequipmaps0_.DRAW_PX_DISP_Y as DRAW8_24_0_ from C_EQUIP_MAPS cequipmaps0_ where cequipmaps0_.EQUI_ID=?

Hibernate: select maplayers0_.EQUI_TYPE_ID as EQUI1_1_, maplayers0_.MAP_TYPE_ID as MAP2_1_, maplayers0_.EQUI_TYPE_ID as EQUI1_8_0_, maplayers0_.MAP_TYPE_ID as MAP2_8_0_, maplayers0_.INCLUDE as INCLUDE8_0_, maplayers0_.DEFAULT_VISIBLE as DEFAULT4_8_0_ from C_MAP_TYPES_EQUIP_LAYES maplayers0_ where maplayers0_.EQUI_TYPE_ID=?
2012-06-07 09:24:49,812 TRACE [Thread-14] (NotificationsUpdateHandler.java:496) - Number of equips retrived: 1

Hibernate: update C_MAP_TYPES_EQUIP_LAYES set INCLUDE=?, DEFAULT_VISIBLE=? where EQUI_TYPE_ID=? and MAP_TYPE_ID=?

2012-06-07 09:24:49,828 ERROR [Thread-14] (NotificationsUpdateHandler.java:512) - Error updating equip state
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
at org.hibernate.type.BooleanType.set(BooleanType.java:36)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:107)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1997)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2371)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2307)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2607)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:92)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at xx.xx.xx.xx.xx.testUpdate(NotificationsUpdateHandler.java:508)


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Thu Jun 07, 2012 4:10 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
I don't know why you are seeing different results when running standalone vs. running in Tomcat. Hibernate usually uses automatic dirty-checking and there is no need to explicitly call Session.update() unless the entity has been modified while being detached from the session (which doesn't seem to be case here).

The exception is probably because you have some mismatch between your mapping and code for some property in one of your classes (the one mapped to C_MAP_TYPES_EQUIP_LAYES table). From the SQL logs it seems like there are several relations from the 'CEquip' class to other classes that are also loaded as a result from the original query. Are you using the same database in both cases? Different data sets may explain why you are seeing different results.


Top
 Profile  
 
 Post subject: Re: Update triggered when executing update
PostPosted: Thu Jun 21, 2012 2:57 am 
Newbie

Joined: Wed Jun 06, 2012 11:47 am
Posts: 6
You where rigth, it seems that when an insert is done in a table is triggered (programatically) an insert to another table, well really an saveOrUpdate.

About the different behaviour between running in Tomcat or running standalone, i haven't search further, because i could do a workaround.

Thanks to all.


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