"含有"使用Set的方法即使在覆盖equals方法

时间:2017-06-06 14:17:13

标签: java spring equals hashcode

有一个实体:

@Entity
@Table(name = "menu")
public class Menu {

    @Id
    @SequenceGenerator(name="s_menu", sequenceName="s_menu", allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="s_menu")
    @Column(name = "menu_id")
    private int id;

    @Column(name = "gmnu_code")
    private Integer groupeMenu;

    @Column(name = "menu_lib")
    private String lib;

    @Column(name = "menu_ordre")
    private Integer ordre;

    @Column(name = "menu_visible")
    private Integer visible;

    @Column(name = "menu_deleted")
    private Integer deleted;

    @Column(name = "menu_bouton")
    private Integer bouton;

    @Column(name = "menu_parent")
    private Integer parent;

    @Column(name = "menu_controlleur")
    private String controlleur;

    @Column(name = "menu_navigation")
    private String navigation;

    @Column(name = "menu_flag_trace")
    private Integer flag_trace;

    @Column(name = "menu_trace")
    private String trace;

    @ManyToMany(fetch = FetchType.EAGER, mappedBy = "menus")
    @JsonBackReference
    private Set<Role> roles = new HashSet<Role>();

    @Transient
    private int depth;

    @Transient
    private boolean affectedToARole;

    @Transient
    private Environment env;

    public Menu() {
        super();
    }

    public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace) {
        super();
        this.groupeMenu = gmnu_code;
        this.lib = menu_lib;
        this.ordre = menu_ordre;
        this.visible = menu_visible;
        this.deleted = menu_deleted;
        this.bouton = menu_bouton;
        this.parent = menu_parent;
        this.controlleur = menu_controlleur;
        this.navigation = menu_navigation;
        this.flag_trace = menu_flag_trace;
        this.trace = menu_trace;
    }

    public Menu(Integer gmnu_code, String menu_lib, Integer menu_ordre, Integer menu_visible, Integer menu_deleted, Integer menu_bouton, Integer menu_parent, String menu_controlleur, String menu_navigation, Integer menu_flag_trace, String menu_trace, Set<Role> roles) {
        super();
        this.groupeMenu = gmnu_code;
        this.lib = menu_lib;
        this.ordre = menu_ordre;
        this.visible = menu_visible;
        this.deleted = menu_deleted;
        this.bouton = menu_bouton;
        this.parent = menu_parent;
        this.controlleur = menu_controlleur;
        this.navigation = menu_navigation;
        this.flag_trace = menu_flag_trace;
        this.trace = menu_trace;
        this.roles = roles;
    }

    public int getId() {
        return id;
    }

    public void setId(int menu_id) {
        this.id = menu_id;
    }

    public Integer getGroupeMenu() {
        return groupeMenu;
    }

    public void setGroupeMenu(Integer gmnu_code) {
        this.groupeMenu = gmnu_code;
    }

    public String getLib() {
        if (this.env != null)
            return env.getProperty(lib.trim());
        return lib;
    }

    public void setLib(String menu_lib) {
        this.lib = menu_lib;
    }

    public Integer getOrdre() {
        return ordre;
    }

    public void setOrdre(Integer menu_ordre) {
        this.ordre = menu_ordre;
    }

    public Integer getVisible() {
        return visible;
    }

    public void setVisible(Integer menu_visible) {
        this.visible = menu_visible;
    }

    public Integer getDeleted() {
        return deleted;
    }

    public void setDeleted(Integer menu_deleted) {
        this.deleted = menu_deleted;
    }

    public Integer getBouton() {
        return bouton;
    }

    public void setBouton(Integer menu_bouton) {
        this.bouton = menu_bouton;
    }

    public Integer getParent() {
        return parent;
    }

    public void setParent(Integer menu_parent) {
        this.parent = menu_parent;
    }

    public String getControlleur() {
        return controlleur;
    }

    public void setControlleur(String menu_controlleur) {
        this.controlleur = menu_controlleur;
    }

    public String getNavigation() {
        try {
            if (this.env != null) 
                return env.getProperty(navigation.trim());
            return navigation.trim();
        } catch (NullPointerException npe) {
            return " --- ";
        }
    }

    public void setNavigation(String menu_navigation) {
        this.navigation = menu_navigation;
    }

    public String getTrace() {
        if (this.env != null)
            return HtmlUtils.htmlUnescape(env.getProperty(trace));
        return trace;
    }

    public void setTrace(String menu_trace) {
        this.trace = HtmlUtils.htmlUnescape(menu_trace);
    }

    public Integer getFlag_trace() {
        return flag_trace;
    }

    public void setFlag_trace(Integer menu_flag_trace) {
        this.flag_trace = menu_flag_trace;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public void setEnv(Environment env) {
        this.env = env;
    }

    public int getDepth() {
        return depth;
    }

    public void setDepth(int depth) {
        this.depth = depth;
    }

    public boolean isAffectedToARole() {
        return affectedToARole;
    }

    public void setAffectedToARole(boolean affectedToARole) {
        this.affectedToARole = affectedToARole;
    }

    @Override
    public boolean equals(Object obj) { // pour pouvoir utiliser la méthode "contains" sur une liste de Menu

        if (this == null && obj == null)
            return true;

        if (this != null && obj != null && obj instanceof Menu)
            return id == ((Menu)obj).getId();

        return super.equals(obj);

    }

}

我想测试一组菜单是否包含特定菜单:

Set<Menu> affected_menus = roleDao.getListMenu(role_code); // gets all menus affected to a role_code

for (Menu menu : temp) {
    if (!affected_menus.isEmpty() && affected_menus.contains(menu))
        menu.setAffectedToARole(true);
    else
        menu.setAffectedToARole(false);
}

在运行时,所有代码都转到menu.setAffectedToARole(false);

那有什么不对?

3 个答案:

答案 0 :(得分:1)

您还应该覆盖hashCode()方法

这里有一些信息:Why do I need to override the equals and hashCode methods in Java?

答案 1 :(得分:1)

int hashCode()类的Object方法的javadoc解释了:

  

它返回对象的哈希码值。支持此方法   为了哈希表的好处,例如提供的哈希表   java.util.HashMap中。

hashCode()方法意图很明确:在依赖哈希表的集合中使用:HashMapHashSetLinkedHashMap等......

此外,hashcode()的一般合同提到equals()hashcode()之间的关系:

  

如果两个对象根据equals(Object)方法相等,那么   必须在两个对象中的每一个上调用hashCode方法   相同的整数结果。

为了能够将自定义类与HashSet一起使用,您需要以一致的方式覆盖equals()hashcode()

答案 2 :(得分:1)

覆盖equals时,您必须始终覆盖hashCode。这两种方法在Java Collections等中一起使用。

在您的情况下,此实现可能就足够了:

@Override
public int hashCode() {
    return id;
}

此外,您的equals实施也可以使用一些帮助:

@Override
public boolean equals(Object obj) {
    if (obj instanceof Menu)
        return id == ((Menu)obj).getId();
    return false;
 }