Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Wednesday, January 21, 2009

use of cascade in hbm file

1) cascade="none", the default, tells Hibernate to ignore the association.
2) cascade="save-update" tells Hibernate to navigate the association when transaction is committed and when an object is passed to save() or
update() and save newly instantiated transient instances and persist changes to
detached instances.
3) cascade=""delete" tells Hibernate to navigate the association and delete persistent
instances when an object is passed to delete().
4) cascade="all" means to cascade both save-update and delete, as well as
calls to evict and lock.
5) cascade="all-delete-orphan" means the same as cascade="all" but, in addition,
Hibernate
delete any persistent entity instance that has been removed
(dereferenced) from the association (for example, from a collection).
6) cascade="delete-orphan" Hibernate will delete any persistent
entity
instance that has been removed (dereferenced) from the association (for
example, from a collection).

Monday, December 22, 2008

FETCH DISTICT ROWS USING HIBERNATE

Using Projections
------------------------------

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

// add the restrictions to the criteria
criteria.add(Restrictions.like("teamName",
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.
projList.add(Projections.distinct(Projections.property("teamId)));
projList.add(Projections.property("teamName"));

// 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.setName("I%");
dataTable.setTeamName (CloConstants.PERCENTAGE + teamSearchName.toUpperCase() + CloConstants.PERCENTAGE);
Example example= Example.create(dataTable);
//exclude a particular column in the table
example.excludeProperty("teamIInactiveFLag");
example.excludeZeroes();
example.enableLike();
criteria.add(example);

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(Projections.property(/*Criteria*/))).addOrder(Order.asc(/*Order by criteria*/));
// Enable query caching.
criteria.getExecutableCriteria(getSession()).setCacheable(true);
List /*list name*/= getHibernateTemplate().findByCriteria(criteria);


Using sessions :

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

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...