我有一个保存对象的方法,但是当我尝试将其保存到方法时,出现错误:
'com.example.pizzacloud.order.JdbcOrderRepository' 中的 saveIngredientRefs(long, java.util.List<com.example.pizzacloud.bind.IngredientRef>)' 不能应用于 '(long, java.util.@javax.validation .constraints.NotNull @javax.validation.constraints.Size(min = 1, message = "你必须至少选择一种成分") List<com.example.pizzacloud.meal.Ingredient>)
我的pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>PizzaCloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>PizzaCloud</name>
<description>PizzaCloud</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.6.10.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
一切都很简单:该方法
saveIngredientRefs
将 , 作为第二个参数List<IngredientRef>
,但在代码List<Ingredient>
中,通过调用getIngredients()
类方法获得的 ,传递给该方法Pizza
。实际上,在错误中,这就是它以纯文本形式显示的内容。
IDE 以红色突出显示代码无法编译的地方。
显然,在将列表传递给方法之前,您需要将对象列表转换为列表
Ingredient
,IngredientRef
如下所示:这种列表转换也可以移动到单独的方法中。