gpt4 book ai didi

java - GET 始终返回 0 id 值。 Spring数据JPA

转载 作者:行者123 更新时间:2023-12-01 17:44:56 25 4
gpt4 key购买 nike

感谢您的及时答复

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

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

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

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

always return zero 0 in id pk

最佳答案

尝试改变

private int id;

private Integer id;

关于java - GET 始终返回 0 id 值。 Spring数据JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60879259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com