在 java Rest api 中,使用 spring boot 和 spring-dta-jpa,db 是 postgresql\n我有一个Book包含bookCopies. 这些图书bookCopies可以是可用的,也可以是不可用的。\n我正在尝试检索具有可用副本数量的图书。\n可以按书名或作者姓名搜索图书。
我设法通过@Transient在我的书籍实体中添加一个字段并在方法@Transient上添加注释来获取信息getNbCopiesAvailable(),但系统要求我使用@Formula注释来完成此操作,但我不知道如何操作。
我实际上收到一个错误:
\n\norg.postgresql.util.PSQLException: ERREUR: plus d\'une ligne renvoy\xc3\xa9e par une sous-requ\xc3\xaate utilis\xc3\xa9e comme une expression \n// which means that several lines are sent by the sub-query\nRun Code Online (Sandbox Code Playgroud)\n\n这是我的实体:
\n\norg.postgresql.util.PSQLException: ERREUR: plus d\'une ligne renvoy\xc3\xa9e par une sous-requ\xc3\xaate utilis\xc3\xa9e comme une expression \n// which means that several lines are sent by …Run Code Online (Sandbox Code Playgroud) 我对此感到疯狂......我关注了Thoughts on Java和Vlad mihalcea关于hibernate中映射关联的所有文章,但我不知道为什么在@ManyToMany中删除,在我的关于Rest服务的项目中总是搞砸了使用 Spring Data Jpa。
我有一本基本的书->作者多关系,我认为我映射正确(至少根据他们在引用的两篇参考文献中的推荐方式)。
我重写了 equals 和 hashcode,我编写并使用这些方法来管理实体之间的关联,但是当我尝试从作者中删除书籍(我想删除)时,我得到了 ConcurrentModificationException: null,即使我使用迭代器。
我的实体:
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Size( min = 2, max = 30)
@Column (length = 30)
private String firstName;
@Size( min = 2, max = 30)
@Column (length = 30)
private String lastName;
@ManyToMany(mappedBy = "authors")
private Set<Book> books = new HashSet<>();
public Author(@NotNull @Size(min = 2, max = 30) String firstName, @NotNull @Size(min …Run Code Online (Sandbox Code Playgroud) 编辑
由于上面 Chellappan 的回答,问题的第一部分得到了解决,但我遇到了其他问题,这让我做了一个 stackblitz,可以在这里找到: https ://stackblitz.com/edit/angular-ivy-bzh3dh
问题是我无法正确检索从表单中选择的产品列表,因为它们全部输出:
"courses": [
{
"courseName": "Dessert",
"products": [
{
"product": true
},
{
"product": false
},
{
"product": null
}
]
Run Code Online (Sandbox Code Playgroud)
我不知道如何能够检索类似的内容:
"courses": [
{
"courseName": "Dessert",
"products": [
{
"Crumble aux pommes": true
},
{
"Mousse au chocolat maison": false
},
{
"Riz au lait": false
}
]
Run Code Online (Sandbox Code Playgroud)
第二个问题是我的复选框的标签不是独立的。如果我添加新的“课程”并选择一些产品,其他课程中所有复选框的标签也会更改。
我希望我的帖子不要太乱,我不会经常在 Stack Overflow 上编辑消息
编辑结束
我有一个带有嵌套表单数组的反应式表单来描述包含多个课程的菜单,每个课程包含多个产品,但我不知道如何使其工作。我的实际问题是我无法检索第二级控件。
这是菜单的 json 结构:
{
"name": "",
"description": "",
"price": "",
"courses": [
{
"courseName": "",
"products": [ …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的 Spring Boot 休息控制器,以检查在 bean 验证失败时请求是否发送正确的错误。
\n\n我有一个@RestController:
\n\n@RestController\n@RequestMapping("/restaurants")\npublic class RestaurantsApiController {\n\n private final RestaurantService restaurantService;\n private final ProductRepository productRepository;\n private final ProductMapper productMapper;\n\n public RestaurantsApiController(RestaurantService restaurantService, ProductRepository productRepository, ProductMapper productMapper) {\n this.restaurantService = restaurantService;\n this.productRepository = productRepository;\n this.productMapper = productMapper;\n }\n\n @PostMapping("{id}/products")\n public ResponseEntity<ProductDto> addProduct(@PathVariable Long id,\n @Valid @RequestBody ProductDto productDto){\n Product product = this.restaurantService.addProduct(id, productMapper.productDtoToProduct(productDto));\n return new ResponseEntity<>(productMapper.productToProductDto(product), HttpStatus.CREATED);\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我有一个带有 @ControllerAdvice 注释的自定义异常处理程序:
\n\n@ControllerAdvice\npublic class ExceptionControllerAdvice {\n @ExceptionHandler({MethodArgumentNotValidException.class})\n public ResponseEntity<Object> validationException(MethodArgumentNotValidException ex, WebRequest request) {\n …Run Code Online (Sandbox Code Playgroud) 我有一个角度模板驱动的表单,当我单击产品时,我想在其中填充值。“description”和“imageUrl”字段可能未定义,如果我不处理它,就会破坏我的表单。我设法用这个解决方案做到了:
private initFormEdit() {
this.product = this.productService.fetchProduct(this.restaurantId, this.productId);
// I'm using a ternaty operator to put a default value if objects are empty
const description = this.product.description ? this.product.description : '';
const imageUrl = this.product.imageUrl ? this.product.imageUrl : '';
this.productForm.setValue({
name: this.product.name,
category: this.product.category,
description,
price: this.product.price,
imageUrl
});
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单或更干净的方法来做到这一点?谢谢 !
java ×3
typescript ×2
angular ×1
formarray ×1
hibernate ×1
junit5 ×1
mockmvc ×1
rest ×1
spring ×1
spring-boot ×1