Spring Boot CrudRepository保存错误数据

时间:2018-11-08 15:19:15

标签: java spring-data-jpa derby

我在将数据保存到DB中有问题.Spring Boot是我的新手。当我运行程序时,写入数据的结果是:packagename @ randomcode示例: com.abc。Patient.Patient@6e3e681e

这是我的Entity类-Patient.java

@Entity
public class Patient {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String name;

    // getter, setter, constructor, etc
}

这是我的CrudRepo PatientRepository.java

public interface PatientRepository extends CrudRepository<Patient,Integer> {
}

这是我的服务类PatientService.java

@Service
public class PatientService {

    @Autowired
    private PatientRepository patientRepository;

    public void savePatient (String name) {
        Patient patient = new Patient(name);
        patientRepository.save(patient);
    }

    public Optional<Patient> showPatient(int id) {
        return patientRepository.findById(id);
    }

    public List<Patient> showAllPatients() {
        List<Patient> patients = new ArrayList<>();
        patientRepository.findAll().forEach(patients::add);
        return patients;
    }
}

我认为这一行的savePatient方法中存在该问题:

Patient patients = new Patient(name);

我检查了"name"参数,它使用100%正确的字符串。我正在使用Derby DB。

2 个答案:

答案 0 :(得分:1)

尝试:

public void savePatient(Patient patient) {
    patientRepository.save(patient);
}

答案 1 :(得分:1)

您唯一的问题是如何打印from to value VillageA VillageD 2.982715352 ## VillageD and VillageA VillageB VillageC 2.344160787 ## VillageC and VillageB VillageB VillageD 3.062475396 ## VillageD and VillageB 类。定义适当的Patient或调试一下自己以查看结果字段。您的JPA实施没有问题。

有关默认toString()的详细信息,请参见此question