0

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

Example:

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

Here is my test case:

    // 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);
    }

I'm getting the following exception:

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

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

Felipe Reis
  • 479
  • 8
  • 24

1 Answers1

2

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?

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).

Osman Shoukry
  • 294
  • 2
  • 6
  • Osman, @BusinessIdentity was a typo, I've fixed in my question. I have also included the equals and hashCode methods. – Felipe Reis Oct 10 '13 at 12:59
  • If you annotate your surrogate keys (i.e. primary keys), and hand off the POJO to your framework to persist, the framework will attempt to de-dupe entries, it will do so by calling hashCode(), and equals(), those calls will force BusinessIdentity to try understand how to do equality - which is impossible on a null object. It is best that you annotate something that can't be null in the DB, for example Person's first / last names, or something else that defines uniqueness. – Osman Shoukry Oct 14 '13 at 18:13
  • See: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/persistent-classes.html#persistent-classes-equalshashcode and http://stackoverflow.com/a/2720173/634553 – Osman Shoukry Oct 14 '13 at 18:19