Openpojo引发BusinessException

Openpojo引发BusinessException

本文介绍了当POJO引用另一个POJO时,Openpojo引发BusinessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenPojo自动化对我的JPA实体的测试.我遇到了引用其他实体的实体的问题.

I'm using OpenPojo to automate tests on my JPA Entities. I'm having troubles with entities that have reference to other entities.

示例:

public class Person {
    @BusinessKey
    private Integer id;

    ...getters/setters

    @Override
    public boolean equals(Object obj) {
            return BusinessIdentity.areEqual(this, obj);
    }

    @Override
    public int hashCode() {
            return BusinessIdentity.getHashCode(this);
    }
}


public class Employee {
    @BusinessKey
    private Integer id;

    private Person person;

    ...getters/setters

    @Override
    public boolean equals(Object obj) {
            return BusinessIdentity.areEqual(this, obj);
    }

    @Override
    public int hashCode() {
            return BusinessIdentity.getHashCode(this);
    }
}

这是我的测试用例:

    // Create Rules to validate structure for POJO_PACKAGE
    pojoValidator.addRule(new NoPublicFieldsRule());
    pojoValidator.addRule(new NoPrimitivesRule());
    pojoValidator.addRule(new NoStaticExceptFinalRule());
    pojoValidator.addRule(new GetterMustExistRule());
    pojoValidator.addRule(new SetterMustExistRule());
    pojoValidator.addRule(new NoNestedClassRule());

    // Create Testers to validate behaviour for POJO_PACKAGE
    pojoValidator.addTester(new DefaultValuesNullTester());
    pojoValidator.addTester(new SetterTester());
    pojoValidator.addTester(new GetterTester());

    for (PojoClass pojoClass : pojoClasses) {
        pojoValidator.runValidation(pojoClass);
    }

我遇到以下异常:

com.openpojo.business.exception.BusinessException: Field required and can't be null [PojoFieldImpl

如果我从Employee类中删除对Person的引用,则不会引发任何异常.

If I remove the reference to Person from the Employee class the tests without any exception being thrown.

推荐答案

除非您使用注释"@BusinessKey",否则您将列出@BusinessIdentity,否则OpenPojo不会引发此异常.另外,您没有显示您的equals和hashCode或toString实现,在何处引用了"BusinessIdentity"?

OpenPojo doesn't throw this exception unless you use the annotation "@BusinessKey", you are listing @BusinessIdentity. Also you aren't showing your equals and hashCode or toString implementations, where is "BusinessIdentity" being referenced?

需要提防的另一件事是,@ BusinessKey应该用于注释实际的业务字段,而不是您的数据库代理ID(又称为主键).

Another thing to watch out for, @BusinessKey is supposed to be used to annotate actual business fields not your DB surrogate Id (aka Primary Key).

这篇关于当POJO引用另一个POJO时,Openpojo引发BusinessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:22