tags:

views:

28

answers:

1

Hi,

im developping a simple webservice : void deleteCar(int idCar)

Currently, what im doing in my code is :
1-to load the cardObjet by id :session.get(Car.class,idCar)
2-to remove it : session.delete(carLoaded)

So for an operation i have at least 2 sql query (n+1 problem ?)
I dont even speak about any not lazy relationship in the car that will resultat in more sql queries.

i have tought to use an HQL query that :
1-will check by the id if the car exist with a lockMode.UPGRADE (boolean carDao.exist(int id)) 2-if the car exist, use a hql query like this one :"delet from Car c where c.id=?"

But how the cache might react to this (we will use ehCache)

In the first solution, i'm almost sure the car will be evicted from the 2nd level cache.

In the 2nd solution( with hql query), the cache will be smart enough to remove the car loaded in the 2nd level cache ?

A: 

So for an operation i have at least 2 sql query (n+1 problem ?)

The N+1 problem occurs when you retrieve N entities (with 1 query) and need to perform an additional query per entity (N in total) to fetch something. That's not really the case here.

I don't even speak about any not lazy relationship in the car that will result in more sql queries.

Well, if you have a graph of objects, you'll need several delete queries. Aren't you happy that Hibernate can cascade them for you?

I have thought to use an HQL query that (...)

Firstly, I don't really see the point of the first select (I thought you wanted to limit the number of queries).

Secondly, a bulk HQL DELETE would indeed save a SELECT but it doesn't cascade to related entities which might be a problem in your case (so you'll have to handle relations yourself, if possible).

Not sure it will suit your needs.

But how the cache might react to this

Hibernate will invalidate needed region(s) upon completion of a "bulk Operation".

See Bulk Operations for details and recommendation about their use.

Pascal Thivent
"Firstly, I don't really see the point of the first select (I thought you wanted to limit the number of queries). " yes but i dont not how to delete an object without loading it first (using session.delete(objectLoaded)..). have you an other way to do it without this 1st loading?
mada
@mada: Well, you don't need to load it first when using a [bulk delete](http://docs.jboss.org/hibernate/core/3.3/reference/en/html/batch.html#batch-direct), as in your example.
Pascal Thivent
And how i force hibernate to not invalid the region cache ? because i can do it mannually like this:
mada
Thansk !is there a way to force hibernate to NOT invalidate the region concerned ? Because i want to invalidate ONLY the specific objet like this: sessionFactory.getCache().evictEntity(entityClass, identifier)
mada
DUPLICATE message
mada
@mada I don't think so. If you want to handle things yourself, I think you'll have to bypass Hibernate's query APIs (criteria, HQL, native) and use raw JDBC and the `doWork()` API.
Pascal Thivent