Base implements Serializable, Identifiable, SecuredObject { @Override public boolean equals(Object other) { if (this == other) { return true; } if (other == null) { return false; } if ((other.getClass().equals(this.getClass()))) { Base base = (Base) other; if (this.getIdentifier() == null && base.getIdentifier() == null) { if (this.getId() != null && base.getId() != null) { return Objects.equals(this.getId(), base.getId()); } else { return false; } } else { return Objects.equals(this.identifier, base.identifier); } } else if (HibernateProxyHelper.getClassWithoutInitializingProxy(other).equals(this.getClass())) { Identifiable base = (Identifiable) other; if (this.getIdentifier() == null && base.getIdentifier() == null) { return false; } else { return Objects.equals(this.identifier, base.getIdentifier()); } } else { return false; } }  @Override boolean equals(Object other); @Override int hashCode();  }
@Test public final void testEqualsWithNullIdentifiers() { b2.setIdentifier(null); b1.setIdentifier(null); assertFalse("Equals should return false", b1.equals(b2)); }
@Test public final void testEqualsWithHibernateProxies() throws Exception { b2.setIdentifier("test"); b2.setId(1L); b1.setIdentifier("test"); b1.setId(1L); SessionImplementor sessionImplementor = EasyMock .createMock(SessionImplementor.class); SessionFactoryImplementor sessionFactoryImplementor = EasyMock .createMock(SessionFactoryImplementor.class); EntityPersister entityPersister = EasyMock .createMock(EntityPersister.class); PersistenceContext persistenceContext = EasyMock .createMock(PersistenceContext.class); EasyMock.expect(sessionImplementor.getFactory()) .andReturn(sessionFactoryImplementor).anyTimes(); EasyMock.expect( sessionFactoryImplementor.getEntityPersister((String) EasyMock .eq("Annotation"))).andReturn(entityPersister) .anyTimes(); EasyMock.expect(sessionImplementor.getPersistenceContext()).andReturn( persistenceContext); EasyMock.expect(persistenceContext.isDefaultReadOnly()) .andReturn(Boolean.TRUE).anyTimes(); EasyMock.expect(entityPersister.isMutable()).andReturn(Boolean.TRUE) .anyTimes(); EasyMock.expect(sessionImplementor.isClosed()).andReturn(Boolean.FALSE) .anyTimes(); EasyMock.expect(sessionImplementor.isOpen()).andReturn(Boolean.TRUE) .anyTimes(); EasyMock.expect(sessionImplementor.isConnected()) .andReturn(Boolean.TRUE).anyTimes(); EasyMock.expect( sessionImplementor.immediateLoad(EasyMock.eq("Annotation"), EasyMock.eq(1L))).andReturn(b2).anyTimes(); EasyMock.replay(sessionImplementor, sessionFactoryImplementor, entityPersister, persistenceContext); JavassistProxyFactory javassistProxyFactory = new JavassistProxyFactory(); Set interfaces = new HashSet(); interfaces.add(HibernateProxy.class); interfaces.add(Serializable.class); interfaces.add(Identifiable.class); interfaces.add(SecuredObject.class); javassistProxyFactory.postInstantiate("Annotation", Annotation.class, interfaces, Annotation.class.getDeclaredMethod("getId"), Annotation.class.getDeclaredMethod("setId", Long.class), null); b3 = javassistProxyFactory.getProxy(1L, sessionImplementor); EasyMock.verify(sessionImplementor, sessionFactoryImplementor, entityPersister, persistenceContext); assertTrue("Equals should return true", b1.equals(b3)); }