Manu
Manu

Reputation: 337

Spring data repository is not working

Application.java

package com.example;

import com.example.repository.AuthorRepository;
import com.example.entities.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;

@SpringBootApplication
public class JpaHibernateApplication {

    @Autowired
    private static AuthorRepository authorRepository;

    @Transactional
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(JpaHibernateApplication.class, args);
        for(String name : ctx.getBeanDefinitionNames()){
            System.out.println(name);
        }
        Author author = new Author();
        author.setName("Vamsi");
        authorRepository.save(author);
    }
}

Author.java

package com.example.entities;

import javax.persistence.*;

@Entity
@Table(name="author")
public class Author {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }




}

AuthorRepository

package com.example.repository;

import com.example.entities.Author;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AuthorRepository extends CrudRepository<Author,Long> {

}

application.properties

 spring.datasource.url = jdbc:mysql://localhost:3306/Learning?useSSL=false
    spring.datasource.username = root
    spring.datasource.password = root

    # Show or not log for each sql query
    spring.jpa.show-sql = true

    # Hibernate ddl auto (create, create-drop, update)
    spring.jpa.hibernate.ddl-auto = update

    # Naming strategy
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

    # Use spring.jpa.properties.* for Hibernate native properties (the prefix is
    # stripped before adding them to the entity manager)

    # The SQL dialect makes Hibernate generate better SQL for the chosen database
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

gradle File

buildscript {
    ext {
        springBootVersion = '1.3.3.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot' 

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.12'
}

I am trying to persist Author Object during the start of application. But it is giving the following null pointer exception.

Error

Exception in thread "main" java.lang.NullPointerException
    at com.example.JpaHibernateApplication.main(JpaHibernateApplication.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Upvotes: 1

Views: 2708

Answers (2)

Sumeet Tiwari
Sumeet Tiwari

Reputation: 371

yes if you use static then it not able to inject that bean that's why you got NullPointerException.

Upvotes: -1

dunni
dunni

Reputation: 44535

Autowiring in static fields doesn't work. Remove the static keyword from your authorRepository field. Also, if you want to execute any code after startup of your Spring Boot application, create a bean which implements the interface CommandLineRunner or ApplicationRunner. Those beans will be executed after your application is started.

Upvotes: 2

Related Questions