有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java尝试使用多个关系添加投票

在play framework web应用程序中实现投票功能时遇到了一些问题

目前,我已经在课堂上定义了这些变量,这些变量应该有一个投票“分数”

@ManyToMany(cascade=CascadeType.REMOVE)
public List<User> upVotes = new ArrayList<User>();

@ManyToMany(cascade=CascadeType.REMOVE)
public List<User> downVotes = new ArrayList<User>();

在同一个类中,我还实现了一些方法,以便用户可以切换他们的投票

public void toggleUpVote(User user){
    if(user == null){
        // Do nothing
        Logger.debug("Vote: User is null!");
    }
    else if(upVotes.contains(user)){
        Logger.debug("Vote: removed upvote!");
        upVotes.remove(user);
        this.saveManyToManyAssociations("upVotes");
    } else {
        if(downVotes.contains(user)){
            Logger.debug("Vote: removed old downvote!");
            downVotes.remove(user);
            this.saveManyToManyAssociations("downVotes");
        }
        Logger.debug("Vote: Added upvote!");
        upVotes.add(user);
        this.saveManyToManyAssociations("upVotes");
    }
    Logger.debug("Uservotestatus: " + getVoteStatus(user));
    this.save();
}

public void toggleDownVote(User user){
    if(user == null){
        // Do nothing
        Logger.debug("Vote: User is null!");
    } else if(downVotes.contains(user)){
        Logger.debug("Vote: removed downvote!");
        downVotes.remove(user);
        this.saveManyToManyAssociations("downVotes");
    } else {
        if(upVotes.contains(user)){
            Logger.debug("Vote: removed old upvote!");
            upVotes.remove(user);
            this.saveManyToManyAssociations("upVotes");
        }
        Logger.debug("Vote: Added downvote!");
        downVotes.add(user);
        this.saveManyToManyAssociations("downVotes");
    }
    Logger.debug("Uservotestatus: " + getVoteStatus(user));
    this.save();
}

public int getVoteStatus(User user){
    if(upVotes.contains(user)){
        return 1;
    } else if (downVotes.contains(user)) {
        return -1;
    } else {
        return 0;
    }
}

public int getScore(){
    Logger.debug("upVotes: " + upVotes.size());
    Logger.debug("downVotes: " + downVotes.size());

    Logger.debug("upvotes: "+ upVotes + "downvotes:" + downVotes);
    return (upVotes.size() - downVotes.size());

似乎每当我把一个用户添加到向上投票列表时,它也会被添加到向下投票列表。投票功能似乎运行良好,我还测试了模型,以确保所有功能正常。(测试工作正常,没有问题)我潜入创建表的sql文件,只能找到以下多个关系:

create table note_user (
  note_id                        bigint not null,
  user_id                        varchar(255) not null,
  constraint pk_note_user primary key (note_id, user_id))
;

为什么不创建两个表?我必须以某种方式命名多对多关系吗

我试着寻找通过注释命名表的方法,但没有找到任何解决方案


共 (0) 个答案