Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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 自动生成的Id,而不是null或默认值0_Java_Spring_Spring Data - Fatal编程技术网

Java 自动生成的Id,而不是null或默认值0

Java 自动生成的Id,而不是null或默认值0,java,spring,spring-data,Java,Spring,Spring Data,就像在一个标题中,我试图找到一种在发布资源时自动生成id的方法 我使用EMBA h2或hsqldb数据库 我已经在测试什么了 我改变了标识策略(我知道AUTO应该与identity相同,但还是尝试了) 我更改了很长时间(获取结果:默认值0更改为默认值null) 我读了很多文章,例如这个答案看起来很可靠,但我认为我的情况不同(我真的应该从POST方法中删除id吗?) 这是我的密码: 控制器: package spring.degath.controller; import org.sprin

就像在一个标题中,我试图找到一种在发布资源时自动生成id的方法

我使用EMBA h2或hsqldb数据库

我已经在测试什么了

  • 我改变了标识策略(我知道AUTO应该与identity相同,但还是尝试了)
  • 我更改了很长时间(获取结果:默认值0更改为默认值null)
  • 我读了很多文章,例如这个答案看起来很可靠,但我认为我的情况不同(我真的应该从POST方法中删除id吗?)
这是我的密码:

控制器:

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);
    }

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

致以最良好的祝愿,
Degath

身份策略自动正确,您只是缺少了使用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);
    }

}
@服务
公共类电话服务{
私人名单电话;
@自动连线
私人电话储存库;
公共列表getAllPhones(){
归还电话;
}
公用电话getPhone(最终长id){
返回phoneRepository.findById(id);
}
公用电话(电话){
电话。添加(电话);
phoneRepository.save(电话);
}
@施工后
私有void initDataForTesting(){
phones=新的ArrayList();
Phone phone1=新手机((长)1,“诺基亚”);
Phone phone2=新手机((长)2,“索尼”);
Phone phone3=新手机((长)3,“三星”);
电话。添加(电话1);
电话。添加(电话2);
电话。添加(电话3);
phoneRepository.save(电话);
}
}
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;
    }
}
@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);
    }

}