【问题标题】:GET is always returning 0 id value. Spring Data JPAGET 总是返回 0 id 值。春季数据 JPA
【发布时间】:2020-03-27 02:43:06
【问题描述】:

感谢您的及时答复

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

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

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

Spring Boot、Spring Data 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 使用可为空的包装类型,因为在保存 ID 之前是“无 ID”。我还要注意,对于您的 findAllCatalogs() 方法到 return empty lists instead of null 之类的方法,通常更加更友好;它不仅使您的方法更易于实现,还可以防止调用者进行无意义的空检查。

标签: java hibernate spring-boot spring-data-jpa spring-data


【解决方案1】:

尝试改变

private int id;

private Integer id;

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      试试这个:

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name = "CAT_ID")
      private int id;
      

      【讨论】:

        猜你喜欢
        • 2016-01-30
        • 2013-11-12
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 2015-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多