Spring数据存储库无法正常工作

时间:2016-04-30 15:30:23

标签: java spring hibernate jpa spring-data-jpa

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文件

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'
}

我正在尝试在应用程序启动期间保留Author Object。但它给出了以下空指针异常。

错误

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)

2 个答案:

答案 0 :(得分:2)

静态字段中的自动装配不起作用。从authorRepository字段中删除static关键字。此外,如果要在启动Spring Boot应用程序后执行任何代码,请创建一个实现接口CommandLineRunnerApplicationRunner的bean。这些bean将在您的应用程序启动后执行。

答案 1 :(得分:-1)

是的,如果你使用静态,那么就无法注入那个为什么你得到NullPointerException的bean。