Over a million developers have joined DZone.

Creating Custom Spring Data JPA Repositories

Here's how to make custom Spring Data JPA repositories with SQL.

· Database Zone

Sign up for the Couchbase Community Newsletter to stay ahead of the curve on the latest NoSQL news, events, and webinars. Brought to you in partnership with Coucbase.

It can be useful to create a custom repository using Spring Data. A good example is a system which contains database views, and you dont want to expose the Create, Remove or Update methods provided by the CrudRepository. Fortunately this is not a hard task.

Lets start with a simple table and view -

CREATE TABLE my_table (
   my_table_id NUMBER,
   my_table_type VARCHAR2(100)
);

CREATE VIEW my_view AS
   SELECT my_table_id, my_table_type FROM my_table;

Populate with sample data –

INSERT INTO my_table(MY_TABLE_ID, MY_TABLE_TYPE) VALUES (1, 'Value 1');
INSERT INTO my_table(MY_TABLE_ID, MY_TABLE_TYPE) VALUES (2, 'Value 2');

Now create a JPA object for this –  

@Entity
@Table(name = "my_view")
public class MyView implements Serializable {

    private static final long serialVersionUID = 1L;


    @Id
    @Column(name = "my_table_id")
    private Long myTableId;

    @Column(name = "my_table_type")
    private String myTableType;

    // add getters and setters

}

Now the interesting bit – define our custom Repository –

import org.springframework.data.repository.*;

@NoRepositoryBean
public interface ViewRepository<T, ID extends Serializable> extends Repository<T, ID> {
   T findOne(ID id);
   boolean exists(ID id);
   Iterable<T> findAll();
   long count();
}

The key points are –

  • Use @NoRepositoryBean to prevent the Repository from being created. This is similar to creating an abstract class

  • We add the method definitions as defined in CrudRepository, but exclude delete and sav

The reason you can just add the methods you require from CrudRepository to your new repository is that they are defined in CrudRepository implementation class SimpleJpaRepository

We can now create an instance of the repository –

public interface MyViewRepository extends ViewRepository<MyView, Long> {
}

We can then call these methods –

@Autowired
private MyViewRepository myViewRepository; 

// ... 

@Override
public long getMyViewCount() {
   return myViewRepository.count();
}

The Getting Started with NoSQL Guide will get you hands-on with NoSQL in minutes with no coding needed. Brought to you in partnership with Couchbase.

Topics:
java ,spring ,spring data jpa

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}