Getting started with Spring-Data

Spring Data is a project that helps creating queries for both SQL and no-SQL databases. This is being done with repository interfaces or classes. Let me show you. In this example we use spring-boot, h2 and spring-data.

First we create a class for our data.

@Data
@Entity
public class Product{

    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private double price;

    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    protected Product() {}
}

Remember to use

import javax.persistence.Id;

and not

import org.springframework.data.annotation.Id;

, because then hibernate will not be able to generate Id’s for our data class.

  • @Data is a Lombok annotation which generates standard boilerplate code more information can be found here.
  • @Entity makes this a JPA entity.
  • @Id makes this the unique identifier.
  • @GeneratedValue says how to create the unique identifier. There are multiple ways to do this. Look at this blog for more information.
  • The default constructor is necessary for Hibernate, but otherwise not necessary. That’s why it’s protected.

We create an interface.

public interface ProductRepository extends CrudRepository<Product, Long>{

}

CrudRepository creates methods for saving, removing and finding objects during runtime. For this it needs the JPA class you use and the type of the primary key in the class.

To show what we can do with the repository we create a commandline runner bean.

@Bean
public CommandLineRunner fillRepository(ProductRepository productRepository){
		return (runner) -> {
			productRepository.save(new Product("Gyros", 2.49));
			System.out.println(productRepository.findOne(1l).toString());

			List plist = new ArrayList<>(Arrays.asList(new Product("Tzaziki", 0.99), new Product("Pita", 1.49)));
			productRepository.save(plist);
			productRepository.findAll().forEach(System.out::println);
		};
	}

Before the main application will run the CommandLineRunner will run. In this case our CommandLineRunner is the full application. We create an individual product and add it to the repository. Then we read the item with id 1 from the repository and print it to the console. we write 1L, because it’s a Long type.

Then we create a list with two more products and save the list. After that we read all the items from the repository and write them to the console using method reference.

When we run the project we see:

Product(id=1, name=Gyros, price=2.49)

//some boot code and then:
Product(id=1, name=Gyros, price=2.49)
Product(id=2, name=Tzaziki, price=0.99)
Product(id=3, name=Pita, price=1.49)

Advertisements

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