Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么hibernate无法保存?_Java_Hibernate_Spring Boot_Flyway - Fatal编程技术网

Java 为什么hibernate无法保存?

Java 为什么hibernate无法保存?,java,hibernate,spring-boot,flyway,Java,Hibernate,Spring Boot,Flyway,我正在使用Flyway尝试创建一个数据库,然后在SpringBootWeb应用程序启动时为其种子。Flyway在第一次迁移中成功创建了数据库表,但在第二次迁移中由于NullPointerException而无法填充它们 以下是迁移代码V2_seed_database.java,位于包db.migration中: package db.migration; import org.flywaydb.core.api.migration.BaseJavaMigration; import org.f

我正在使用Flyway尝试创建一个数据库,然后在SpringBootWeb应用程序启动时为其种子。Flyway在第一次迁移中成功创建了数据库表,但在第二次迁移中由于NullPointerException而无法填充它们

以下是迁移代码V2_seed_database.java,位于包db.migration中:

package db.migration;

import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

import net.tekknow.medaverter.db.seeds.AppointmentSeeder;

public class V2__seed_database extends BaseJavaMigration {
    public void migrate(Context context) {
        AppointmentSeeder appointmentSeeder = new AppointmentSeeder();
        appointmentSeeder.seed();
    }
}
以下是任命播种机代码:

package net.tekknow.medaverter.db.seeds;

import org.json.JSONArray; 
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.tekknow.medaverter.domain.Appointment;
import net.tekknow.medaverter.service.AppointmentService;

@Service
public class AppointmentSeeder {
    @Autowired
    AppointmentService appointmentService;

    @PostConstruct
    public void seed() {
        String json = "[" +
            "{\"id\":1,\"patient_id\":1,\"dateTime\":\"10/29/2010\",\"physician_id\":1,\"lab_id\":1,\"note_id\":0}" +
        "]";
        org.json.JSONArray appointments = new JSONArray(json);

        for (int i=0; i<appointments.length(); i++) {
            JSONObject appointment = appointments.getJSONObject(i);
            Appointment dbAppointment = new Appointment();
            dbAppointment.setId(appointment.getInt("id"));
            dbAppointment.setPatientId(appointment.getInt("patient_id"));
            dbAppointment.setDateTime(appointment.getString("dateTime"));
            dbAppointment.setPhysicianId(appointment.getInt("physician_id"));
            dbAppointment.setLabId(appointment.getInt("lab_id"));
            dbAppointment.setNoteId(appointment.getInt("note_id"));
            appointmentService.save(dbAppointment);
        }
    }
}   
以下是预约bean:

package net.tekknow.medaverter.domain;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Entity
@Table(name = "appointments")
public class Appointment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    @NotBlank
    @Column(unique = true)
    private int patient_id;
    @Size(max = 32)
    private String date_time;
    private int physician_id;
    private int lab_id;
    private int note_id;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
... other getters and setters, truncated for brevity
当我运行程序时,迁移开始,但在这一行失败:

    repo.save(appointment);  //its failing here
显示以下错误消息:

原因:java.lang.NullPointerException:null net.teknow.medverter.service.AppointmentService.save(AppointmentService.java:32) ~[classes/:na]

就在失败的代码行之前,我输出了表示为null的对象的内容,以及表示为notnull的对象的内容:

AppointmentService.save:appointment=id:1,patient\u id:1, 日期时间:2010年10月29日,医师id:1,实验室id:1,备注id:0


有什么建议吗?

也许您应该将JSON文件中**note\u id**的值更改为1

这与hibernate无关,您使用spring依赖项注入的方式不正确:您试图自动连接静态字段,这是不允许的

更新

现在你有了

AppointmentService appointmentService = new AppointmentService();
而不是将这片土地注入播种机

如果您想要依赖注入工作,Seeder需要是一个Spring管理的bean(
@Service
)。另请参见
@PostConstruct
,您可以使用它在bean初始化后调用方法

更新2

您仍然使用
new
自己实例化Springbean,因此没有注入依赖项

AppointmentSeeder appointmentSeeder = new AppointmentSeeder();
然而,您已经成功地将问题推向了Flyway。现在的问题是Flyway迁移不是SpringBean:它们是由Flyway而不是Spring创建的,因此它们的依赖项不会由Spring自动连接

