We will start with project setup then create entities, repositories and discuss about @EnableJpaRepositories annotation.
We will add spring-boot-starter-jpa to manage dependencies. We will use h2 embedded database server for persistence. Let's start with adding pom.xml dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
We have three entities in the example project i.e. Product, Rating and User.
@Entity
@Table(name = "product_ratings", schema = "product")
public class Rating {
@Id
@GeneratedValue
@Column(name="rating_id")
private Long ratingId;
private double rating;
@Column(name="product_id")
private String productId;
@Column(name="user_id")
private String userId;
public Rating() {
}
public Rating(Long ratingId, double rating, String productId, String userId) {
super();
this.ratingId = ratingId;
this.rating = rating;
this.productId = productId;
this.userId = userId;
}
//getters, setters, toString, hashCode, equals
}
@Entity annotation specifies that this is an entity class. @Table annotation specifies the primary table for an entity class. You can configure the table_name and schema using this annotation for the entity class. @Id specifies that this field is the primary key of the entity. @GeneratedValue specifies how primary key will be generated. @Column is used to specify the mapped column for the property or field. You can also configure if the property is unique, nullable, length, precision, scale and/or if you want to insert or update it in the table.
We can extend the JpaRepository or CrudRepository interface to create the repository.
@Transactional
@Repository
public interface ProductRepository extends JpaRepository<Product, String> {
}
Here, we created a ProductRepository interface which extends JpaRepository interface. You may wonder that instead of writing a repository class, we have created an interface and where will this get the implementation? The answer is SimpleJpaRepository class. A Proxy is generated by Spring and all the request is catered by the SimpleJpaRepository.
This contains all the basic methods like find, delete, save, findAll and few sort related/ criteria based search methods. Could be a case that you need to write your own specific method and in this case finding all the ratings of product. This could be done as follows.
@Transactional
@Repository
public interface RatingRepository extends JpaRepository<Rating, Long> {
public Iterable<Rating> getRatingsByProductId(final String productId);
}
@EnableJpaRepositories annotationThis annotation will enable JPA repositories. This will scan for Spring Data repositories in annotated configuration class by default. You can also change the basePackages to scan in this annotation.
@SpringBootApplication
@EnableJpaRepositories
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
You can see the full code on Github used in this example.
Few important points
If you are using embedded server in the above example, then you may need to set the following configurations.
Adding
schema.sqlin the classpath, if you are using schema in your tables(entity classes). You can get sample here. You can change the datasource name(by defaulttestdb) and other properties. Seeorg.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesfor full list of properties that you can configure.