-->
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: 'select' before 'insert' and 'update' after 'insert'?
PostPosted: Mon Jul 25, 2005 7:07 am 
Beginner
Beginner

Joined: Tue Jul 12, 2005 11:15 am
Posts: 29
Hi,

I don't understand why hibernate needs:
- to make a select SQL command before inserting the data,
- to make an update SQL command after inserting the data.


____________
| Car |
------------------
| id |
|fatherCar |
|firstSonCar |
|nextSiblingCar |
------------------

I intialize the first car, the second car and the father car like this:

O father car: id=[0] firstSonId=[1]
/ \
/ O second car: id=[0] fatherId=[0]
O
first car: id=[1] fatherId=[0] nextSiblingId=[1]



Why hibernate make 'select' and 'update' SQL command, and not just 'insert' ?
How to ban the 'select' and the 'insert'?
Should i change the relationnal mapping?
Should i set a property and which property?



Thanks.


*************************************************************
Hibernate version: 3.0.5

*************************************************************
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="samples.domain.Car"
table="T_CAR"
dynamic-update="false"
dynamic-insert="false"
select-before-update="false"
optimistic-lock="version"
>
<cache usage="read-write" />
<id
name="id"
column="CAR_ID"
type="java.lang.Integer"
>
<generator class="assigned" />
</id>

<property
name="content"
type="java.lang.String"
update="true"
insert="true"
access="property"
column="CAR_CONTENT"
/>



<!-- unidirectionnal association one-to-one with the father car -->
<many-to-one
name="fatherCar"
class="samples.domain.Car"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="CAR_FATHER_ID"
not-null="false"
lazy="true"
/>

<!-- unidirectionnal association one-to-one with the next sibling car -->
<many-to-one
name="nextSiblingCar"
class="samples.domain.Car"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="CAR_NEXTSIBLING_ID"
not-null="false"
lazy="true"
/>
<!-- unidirectionnal association one-to-one with the first son car -->
<many-to-one
name="firstSonCar"
class="samples.domain.Car"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="CAR_FIRSTSON_ID"
not-null="false"
lazy="true"
/>

</class>
</hibernate-mapping>

*************************************************************

Code between sessionFactory.openSession() and session.close():


Main:
------
Car fatherCar = new Car();
fatherCar.setId(new Integer(0));
fatherCar.setContent(" Father car");

Car firstCar = new Car();
firstCar.setId(new Integer(1));
firstCar.setContent(" first car");

Car secondCar = new Car();
secondCar.setId(new Integer(2));
secondCar.setContent(" second car");

//
fatherCar.setFirstSonCar(firstCar);

//
firstCar.setFatherCar(fatherCar);
firstCar.setNextSiblingCar(secondCar);

//
secondCar.setFatherCar(fatherCar);

// save first car
PersistenceManager.getUniqueInstance().saveCar(firstCar);

// save second car
PersistenceManager.getUniqueInstance().saveCar(secondCar);

// save father car
PersistenceManager.getUniqueInstance().saveCar(fatherCar);

PersistenceManager.getUniqueInstance().closeSession();


*****************************************

Persistence Manager:
-------------------------
package samples;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import samples.domain.Car;
import samples.domain.SteeringWheel;

