java - 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}}}
解决方案
在 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);
}
推荐阅读
- android - 我正在尝试构建一个 Android 应用程序以使用 Kotlin 连接到 HC-05 蓝牙设备,但我无法连接
- java - SSL 证书问题 msgrapgh sdk java
- r - 变化点检测
- android - 为什么 Native Advanced 广告从回收站视图 android 中删除了该项目?
- react-native - React Native 错误:“变量”是只读的
- javascript - 自动移动特定日期的数据
- mysql - 在mysql中获得第二高的总和
- spring-boot - 如何在JPA Springboot中映射两个表,如主键和外键?
- php - Symfony 5.1 目录不存在错误
- sql - 如何使用另一个表中的值更新数据库表