Cascade Attribute in Hibernate: None Save-Update
Cascade Attribute in Hibernate: None Save-Update
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).
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.
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
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);
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();