【问题标题】:Arquillian Unable to deploy Override equals and hashcodeArquillian 无法部署覆盖等于和哈希码
【发布时间】:2014-10-20 13:55:48
【问题描述】:

当我重写实体 equalshashcode 方法时,我的 Arquillian 无法部署实体类到 WebArchice

环境

  • 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;
    }
}

这就是我将类添加到WebArchiceclass 的方式。

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

当我从 Property 类中删除 equalshashcode 方法时,它会部署在 Arquillian 中。

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

【问题讨论】:

    标签: hibernate equals hashcode apache-tomee jboss-arquillian


    【解决方案1】:

    解决方案如下,我让它工作。

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

    在我更改了从 equalshashcode 中访问变量的方式后,Arquillian 成功部署了该类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 2014-08-07
      相关资源
      最近更新 更多