Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof SimpleObject)) return false;
- SimpleObject that = (SimpleObject) o;
- // we prefer the method versions of accessors,
- // because of Hibernate's proxies.
- if (getId() != null ?
- !getId().equals(that.getId()) : that.getId() != null)
- return false;
- if (getKey() != null ?
- !getKey().equals(that.getKey()) : that.getKey() != null)
- return false;
- if (getValue() != null ?
- !getValue().equals(that.getValue()) : that.getValue() != null)
- return false;
- return true;
- }
- @Override
- public int hashCode() {
- int result = getId() != null ? getId().hashCode() : 0;
- result = 31 * result + (getKey() != null ? getKey().hashCode() : 0);
- result = 31 * result + (getValue() != null ? getValue().hashCode() : 0);
- return result;
- }
- @Test
- public void testSaveLoad() {
- Long id = null;
- SimpleObject obj;
- try (Session session = SessionUtil.getSession()) {
- Transaction tx = session.beginTransaction();
- obj = new SimpleObject();
- obj.setKey("sl");
- obj.setValue(10 L);
- session.save(obj);
- assertNotNull(obj.getId());
- // we should have an id now, set by Session.save()
- id = obj.getId();
- tx.commit();
- }
- try (Session session = SessionUtil.getSession()) {
- // we're loading the object by id
- SimpleObject o2 = session.load(SimpleObject.class, id);
- assertEquals(o2.getKey(), "sl");
- assertNotNull(o2.getValue());
- assertEquals(o2.getValue().longValue(), 10 L);
- SimpleObject o3 = session.load(SimpleObject.class, id);
- // since o3 and o2 were loaded in the same session, they're not only
- // equivalent - as shown by equals() - but equal, as shown by ==.
- // since obj was NOT loaded in this session, it's equivalent but
- // not ==.
- assertEquals(o2, o3);
- assertEquals(obj, o2);
- assertTrue(o2 == o3);
- assertFalse(o2 == obj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment