views:

158

answers:

3

In hibernate as well as simple ORM strategy,i am normally loading all the values for the objects.Is there any way to avoid this. Due to this my app running slowly..

+1  A: 

There are several ways to achieve this:

  • Enable caching. While the SQL won't change, Hibernate will compare the results from the database and reuse existing objects. This doesn't give an enormous boost but it's simple to implement and maintain.

  • Create a second mapping (same tables, different object) to load just a subset of the values.

  • Move the rarely used data into a second table.

  • Load your objects with a custom native SQL query. Here, you must make sure that you know how much has been loaded and how to "fill the gaps".

Aaron Digulla
+2  A: 

The Hibernate reference document has a chapter on Improving Performance. You could look at your fetching strategies or whether using caching would improve performance.

To be more specific you would need to provide more details on your application and how the objects loaded via ORM are used.

Mark
Usually it's loading dependant objects that causes ORM performance to suffer. You need to set your fetch policy aggression based on how likely it is you'll need a child property that causes another fetch. A person object retrieving an Address object or Orders collection, for example. Address will probably be needed, so load it, but Orders, maybe not, so be less aggressive.
Chris Kaminski
A: 

In yoru entities mapping use FetchType.LAZY. i.e.:

@Entity
public class MyEntity {

private List<MyAnotherEntity> values;

@ManyToOne(fetch = FetchType.LAZY)
public List<MyAnotherEntity> getValues() {
 return values;
}


}

then, when you select entity with criteria you need set fetch mode if you need your collection to be mapped into object.

Criteria c = createCreteria(entityManager, MyEntity.class);
c.setFetchMode("values", FetchMode.JOIN);
Vanger