public class PersistenceManager {
/**
* Logger for this class
*/
private static final Logger _logger = Logger
.getLogger(PersistenceManager.class);

/**
* Hibernate session factory
*/
private SessionFactory _sessionFactory;

/**
* Hibernate session
*/
private Session _session;

/**
* Transaction
*/
private Transaction _tx;

/**
* The unique instance of PersistenceManager class
*/
private static PersistenceManager _uniqueInstance;

/**
* Default constructor
*/
private PersistenceManager() {

super();
// Load hibernate configuration
Configuration cfg = new Configuration();
cfg.configure();

_sessionFactory = cfg.buildSessionFactory();
_session = _sessionFactory.openSession();
_tx = _session.beginTransaction();
}

/**
* @return the unique instance of PersistenceManager
*/
public static PersistenceManager getUniqueInstance() {

if (null == _uniqueInstance) {
_uniqueInstance = new PersistenceManager();
}
return _uniqueInstance;
}

public void saveCar(Car car) {
_logger.debug(" -> saveCar() ");

_session.save(car);
_session.flush();
_tx.commit();

}

public void saveSteeringWheel(SteeringWheel pSteeringWheel) {
_logger.debug(" -> saveSteeringWheel() ");

_session.save(pSteeringWheel);
_session.flush();
_tx.commit();

}

/**
* close the session close the session factory
*/
public void closeSession() {
_session.flush();
_tx.commit();
_session.clear();
_session.close();
_sessionFactory.close();
}
}
*************************************************************
Name and version of the database you are using:
postgresql 8 on XP SP2

*************************************************************
The generated SQL (show_sql=true):

stdout trace:
-------------
12:32:10,314 DEBUG (main) [hibernate.SQL] - select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?
12:32:10,384 DEBUG (main) [hibernate.SQL] - select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?
12:32:10,404 DEBUG (main) [hibernate.SQL] - insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)
12:32:10,424 DEBUG (main) [hibernate.SQL] - update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?
12:32:10,444 DEBUG (main) [hibernate.SQL] - insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)
12:32:10,444 DEBUG (main) [hibernate.SQL] - update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?
12:32:10,454 DEBUG (main) [hibernate.SQL] - insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)
12:32:10,454 INFO (main) [samples.Main] - Time elapsed=[1763 ms]

p6spy trace:
-------------
12:32:09,332|-1||debug||this is com.p6spy.engine.spy.P6SpyDriver@3afb99 and passthru is org.postgresql.Driver@11e67ac
12:32:10,384|40|1|statement|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=0
12:32:10,384|0|1|result|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=0
12:32:10,384|0|1|statement|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=2
12:32:10,384|0|1|result|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=?|select car_."CAR_ID", car_."CAR_CONTENT" as CAR2_0_, car_."CAR_STEERINGWHEEL_ID" as CAR3_0_, car_."CAR_FATHER_ID" as CAR4_0_, car_."CAR_NEXTSIBLING_ID" as CAR5_0_, car_."CAR_FIRSTSON_ID" as CAR6_0_ from "T_CAR" car_ where car_."CAR_ID"=2
12:32:10,404|0|1|batch|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' first car', '', '', '', '', 1)
12:32:10,404|0|1|statement|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' first car', '', '', '', '', 1)
12:32:10,434|0|1|batch|update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?|update "T_CAR" set "CAR_CONTENT"=' first car', "CAR_STEERINGWHEEL_ID"='', "CAR_FATHER_ID"=0, "CAR_NEXTSIBLING_ID"=2, "CAR_FIRSTSON_ID"='' where "CAR_ID"=1
12:32:10,444|0|1|statement|update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?|update "T_CAR" set "CAR_CONTENT"=' first car', "CAR_STEERINGWHEEL_ID"='', "CAR_FATHER_ID"=0, "CAR_NEXTSIBLING_ID"=2, "CAR_FIRSTSON_ID"='' where "CAR_ID"=1
12:32:10,444|0|1|commit||
12:32:10,444|0|1|batch|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' second car', '', '', '', '', 2)
12:32:10,444|0|1|statement|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' second car', '', '', '', '', 2)
12:32:10,454|10|1|batch|update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?|update "T_CAR" set "CAR_CONTENT"=' second car', "CAR_STEERINGWHEEL_ID"='', "CAR_FATHER_ID"=0, "CAR_NEXTSIBLING_ID"='', "CAR_FIRSTSON_ID"='' where "CAR_ID"=2
12:32:10,454|0|1|statement|update "T_CAR" set "CAR_CONTENT"=?, "CAR_STEERINGWHEEL_ID"=?, "CAR_FATHER_ID"=?, "CAR_NEXTSIBLING_ID"=?, "CAR_FIRSTSON_ID"=? where "CAR_ID"=?|update "T_CAR" set "CAR_CONTENT"=' second car', "CAR_STEERINGWHEEL_ID"='', "CAR_FATHER_ID"=0, "CAR_NEXTSIBLING_ID"='', "CAR_FIRSTSON_ID"='' where "CAR_ID"=2
12:32:10,454|0|1|commit||
12:32:10,454|0|1|batch|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' Father car', '', '', '', 1, 0)
12:32:10,454|0|1|statement|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (?, ?, ?, ?, ?, ?)|insert into "T_CAR" ("CAR_CONTENT", "CAR_STEERINGWHEEL_ID", "CAR_FATHER_ID", "CAR_NEXTSIBLING_ID", "CAR_FIRSTSON_ID", "CAR_ID") values (' Father car', '', '', '', 1, 0)
12:32:10,454|0|1|commit||
12:32:10,454|0|1|commit||


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 5:57 pm 
Beginner
Beginner

