Arquillian无法部署Override equals和hashcode

时间:2014-10-20 13:55:48

标签: hibernate equals hashcode tomee jboss-arquillian

当我覆盖实体WebArchiceequals方法时,在向hashcode添加实体类时,我的Arquillian无法部署。

环境

  • TomEE 1.7.1
  • arquillian-tomee-embedded 1.7.1
  • arquillian-junit-container 1.1.5.Final
  • org.hibernate 4.1.4.Final

以下是实体类:

@Entity
public class Property implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;
    @Column(name = "PROP_NAME", nullable = false, unique = true)
    private String name;
    private String description;
    private boolean isModifiable;
    @Column(name = "PROP_VALUE", nullable = false)
    private String value;

    public Property() {
    }

    public Property(Long id, String name, boolean isModifiable, String value) {
        this.id = id;
        this.name = name;
        this.isModifiable = isModifiable;
        this.value = value;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isIsModifiable() {
        return isModifiable;
    }

    public void setIsModifiable(boolean isModifiable) {
        this.isModifiable = isModifiable;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final Property other = (Property) obj;
        if (!this.id.equals(other.id)) {
            return false;
        }

        if (!this.name.equals(other.name)) {
            return false;
        }

        if (!this.value.equals(other.value)) {
            return false;
        }

        if ((this.description == null ? other.description != null : !this.description.equals(other.description))) {
            return false;
        }

        return this.isModifiable == other.isModifiable;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 97 * hash + (id == null ? 0 : (int) (this.id ^ (this.id >>> 32)));
        hash = 97 * hash + Objects.hashCode(this.name);
        hash = 97 * hash + Objects.hashCode(this.description);
        hash = 97 * hash + (this.isModifiable ? 1 : 0);
        hash = 97 * hash + Objects.hashCode(this.value);
        return hash;
    }
}

这是我将类添加到WebArchice类的方法。

WebArchive webArchive = ShrinkWrap.create(WebArchive.class);
webArchive.addClasses(Property.class);

当我从我的equals类中删除hashcodeProperty方法时,它会在Arquillian中部署。

如何让Arquillian使用覆盖equalshashcode方法的类?

1 个答案:

答案 0 :(得分:0)

我可以通过以下解决方案让它发挥作用。

覆盖equalshashcode方法时,不直接访问类变量。 使用getter方法从equalshashcode

中访问它们

在我改变了equalshashcode中的变量访问方式之后,Arquillian成功地与该类一起部署。