首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么二级嵌套列表的@NotNull验证在顶级更新时失败?

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

Stack Overflow用户
提问于 2019-06-13 02:43:20
回答 1查看 116关注 0票数 3

我有申请,有联盟,球员和团队。该应用程序允许客户创建一个与这些球队和球员的联盟。

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

以下是实体:

代码语言:javascript
复制
@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;    
}

下面是我的集成测试,它再现了这个问题:

代码语言:javascript
复制
@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);
    }   
}

下面是我得到的验证错误:

代码语言:javascript
复制
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]]

为什么即使所有球队都至少有一名球员,我还是会得到这个验证错误?

EN

回答 1

Stack Overflow用户

发布于 2019-06-13 12:56:47

在Team类中,您声明"id“是自动生成的概念。但是您将该值赋值为constant.Then,只抛出错误。

代码语言:javascript
复制
@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);
}   

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56568554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档