首页 > 解决方案 > GET 总是返回 0 id 值。春季数据 JPA

问题描述

感谢您的尽快答复

目前我回到了这个项目,我不知道为什么我总是在 GET 请求中得到 0。我附上所有证据。

我的 PK ID 值为零。但这是不正确的。当我在数据库中进行查询时,我会按顺序获得所有正确的 pk 值。

我在PK中选择IDENTITY来实现key自增,同时尊重数据库中的顺序,不污染key。我已经知道我可以选择序列,并在我的数据库中创建序列。

春季启动、春季数据 jpa、postgreSQL

@Getter
@Setter
@ToString
@Entity
@Table(name = "CATALOGS")
public class Catalogs implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) // usar identity para llave autoincr
    @Column(name = "CAT_ID")
    private int id;

    @NotEmpty(message = "CarName must not be empty")
    @Column(name = "CAT_CAR_NAME")
    private String carName;

    @NotEmpty(message = "CarModel must not be empty")
    @Column(name = "CAT_CAR_MODEL")
    private String carModel;

    @NotEmpty(message = "CarYear must not be empty")
    @Column(name = "CAT_CAR_YEAR")
    private int carYear;

    @NotEmpty(message = "CarVersion must not be empty")
    @Column(name = "CAT_CAR_VERSION")
    private String carVersion;

    @NotEmpty(message = "CarPrice must not be empty")
    @Column(name = "CAT_CAR_PRICE")
    private int carPrice;

    @OneToMany(targetEntity = Items.class, mappedBy = "itemId", orphanRemoval = false, fetch = FetchType.LAZY)
    private Set<Items> catItem;

}

@Repository("catalogsRepository")
@Transactional
public interface CatalogsRepository extends CrudRepository<Catalogs, Integer> {

    List<Catalogs> findAll();

    // I can use Repository interface to return Optional
    Optional<Catalogs> findById(Integer id);


}

@Service("serviceCatalogs")
public class ServiceCatalogsImpl implements ServiceCatalogs {

    public static final Logger LOGGER = LoggerFactory.getLogger(ServiceCatalogsImpl.class);

    @Autowired
    @Qualifier("catalogsRepository")
    private CatalogsRepository catsRepo;

    @Override
    public List<CatalogsModel> findAllCatalogs() {
      //CatalogsModel is a BusinessObject to return and avoid to return the DTO directly

        List<Catalogs> catRep = catsRepo.findAll();
        List<CatalogsModel> lsResp = null;

        if (!catRep.isEmpty()) {

            CatalogsModel target = new CatalogsModel();
            lsResp = new ArrayList<>();

            for (Catalogs catSource : catRep) {
                target.setCarModel(catSource.getCarModel());
                target.setCarName(catSource.getCarName());
                target.setCarPrice(catSource.getCarPrice());
                target.setCarVersion(catSource.getCarVersion());
                target.setCarYear(catSource.getCarYear());
                lsResp.add(target);
            }

            lsResp.stream().forEach(System.out::println);

        }
        return lsResp;
    }
APP YML:

server:
    port: 9080

logging:
    level:
        org:
            springframework: ERROR
spring:
    datasource:
        password:***********
        platform: ***********
        url: 'jdbc:postgresql://localhost:5432/***********'
        username: postgres
    jpa:
        hibernate:
            ddl-auto: validate
            naming:
                physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl    
        properties:
            hibernate: {jdbc: {lob: {non_contextual_creation: true}}}

总是在 id pk 中返回零 0

标签: javahibernatespring-bootspring-data-jpaspring-data

解决方案


在 CatalogsModel 你没有设置 id

添加target.setIdCatalog(catSource.getid);

for (Catalogs catSource : catRep) {
                target.setCarModel(catSource.getCarModel());
                target.setCarName(catSource.getCarName());
                target.setCarPrice(catSource.getCarPrice());
                target.setCarVersion(catSource.getCarVersion());
                target.setCarYear(catSource.getCarYear());
                target.setIdCatalog(catSource.getid);
                lsResp.add(target);
}

推荐阅读