Cascade Attribute in Hibernate
none ignore the association. save-update navigate the association when the 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. delete navigate the association and delete persistent instances when an object is passed to delete(). all cascade both save-update and delete as well as calls to evict and lock. all-delete-orphan the same as cascade "all" but in addition deletes any persistent entity instance that has been removed (dereferenced) from the association (for example from a collection). delete-orphan delete any persistent entity instance that has been removed (dereference) from the association (for example from a collection).
1. HQL Select Query Example
Query query = session.createQuery("from Stock where stockCode = :code "); query.setParameter("code", "7277"); List list = query.list();
2. HQL Update Query Example
Update a stock name to DIALOG1 where stock code is 7277.
Query query = session.createQuery("update Stock set stockName = :stockName" + " where stockCode = :stockCode"); query.setParameter("stockName", "DIALOG1"); query.setParameter("stockCode", "7277"); int result = query.executeUpdate();
4. HQL Insert Query Example
In HQL, only the INSERT INTO SELECT is supported; there is no INSERT INTO VALUES. HQL only support insert from another table. For example
"insert into Object (id, name) select oo.id, oo.name from OtherObject oo";
Insert a stock record from another backup_stock table. This can also called bulk-insert statement.
Query query = session.createQuery("insert into Stock(stock_code, stock_name)" + "select stock_code, stock_name from backup_stock"); int result = query.executeUpdate();
The query.executeUpdate() will return how many number of record has been inserted, updated or deleted.
Hibernate Criteria examples
Ordering the records::
Criteria criteria = ses.createCriteria(Child.class).addOrder( Order.asc("date") );
estrictions.lt, le, gt, ge
Make sure the volume is less than 10000.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.lt("volume", 10000));
Make sure the volume is less than or equal to 10000.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.le("volume", 10000));
Make sure the volume is great than 10000.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.gt("volume", 10000));
Make sure the volume is great than or equal to 10000.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.ge("volume", 10000)); Criteria criteria = ses.createCriteria(Parent.class) .add(Restrictions.like("pname", "g%")); List al= criteria.list(); Iterator itr=al.iterator(); while(itr.hasNext()) { Parent neaw=(Parent)itr.next(); System.out.println("Parent is .." +neaw.getPname()); }
Restrictions.between
Make sure the date is between start date and end date.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.between("date", startDate, endDate));
Restrictions.isNull, isNotNull
Make sure the volume is null.
Criteria criteria = session.createCriteria(StockDailyRecord.class) .add(Restrictions.isNull("volume"));
3. Criteria paging the result
Criteria provide few functions to make pagination extremely easy. Starting from the 20th record, and retrieve the next 10 records from database.
Criteria criteria = session.createCriteria(StockDailyRecord.class); criteria.setMaxResults(10); criteria.setFirstResult(20);
Criteria criteria = ses.createCriteria(Parent.class) .add(Restrictions.like("pname", "g%")); criteria.setMaxResults(1);
Hibernate parameter binding
There are two ways to parameter binding : named parameters or positional.
1. Named parameters
This is the most common and user friendly way. It use colon followed by a parameter name (:example) to define a named parameter. See examples
Example 1 setParameter
The setParameter is smart enough to discover the parameter data type for you.
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setParameter("stockCode", "7277") .list();
Example 2 setString
You can use setString to tell Hibernate this parameter date type is String.
String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setString("stockCode", "7277") .list();
Example 3 setProperties
This feature is great ! You can pass an object into the parameter binding. Hibernate will automatic check the objects properties and match with the colon parameter.
Stock stock = new Stock(); stock.setStockCode("7277"); String hql = "from Stock s where s.stockCode = :stockCode"; List result = session.createQuery(hql) .setProperties(stock) .list();
2. Positional parameters
Its use question mark (?) to define a named parameter, and you have to set your parameter according to the position sequence. See example
String hql = "from Stock s where s.stockCode = ? and s.stockName = ?"; List result = session.createQuery(hql) .setString(0, "7277") .setParameter(1, "DIALOG") .list();
This approach is not support the setProperties function. In addition, its vulnerable to easy breakage because every change of the position of the bind parameters requires a change to the parameter binding code.
String hql = "from Stock s where s.stockName = ? and s.stockCode = ?"; List result = session.createQuery(hql) .setParameter(0, "DIALOG") .setString(1, "7277") .list();