简体   繁体   中英

Auto Generated Id instead of null or default 0

Like in a title, I'm trying to find a way to get auto genrated id when POST resource.

I use embaded h2 or hsqldb database.

What I was already testing?

  • I changed strategy for identity (I know that AUTO should be the same as identity, but tried anyway)
  • I changed long for Long (GET result: default 0 changed for default null)
  • I readed a lot of posts for example this answer looks solid similar post answer but I think my case is different (should I really delete id from POST method?)

Here is my code:

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

}

Service:

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

    }

}

Repository:

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

}

Model:

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

I have no clue what to do. Please share your knowledge.

Best wishes, Degath

Identity Strategy AUTO is correct, you just missing save phone to the database with phoneRepository.save() that's why it didn't generate the ID. All you do above is just adding to the list and retrieve back from the list. Try this:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM