Home » Java » Enterprise Java » Spring JpaRepository Example (In-Memory)

About Jerome Versrynge

Jerome Versrynge

Spring JpaRepository Example (In-Memory)

This post describes a simple Spring JpaRepository example using an in memory HSQL database. The code example is available from GitHub in the Spring-JpaRepository directory. It is based on the Spring-MVC-With-Annotations example and information available here.

JPA Repository

We implement a dummy bean for this example:

public class SomeItem {

    private long Id;

    private String someText;

    /* ...Setters & Getters */


and the corresponding JpaRepository:

public interface SomeItemRepository
        extends JpaRepository<SomeItem, Long> {


Service & Controller

Next, we implement a service where our repository will be injected. We also populate the repository with dummy data:

public class SomeItemService {

    private SomeItemRepository someItemRepository;

    public void populate() {

        SomeItem si = new SomeItem();

        si = new SomeItem();

        si = new SomeItem();


    public List<SomeItem> getAll() {

        return someItemRepository.findAll();


    public SomeItem saveAndFlush(SomeItem si) {

        if ( si != null ) {
            si = someItemRepository.saveAndFlush(si);

        return si;


    public void delete(long id) {




and a controller:

public class MyController {

    private SomeItemService someItemService;

    @RequestMapping(value = "/")
    public ModelAndView index() {

        ModelAndView result = new ModelAndView("index");
        result.addObject("items", this.someItemService.getAll());

        return result;


    @RequestMapping(value = "/delete/{id}")
    public String delete(
        @PathVariable(value="id") String id) {


        return "redirect:/";


    @RequestMapping(value = "/create")
    public String add() {

        SomeItem si = new SomeItem();
        si.setSomeText("Time is: " + System.currentTimeMillis());


        return "redirect:/";



JPA Configuration

On top of creating an entity manager based on an in-memeory instance of HSQL database, we enable JPA repositories with the
@EnableJpaRepositories annotation:

public class JpaConfig implements DisposableBean {

    private EmbeddedDatabase ed;

    public EmbeddedDatabase hsqlInMemory() {

        if ( this.ed == null ) {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            this.ed = builder.setType(EmbeddedDatabaseType.HSQL).build();

        return this.ed;


    public LocalContainerEntityManagerFactoryBean entityManagerFactory(){

        LocalContainerEntityManagerFactoryBean lcemfb
            = new LocalContainerEntityManagerFactoryBean();

        lcemfb.setPackagesToScan(new String[] {"com.jverstry"});


        HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();

        Properties ps = new Properties();
        ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        ps.put("hibernate.hbm2ddl.auto", "create");


        return lcemfb;


    public PlatformTransactionManager transactionManager(){

        JpaTransactionManager tm = new JpaTransactionManager();

            this.entityManagerFactory().getObject() );

        return tm;


    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();

    public void destroy() {

        if ( this.ed != null ) {



The JSP Page

We create a simple page to list existing items with a delete link, and the possibility to create new items:

 Running The Example

One can run it using the maven tomcat:run goal. Then, browse: http://localhost:9191/spring-jparepository/


Reference: Spring JpaRepository Example (In-Memory) from our JCG partner Jerome Versrynge at the Technical Notes blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
2 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
2 Comment authors
Kaizar LaxmidharBoris Recent comment authors
newest oldest most voted
Notify of

I get java.lang.NullPointerException when I try to run metthods of interface in my case
repo.findAll() where repo defined before as @Autowired
GreetingRepo repo;

Instance is null, Something miss here

Kaizar Laxmidhar
Kaizar Laxmidhar

I also face the same issue reported above. java.lang.NullPointerException.