gpt4 book ai didi

spring - 为什么第二级嵌套列表的@NotNull 验证在顶级更新时失败?

转载 作者:行者123 更新时间:2023-12-04 18:28:42 25 4
gpt4 key购买 nike

我的应用程序包含联赛、球员和球队。该应用程序允许客户创建一个联盟,其中包括球队和这些球队的球员。

稍后,客户端可以通过添加带有球员的新球队来修改联赛。正是在这一点上,客户端从我的应用程序中收到了意外的验证错误。该错误表明即使客户端已明确发送属于新团队的新玩家列表,新玩家列表也不能为空。

这里是实体:

@Entity
@Data
public class League {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String name;

@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="leagueId", nullable=false)
@Valid
@NotNull
private List<Team> teams;
}

@Entity
@Data
public class Team {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String name;

@OneToMany(cascade=CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="teamId", nullable=false)
@Valid
@NotNull
private List<Player> players;
}

@Entity
@Data
public class Player {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;

private String name;
}

这是我重现问题的集成测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class LeagueSaveTest {

@Autowired
private LeagueRepository repo;

@Test
public void updateLegaue() {

Player ramsey = new Player();
ramsey.setId(1L);
ramsey.setName("Aaron Ramsey");

Team arsenal = new Team();
arsenal.setId(1L);
arsenal.setName("Arsenal");
arsenal.setPlayers(Arrays.asList(ramsey));

Player hazard = new Player();
hazard.setName("Eden Hazard");

Team chelsea = new Team();
chelsea.setName("Chelsea");
chelsea.setPlayers(Arrays.asList(hazard));

League premier = new League();
premier.setId(1L);
premier.setName("Premier");
premier.setTeams(Arrays.asList(arsenal, chelsea));

repo.save(premier);
}
}

这是我得到的验证错误:

INFO 16136 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test: [DefaultTestContext@557caf28 testClass = LeagueSaveTest, testInstance = com.example.service.LeagueSaveTest@133d0471, testMethod = updateLegaue@LeagueSaveTest, testException = javax.validation.ConstraintViolationException: Validation failed for classes [com.example.domain.Team] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='must not be null', propertyPath=players, rootBeanClass=class com.example.domain.Team, messageTemplate='{javax.validation.constraints.NotNull.message}'}
], mergedContextConfiguration = [WebMergedContextConfiguration@408d971b testClass = LeagueSaveTest, locations = '{}', classes = '{class com.example.LeagueApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@371a67ec, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1a3869f4, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@63440df3, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@569cfc36], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]

为什么我会收到此验证错误,即使所有团队都至少有一名球员?

最佳答案

在团队类中,您声明“id”是自动生成的概念。但是您将该值指定为常量。然后只会抛出错误。

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class LeagueSaveTest {

@Autowired
private LeagueRepository repo;

@Test
public void updateLeague() {

Player ramsey = new Player();
ramsey.setName("Aaron Ramsey");

Team arsenal = new Team();
arsenal.setName("Arsenal");
arsenal.setPlayers(Arrays.asList(ramsey));

Player hazard = new Player();
hazard.setName("Eden Hazard");

Team chelsea = new Team();
chelsea.setName("Chelsea");
chelsea.setPlayers(Arrays.asList(hazard));

League premier = new League()
premier.setName("Premier");
premier.setTeams(Arrays.asList(arsenal, chelsea));

repo.save(premier);
}

关于spring - 为什么第二级嵌套列表的@NotNull 验证在顶级更新时失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56568554/

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