Joined: Tue Jul 12, 2005 11:15 am
Posts: 29
En fait, nous avons des references croisées sur les objets transients:

L'objet father car reference first car,
l'objet first car reference father car et second car,
l'objet second car reference father car.

Si l'on devait insérer ces données en base, 3 inserts suffisent.
Or Hibernate a besoin de 5 commandes SQL: 3 inserts et 2 updates.

Nous ne voulons pas d'update car cela plombe nos performances.
On cherche a minimiser les commandes SQL.

Comment faire pour eviter les updates?
faut-il modifier les relations entre objet?

Merci d'avance,
Pimento.

PS:
N'ayant pas de reponse, je me demande si le probleme est assez clair.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 5:58 pm 
Beginner
Beginner

Joined: Tue Jul 12, 2005 11:15 am
Posts: 29
En fait, nous avons des references croisées sur les objets transients:

L'objet father car reference first car,
l'objet first car reference father car et second car,
l'objet second car reference father car.

Si l'on devait insérer ces données en base, 3 inserts suffisent.
Or Hibernate a besoin de 5 commandes SQL: 3 inserts et 2 updates.

Nous ne voulons pas d'update car cela plombe nos performances.
On cherche a minimiser les commandes SQL.

Comment faire pour eviter les updates?
faut-il modifier les relations entre objet?

Merci d'avance,
Pimento.

PS:
N'ayant pas de reponse, je me demande si le probleme est assez clair.


Top
 Profile  
 
 Post subject: Re: 'select' before 'insert' and 'update' after 'insert'?
PostPosted: Sat Feb 06, 2010 3:53 pm 
Newbie

Joined: Sat Feb 06, 2010 3:51 pm
Posts: 2
Hi, i have the same probleme. Hibernate make an update after an insert. Have you find an issue to this problem because it's a real problem in my case? Thanks :-)


Top
 Profile  
 
 Post subject: Re: 'select' before 'insert' and 'update' after 'insert'?
PostPosted: Sat Feb 06, 2010 6:21 pm 
Newbie

Joined: Sat Feb 06, 2010 3:51 pm
Posts: 2
Je vais traduire mon problème en français. J'ai donc le même problème lorsque je fais un insert hibernate déclenche ensuite un update ce qui me pose un gros problème puisque je déclenche une action suivant l'évènement déclenché.

Quelqu'un aurait il la solution pour éviter cet update?

Merci beaucoup


Top
 Profile  
 
 Post subject: Re: 'select' before 'insert' and 'update' after 'insert'?
PostPosted: Sat Feb 13, 2010 12:28 am 
Newbie

Joined: Sat Feb 13, 2010 12:19 am
Posts: 1
Location: usa
Yes, I am agree this forum site according to me......

_________________
Clean Whites
Clean Whites


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.