Monday, December 22, 2008


Using Projections

DetachedCriteria criteria = DetachedCriteria.forClass(DATATABLE.class);
ProjectionList projList=Projections.projectionList();

// add the restrictions to the criteria
CloConstants.PERCENTAGE + name.toUpperCase() + CloConstants.PERCENTAGE));

// distinct on teamId, also adding the two properties teamId and TeamName will return only two properties from the DB.

// hibernateTemplate for springs
List teamList = hibernateTemplate.findByCriteria(criteria);
System.out.println("teamsList :"+teamList.size());

// The List, teamList returned will be a list of Object. In order to retrieve a list of DATATABLE objects, use setResultTransformer. But this retrieves all the columns from the table DATATABLE. criteria.setProjection(projList).setResultTransformer(Transformers.aliasToBean(DATATABLE.class));

Using Named Query
var names = (from dr in dataTable.Rows
select (string)dr["Name"]).Distinct().OrderBy(name => name);

Using Query by Example

DataTable dataTable = new DataTable ();
//Search by the name
dataTable.setTeamName (CloConstants.PERCENTAGE + teamSearchName.toUpperCase() + CloConstants.PERCENTAGE);
Example example= Example.create(dataTable);
//exclude a particular column in the table

Monday, October 20, 2008

Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on the situation


A SAX (Simple API for XML) parser does not create any internal structure. Instead, it takes the occurrences of components of an input document as events, and tells the client what it reads as it reads through the input document.

· A SAX parser serves the client application always only with pieces of the document at any given time.

· A SAX parser, however, is much more space efficient in case of a big input document (because it creates no internal structure). What’s more, it runs faster and is easier to learn than DOM parser because its API is really simple. But from the functionality point of view, it provides a fewer functions, which means that the users themselves have to take care of more, such as creating their own data structures.

1. Parses node by node

2. Doesn't store the XML in memory

3. We cant insert or delete a node

4. Top to bottom traversing

The SAX protocol requires a lot more programming than the Document Object Model (DOM). It’s an event-driven model (you provide the callback methods, and the parser invokes them as it reads the XML data), which makes it harder to visualize. Finally, you can’t “back up” to an earlier part of the document, or rearrange it, any more than you can back up a serial data stream or rearrange characters you have read from that stream.


A DOM (Document Object Model) parser creates a tree structure in memory from an input document and then waits for requests from client.
· A DOM parser always serves the client application with the entire document no matter how much is actually needed by the client.

· A DOM parser is rich in functionality. It creates a DOM tree in memory and allows you to access any part of the document repeatedly and allows you to modify the DOM tree. But it is space inefficient when the document is huge, and it takes a little bit longer to learn how to work with it.

1. Stores the entire XML document into memory before processing

2. Occupies more memory

3. We can insert or delete nodes

4. Traverse in any direction.

If we need to find a node and doesn’t need to insert or delete we can go with SAX itself otherwise DOM provided we have more memory.

Saturday, October 18, 2008

difference between getHibernateTemplate().load and getHibernateTemplate().get

the HibernateTemplate is basically a wrapper around the native Hibernate API.

load() just creates a proxy and does not hit the database while get() does.
With load you need to re-attach the owning session somehow, which is only possible by locking it to the saved object. You don't really want to do that if your using the OpenSessionInViewFilter / Interceptor for your views as you will end up with a session remaining locked open

get() will return null if an object is not found while load() will always return a non-null object which is a proxy. If the underlying object does not exist, the proxy will thrown ObjectNotFoundException.
load() should be used when you are sure that the object exits while get() when you're not.

Wednesday, August 20, 2008

Query Caching using Detached criteria

//Create a criteria
DetachedCriteria criteria = DetachedCriteria.forClass(/*classname.class*/);
criteria = criteria.setProjection(
Projections.distinct(*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/));
// Enable query caching.
List /*list name*/= getHibernateTemplate().findByCriteria(criteria);

Using sessions :

session = HibernateSessionFactory.currentSession();
List /*list name*/= session.createCriteria(/*classname.class*/).setProjection( Projections.distinct(*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/))) .setCacheable(true).list();