Java @spring boot中有多少关系?

Java @spring boot中有多少关系?,java,hibernate,spring-boot,Java,Hibernate,Spring Boot,我的“用户”类中有以下行: @ManyToMany(mappedBy=“members”) 私有列表社区=新的ArrayList(); 在“社区”课程中,我有以下几行: @ManyToMany(cascade=CascadeType.ALL) @可接合 非公开名单成员; 我正试图让它,让一个用户可以“创建”一个社区,然后让该用户作为所说的新社区的成员出现 这是我创建社区的代码 public ResponseEntity<?> newCommunity(@Valid @Reques

我的“用户”类中有以下行:

@ManyToMany(mappedBy=“members”)
私有列表社区=新的ArrayList();
在“社区”课程中,我有以下几行:

@ManyToMany(cascade=CascadeType.ALL)
@可接合
非公开名单成员;
我正试图让它,让一个用户可以“创建”一个社区,然后让该用户作为所说的新社区的成员出现

这是我创建社区的代码

public ResponseEntity<?> newCommunity(@Valid @RequestBody NewCommunityRequestPayload newCommunityrequest) {

    // Get current user.
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    // New list of users here, since it is a new community.
    List<User> members = new ArrayList<>();

    User currentUser;
    if (principal instanceof User){
        currentUser = ((User)principal);

        // Add this user to the member list.
        members.add(currentUser);

        // create a new Community object.
        Community newCommunity = new Community();

        // Set the name, and set the member list.
        newCommunity.setCommunityName(newCommunityRequest.getCommunityName());
        newCommunity.setMembers(members);

        Community result = communityRepository.save(newCommunity);

        return new ResponseEntity(new ApiResponse(true, "Created new community."),
                HttpStatus.CREATED);

    }
    return new ResponseEntity(new ApiResponse(false, "Invalid user object"),
                HttpStatus.BAD_REQUEST);
}
public ResponseEntity newCommunity(@Valid@RequestBody NewCommunityRequestPayload newCommunityrequest){
//获取当前用户。
对象主体=SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//这里有新的用户列表,因为它是一个新的社区。
列表成员=新的ArrayList();
用户当前用户;
if(用户的主体实例){
当前用户=((用户)主体);
//将此用户添加到成员列表。
添加(当前用户);
//创建一个新的社区对象。
社区新社区=新社区();
//设置名称,然后设置成员列表。
newCommunity.setCommunityName(newCommunityRequest.getCommunityName());
新社区。集合成员(成员);
社区结果=communityRepository.save(新社区);
return new ResponseEntity(新的ApiResponse(true,“创建新社区”),
HttpStatus.CREATED);
}
返回新的ResponseEntity(新的ApiResponse(false,“无效的用户对象”),
HttpStatus.BAD_请求);
}

如前所述,我正试图让它成为一个用户可以“创建”一个社区,然后让该用户作为所述新社区的成员出现。。。当我保存上面的社区时,它被保存在db中,hibernate为关系生成了一个表,但其中没有用于该关系的行。我该如何解决这个问题?

请添加您为Project添加的表的数量您可以指定您的问题吗?@PoojaAggarwal Omg,我已经筋疲力尽,忘记添加真正的问题了。。。我现在就去!也许这个链接会帮助你:@PauloPedroso-是的。。。这就是我开始编写代码的地方。这不起作用。请添加您为项目添加的表的数量。您可以指定您的问题吗?@PoojaAggarwal Omg,我已经筋疲力尽,忘记添加真正的问题了。。。我现在就去!也许这个链接会帮助你:@PauloPedroso-是的。。。这就是我开始编写代码的地方。它不起作用。
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable
private List<User> members;
public ResponseEntity<?> newCommunity(@Valid @RequestBody NewCommunityRequestPayload newCommunityrequest) {

    // Get current user.
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

    // New list of users here, since it is a new community.
    List<User> members = new ArrayList<>();

    User currentUser;
    if (principal instanceof User){
        currentUser = ((User)principal);

        // Add this user to the member list.
        members.add(currentUser);

        // create a new Community object.
        Community newCommunity = new Community();

        // Set the name, and set the member list.
        newCommunity.setCommunityName(newCommunityRequest.getCommunityName());
        newCommunity.setMembers(members);

        Community result = communityRepository.save(newCommunity);

        return new ResponseEntity(new ApiResponse(true, "Created new community."),
                HttpStatus.CREATED);

    }
    return new ResponseEntity(new ApiResponse(false, "Invalid user object"),
                HttpStatus.BAD_REQUEST);
}