Spring data -yet another abstraction -quickly getting started


We started with JDBC, then ORM which abstracted the underlying relation DB. Then came JPA -which abstracted the underlying ORM. Now Spring Data

But the kind of versatility we see today in data storage -relational, distributed, nosql and others, to me Spring Data seems to server the purpose.

It abstracts the CRUD for “different” type of data as relational, nosql and just by coding to appropriate interface CRUD operations can be achieved. For getting started use this link for configuring the entity manage, transaction manager -basically the setup stuff.

Write a DaoInterface -here the magic goes, the extended interface decides type of data , relational, nosql etc.

public interface TestDao extends CrudRepository<TestData , Integer> {
}

And then for CRUD operations use this Interface :

@Service
public class TestService{
    @Autowired
    TestDao testDao;
 	public boolean saveTestData(TestData testData){
    	if(testData!=null){
    		testDao.save(user);
                return true;
        }
    	return false;
    }
}
     @Entity
	@Table (name="testtable")
	public class TestData {
	 
	    @Id
	    @GeneratedValue
	    private Long id;
	     
	    public Long getId() {
	        return id;
	    }
	 
	    public void setId(Long id) {
	        this.id = id;
	    }

I found it simple, yet flexible and power full considering the fact that although projects start with SQL, but down the line NOSQL is definitely on their road map -this abstraction will ensure least throwaway code!

Advertisements

About ChandanPandey

Try to come up with a good design as by product of good coding practices

Posted on August 22, 2013, in Java, Spring and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: