Benjamin Winterberg

Ranch Hand
+ Follow
since Sep 19, 2007
Merit badge: grant badges
For More
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Benjamin Winterberg

Normally Springs HibernateDaoSupport manages the hibernate session by itself. The internally used HibernateTemplate opens the hibernate session and closes the session on transaction#commit(). Maybe there's some misconfiguration in your project which brokes springs session management?

Also HibernateDaoSupport isnt the right thing to implement DAOs anymore. Take a look at this blog entry for a detailed explanation:

You can inspect the HibernateStatistics to check if all opened sessions are correctly closed (see SessionFactory#getStatistics()).
Have you read the chapter about single ended association proxies?

Perhaps there is a case where hibernate cannot create a proxy so hibernate forces to eagerly load the association.
Take a look at Hibernate EventListeners:

You could check if the property is null or empty after the entity has been loaded by hibernate and in that case set your default value.
I'm using an Apache DBCP datasource with hibernate. Works perfectly. There's no magic around pooling database connections. ;)

lokesh pattajoshi wrote:HI every body i am using this query to get single value from data base but it is returning multiple values.I know setFirstResult and setMaxResult but except that can we use other things directly in hql.

select count(S.humanID) from Human S,Animal W WHERE S.humanID=W.animalID group by S.humanID order by count(S.humanID) desc limit 0,1

There is no database independant way to express the FirstResult/MaxResult semantic in SQL. So there is no way you can do it via HQL. setFirstResult/setMaxResults will be translated into LIMIT x,y for MySQL Dialect. But the sql for other databases is totally different (e.g. "fetch first x rows only" for DB2).

If you except a single row result but you get mulitple rows than you should re-think your statement. It might be wrong.

Paul Sturrock wrote:For my own curiosity Ben because I don't know Spring very well (and sorry Max for the slight hijack) if you use an HTTP provided DAO layer does Spring propagate transactions across it?

Good point Paul. My fault, this will not work unless it runs on a JEE container with JTA support. You would need to not only put the dao layer to this web project but the whole service layer. So you would end up with an EJB like service facade which encapsulates the whole persistence layer in one project. Spring makes this project run without an EJB container on servlet container.
Take a look at how Cascade works in Hibernate. You have marked the relation with cascade = all. This means that when you delete an entity all related entities will be deleted as well.
You could encapsulate your persistence layer into a seperate web project with an additional service facade delegating to the dao methods. These services can be made accessible to other web projects (jsf) not matter if they are deployed on the same or another server.

There are many ways to provide those services to other projects. I prefer using the Spring Framework. In this case you can use Springs HTTP Invoker protocol to make the services accessible via HTTP. But you can easily switch to Webservices, RMI, Hession or whatever you wish.

Another simple approach is to put your persistence code into a separate java project. Your web projects can than be dependant to this project. They will instantiate their own database connections, daos etc on runtime.

I would prefer the first approach. You can use one database connection pool instead of many, you can use a second level cache to improve performance. But on the other side its a single point of failure.
You can easily use hibernate with the same database on multiple servers. In most cases you dont need real distributed transactions (thats what you described cameron?), normal database transactions are enough. Therefore the data doesnt get locked. It depends on the transaction isolation level how concurrent transactions are handled by your database.
You could try to put an index onto some of the columns from your where clause. You also could try to replace one or more JOINs with subselects.

I dont know which DB you are using (8 mins sounds insane). You can inspect the explain plan of the sql query to see whats going wrong there, if your DB got something like that (explain plan is oracle specific).

You should also check if you're using the right database driver. Try the generated sql with a proprietary database tool and look if its the same time as from hibernate.
Im no friend of writing one DAO for multiple entities. Requirements are changing during development. You have to write more and more DAO-methods so you end up with one bloated DAO class you have to refactor.

What Im doing is writing one DAO per entity class with a generic super class to implement CRUD methods which is the same to all entities.

You can find a good explanation of the generic DAO here:
I was refering to this:

But maybe I was wrong about EAGER fetching by SELECT.

No matter what, best practices goes around lazy fetching:

Prefer lazy fetching for associations.
Use eager fetching sparingly. Use proxies and lazy collections for most associations to classes that are not
likely to be completely held in the second-level cache. For associations to cached classes, where there is an
a extremely high probability of a cache hit, explicitly disable eager fetching using lazy="false". When an
join fetching is appropriate to a particular use case, use a query with a left join fetch.

Bhavesh Dak wrote:e.g. now I want to execute query, (Assuming all the required constructors available)

Will this work or how can I achieve this?

Dont know if it works. Just try it out. If its not working, you can easily work around by creating the objects manually:

Bhavesh Dak wrote:One more thing I would Like to know is, Is it possible that we can manipulate java code inside query ?

What do you mean exactly by manipulating java code? There are many ways to manipulate java code programmatically (e.g. JDK Proxies, Bytecode Instrumentation etc) but I dont think its possible to do something like that via HQL.