Auto-incrementing Lazy loading caching cascading

Posted on 2011-05-06
Last Modified: 2012-06-22

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.
Question by:gudii9
    LVL 47

    Accepted Solution


    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:

    LVL 92

    Expert Comment

    > 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
    LVL 92

    Assisted Solution

    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

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now