Flyway4.1通过允许使用预先存在的Springbeans(或其他Java对象)和一些配置来解决这个问题


另请参见和javadoc。

在AppointmentSeeder中执行此操作

@Autowire
AppointmentService service;

public void seed() {
        String json = "[" +
            "{\"id\":1,\"patient_id\":1,\"dateTime\":\"10/29/2010\",\"physician_id\":1,\"lab_id\":1,\"note_id\":0}" +
        "]";
        org.json.JSONArray appointments = new JSONArray(json);

        for (int i=0; i<appointments.length(); i++) {
            JSONObject appointment = appointments.getJSONObject(i);
            Appointment dbAppointment = new Appointment();
            dbAppointment.setId(appointment.getInt("id"));
            dbAppointment.setPatientId(appointment.getInt("patient_id"));
            dbAppointment.setDateTime(appointment.getString("dateTime"));
            dbAppointment.setPhysicianId(appointment.getInt("physician_id"));
            dbAppointment.setLabId(appointment.getInt("lab_id"));
            dbAppointment.setNoteId(appointment.getInt("note_id"));
            service.save(dbAppointment);
        }
    }
@Autowire
任命服务;
公开无效种子(){
字符串json=“[”+
“{\'id\':1,\'patient\'U id\':1,\'dateTime\':\'10/29/2010\',\'Medical\'U id\':1,\'lab\'U id\':1,\'note\'U id\':0}”+
"]";
org.json.JSONArray约会=新的JSONArray(json);

对于(inti=0;iI)最初不是这样的。Eclipse提供它作为“快速修复”错误“无法对非静态字段repo进行静态引用”。那么我应该在intead中做什么呢?您可能只有
repo
字段作为实例字段,但是
save
方法是静态的。不可能从静态方法访问实例字段(因此使用QuickFix)。您需要将字段和方法都设置为非静态,但save方法是从public interface AppointRepository Extendes JpaRepository{}继承的。JpaRepository是Spring Boot(或Hibernate?)代码,不是我的。啊,当然可以。很抱歉,我错过了。谢谢。我拿走了它,现在出现了下一个错误“org.springframework.beans.factory.BeanCreationException:创建名为'appController'的bean时出错,该bean在文件[C:\Users\Greg\Projects\MedAverter\target\classes\net\Teknow\MedAverter\controller\appController.class]中定义]:合并bean定义的后处理失败;嵌套异常为java.lang.IllegalStateException:未能内省类[net.Teknow.medaverter.controller.AppController],原因是:java.lang.NoClassDefFoundError:Lnet/Teknow/medaverter/service/AppointmentService;“我想你解决了这个问题,所以我会给你积分。谢谢!我把它改成了1,但是@Autowired没有区别。但是这也不起作用。仍然得到相同的错误”原因是:java.lang.NullPointerException:null at net.teknow.medverter.service.AppointService.save(AppointService.java:32)~[classes/:na]”我用你的建议修改了主帖子中显示的代码。
AppointmentSeeder appointmentSeeder = new AppointmentSeeder();
ApplicationContext applicationContext = ...; // obtain a reference to Spring's ApplicationContext.

Flyway flyway = Flyway.configure()
    .dataSource(url, user, password)
    // Add all Spring-instantiated JavaMigration beans
    .javaMigrations(applicationContext.getBeansOfType(JavaMigration.class).values().toArray(new JavaMigration[0]))
    .load();
flyway.migrate();
@Autowire
AppointmentService service;

public void seed() {
        String json = "[" +
            "{\"id\":1,\"patient_id\":1,\"dateTime\":\"10/29/2010\",\"physician_id\":1,\"lab_id\":1,\"note_id\":0}" +
        "]";
        org.json.JSONArray appointments = new JSONArray(json);

        for (int i=0; i<appointments.length(); i++) {
            JSONObject appointment = appointments.getJSONObject(i);
            Appointment dbAppointment = new Appointment();
            dbAppointment.setId(appointment.getInt("id"));
            dbAppointment.setPatientId(appointment.getInt("patient_id"));
            dbAppointment.setDateTime(appointment.getString("dateTime"));
            dbAppointment.setPhysicianId(appointment.getInt("physician_id"));
            dbAppointment.setLabId(appointment.getInt("lab_id"));
            dbAppointment.setNoteId(appointment.getInt("note_id"));
            service.save(dbAppointment);
        }
    }