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..
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".
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.
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);