2
carA = carRepository.load(1);
carB = carRepository.load(1);
carA == carB; // true
carA.changeColor(red);
carB.changeColor(blue);
carA == carB; // true

Equal entity objects but with non-equal attributes - do they exist in single-threaded/single-db applications?

When could a similar case like above ever have carA to be red but carB to be blue?

For a single-threaded web-app with one DB, is there ever a case where you would need carA and carB to be the same identity but have non-equal attributes?

j-a
  • 1,780
  • 1
  • 21
  • 19

2 Answers2

3

Equal entity objects but with non-equal attributes - do they exist in single-threaded/single-db applications?

They may exist in applications with a home-grown ORM or data access layer. This is problematic because it makes code less intuitive and predictable. In general this is a solved problem and a non-issue if you use a proper Unit Of Work implementation (like Hibernate Session for example). UOW is responsible for tracking everything that you loaded during a given transaction and would make sure that the same Entity is represented as a single Java/C# instance:

A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.

Dmitry
  • 17,078
  • 2
  • 44
  • 70
1

What color is carA now?

It is blue.

Line by line

carA = carRepository.load(1);  // carA is a reference to an object with id=1
carB = carRepository.load(1);  // carB is a reference to an object with id=1
carA == carB; // true          // carA == carB, they point to the same object 
carA.changeColor(red);         // object with id=1 has color `red`
carB.changeColor(blue);        // object with id=1 has color `blue`
carA == carB; // true          // carA == carB, they point to the same object 
oleksii
  • 35,458
  • 16
  • 93
  • 163
  • I am sorry, I am more interested in the question "Equal entity objects but with non-equal attributes - do they exist in". Let me update. – j-a Aug 24 '12 at 17:56