自动生成的ID而不是null或默认值为0

时间:2017-10-09 21:50:31

标签: java spring spring-data

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

我使用embaded h2或hsqldb数据库。

我刚刚测试过什么?

  • 我改变了身份策略(我知道AUTO应该与身份相同,但无论如何都要尝试)
  • 我为Long换了很长时间(GET结果:默认值为0,默认值为null)
  • 我读了很多帖子,例如这个答案看起来很稳健similar post answer但我认为我的情况不同(我应该从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);
    }

}

服务:

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

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

祝福, Degath

1 个答案:

答案 0 :(得分:1)

身份策略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);
    }

}