[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

Auto-incrementing Lazy loading caching cascading

I was reading folowing lines and I did not understand clearly.

Auto-incrementing keys
When you insert a row in the database, you typically assign it a primary key that
uniquely identifies that row. It is good practice to use a surrogate key for your primary
key. That is, the primary key should have no business meaning but is instead
generated within your application. Spring provides a means to do this via the
DataFieldMaxValueIncrementer interface. This interface has three different methods
for obtaining the next value to be used as a key: nextIntValue(), next-
LongValue(), and nextStringValue().
We would use a DataFieldMaxValueIncrementer like this:
We can then wire in an implementation of this interface. Spring comes with
implementations that hook into the sequence mechanism for Oracle, Postgre-
SQL, MySQL, and Hypersonic databases. You are free to write your own implementation
as well.

Lazy loading—As our object graphs become more complex, we sometimes
don’t want to fetch entire relationships immediately. To use a typical example,
suppose we are selecting a collection of PurchaseOrder objects, and
each of these objects contains a collection of LineItem objects. If we are
only interested in PurchaseOrder attributes, it makes no sense to grab the
LineItem data. This could be quite expensive. Lazy loading allows us to
grab data only as it is needed.

i was not clear on caching, cascading as well. Which one we have to use where and advantage and disadvanate of using them

Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
  • 2
2 Solutions

These links will give you short and understandable description and
code example about what is meant by Lazy loading and pre-loading of beans


Autoincrementing is rathe starightforward stuff - when you insert
your objects into database it is often convenient to have unique
key associated with each object
(think about say help desk request ticket numbers, or long numbers
of air tickets - thoe are all examples of autogenerated keys).
The snippet below illusatrates how DataFieldMaxValueIncrementer
works for generation of unique id in order to associate with each new
row in the database.
Without Spring you'd probably explicitly use database sequence
to generate id in such cases.
Spring makes it easier for the developer and independent of the database

public class LibraryDaoImp implements LibraryDao {

	private JdbcTemplate jdbcTemplate;
	private DataFieldMaxValueIncrementer bookIncrementer;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	public void setBookIncrementer(DataFieldMaxValueIncrementer bookIncrementer) {
		this.bookIncrementer = bookIncrementer;
	/* (non-Javadoc)
	 * @see de.laliluna.library.LibraryDao#insertBook(de.laliluna.library.Book)
	public Book insertBook(Book book) {
		// TODO Auto-generated method stub
		return null;

To insert, update and to delete needs no further preparation, so have a look at the source code:

	public Book insertBook(Book book) {
		//get next autoincrement value
		Integer id = bookIncrementer.nextIntValue();
		String sql = "insert into book (id, title, author) values (?,?,?)";
		Object args []= new Object[] { id, book.getTitle(), book.getAuthor() };
		int types[] = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR };
		jdbcTemplate.update(sql, args, types);
		return book;
	public void updateBook(Book book) {
		String sql = "update book set title=?, author=?, borrowedby=? where id = ?";
		Object args []= new Object[] { book.getTitle(), book.getAuthor(), book.getBorrowedBy(), book.getId() };
		int types[] = new int[] { Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.INTEGER };
		jdbcTemplate.update(sql, args, types);

	public void deleteBook(Book book) {
		String sql = "delete from book where id = ?";
		Object params[] = new Object[] {book.getId()};
		int types[] = new int [] {Types.INTEGER};		
		jdbcTemplate.update(sql, params, types);

Open in new window

This seesm to be a good article about spring caching, though probably a litte bit too detailed for
genearl understanding:


> i was not clear on caching, cascading as well. Which one we have to use where and advantage and disadvanate of using them

caching and cascading of what exactly? The text you posted is nothing to do with those concepts.
If you've posted the wrong question then simplest to just delete this one and reopen a new one
good discussion here on lazy fetching

though as I said caching and cascading are really seperate issues

If you're talking about use of the second level cache then have a read here
some good examples of cascading here

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now