gpt4 book ai didi

java - 自动生成的 ID,而不是 null 或默认 0

转载 作者:行者123 更新时间:2023-12-02 12:09:27 25 4
gpt4 key购买 nike

就像标题一样,我正在尝试找到一种在 POST 资源时获取自动生成的 id 的方法。

我使用嵌入的 h2 或 hsqldb 数据库。

我已经测试了什么?

  • 我更改了身份策略(我知道 AUTO 应该与身份相同,但还是尝试了)
  • 我将 long 更改为 Long(GET 结果:默认 0 更改为默认 null)
  • 我读了很多帖子,例如这个答案看起来很可靠 similar post answer但我认为我的情况有所不同(我真的应该从 POST 方法中删除 id 吗?)

这是我的代码:

Controller :

package spring.degath.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.degath.data.PhoneRepository;
import spring.degath.model.Phone;
import spring.degath.services.PhoneService;
import java.util.List;

@RestController
public class PhoneController {

@Autowired
private PhoneService phoneService;

@Autowired
private PhoneRepository phoneRepository;

@RequestMapping(method = RequestMethod.GET, value = "/phones")
public List<Phone> getAllPhones(){
return phoneService.getAllPhones();
}

@RequestMapping(method = RequestMethod.GET, value = "/phones/{id}")
public Phone getPhone(@PathVariable Long id){
return phoneService.getPhone(id);
}

@RequestMapping(method = RequestMethod.POST, value = "/phones")
public void addPhone(@RequestBody Phone phone){
phoneService.addPhone(phone);
}

}

服务:

package spring.degath.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.degath.data.PhoneRepository;
import spring.degath.model.Phone;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
public class PhoneService {

private List<Phone> phones;

@Autowired
private PhoneRepository phoneRepository;

public List<Phone> getAllPhones(){
return phones;
}

public Phone getPhone(final Long id){
return phoneRepository.findById(id);
}

public void addPhone(Phone phone) {
phones.add(phone);
}

@PostConstruct
private void initDataForTesting() {

phones = new ArrayList<Phone>();

Phone phone1 = new Phone((long) 1,"nokia");
Phone phone2 = new Phone((long) 2,"sony");
Phone phone3 = new Phone((long) 3,"samsung");

phones.add(phone1);
phones.add(phone2);
phones.add(phone3);

}

}

存储库:

package spring.degath.data;
import org.springframework.data.repository.CrudRepository;
import spring.degath.model.Phone;

public interface PhoneRepository extends CrudRepository<Phone, String> {

Phone findById(Long id);

}

型号:

package spring.degath.model;
import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Entity
public class Phone {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY )
@NotNull
private Long id;
private String brand;

public Phone(){
}

public Phone(Long id, String brand) {
super();
this.id = id;
this.brand = brand;
}

public Long getId() {
return id;
}

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

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}
}

我不知道该怎么办。请分享您的知识。

最美好的祝愿,德加斯

最佳答案

身份策略AUTO是正确的,您只是缺少使用phoneRepository.save()将手机保存到数据库,这就是它没有生成ID的原因。您上面所做的只是添加到列表并从列表中检索。试试这个:

@Service
public class PhoneService {

private List<Phone> phones;

@Autowired
private PhoneRepository phoneRepository;

public List<Phone> getAllPhones(){
return phones;
}

public Phone getPhone(final Long id){
return phoneRepository.findById(id);
}

public void addPhone(Phone phone) {
phones.add(phone);
phoneRepository.save(phone);
}

@PostConstruct
private void initDataForTesting() {

phones = new ArrayList<Phone>();

Phone phone1 = new Phone((long) 1,"nokia");
Phone phone2 = new Phone((long) 2,"sony");
Phone phone3 = new Phone((long) 3,"samsung");

phones.add(phone1);
phones.add(phone2);
phones.add(phone3);
phoneRepository.save(phones);
}

}

关于java - 自动生成的 ID,而不是 null 或默认 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46655495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com