The problem comes: There is a necessary prerequisite for the above code to run correctly, that is, the method call parameter cat object must be a PO that has been persisted, that is, it should be queryed from the database first, and then it can be used in this way. However, the programmer at the business layer obviously does not know the internal mystery. If his business is to add a cat now and then add its mate, he will obviously call it like this, new cat object comes out, and then addMate:
Java code
Cat cat = new Cat();;
();;
(cat,mate);;
Cat cat = new Cat();;
();;
(cat,mate);;
Cat cat = new Cat();;
();;
(cat,mate);;
But please note that this cat object is just a VO, it has not been persisted, it is not a PO, it is not qualified to call the addMate method, so calling the addMate method will not actually send the update sql to the database. This cat object must be saved to the database first, and only after it becomes a PO can it be qualified to addMate.
You have to do this:
Java code
Cat cat = new Cat();;
();;
(cat);;
(cat, mate);;
Cat cat = new Cat();;
();;
(cat);;
(cat, mate);;
Cat cat = new Cat();;
();;
(cat);;
(cat, mate);;
The cat is persisted before other persistence operations can be performed on the cat. Therefore, programmers at the business layer must be clear about the state of the cat object, whether it is the first or the third. If it is the first type, you must save first and then addMate; if it is the third type, you must addMate directly.
But the most fatal thing is that if the entire software is layered a lot and the programmer at the business layer gets this cat object, it may also be a cat passed by the upper web application layer, and he himself does not know whether the cat is VO, it has not been persisted, or it has been persisted, then he has no way to write a program.
So such DAOImpl is obviously problematic. It will cause many programming traps to programmers at the business level. Programmers at the business level must have a deep understanding of what state management the PO object each DAO they call performs on, and must have a deep understanding of what exact state his PO object is in at any time to ensure the correctness of programming. Obviously this cannot be done, but with saveOrUpdate, these problems are solved.
Now you need to modify the addMate method:
Java code
public void addMate(Cat cat, Mate mate); {
Session session = ...;
Transacton tx = ...;
(cat);;
(mate);;
();;
();;
};
public void addMate(Cat cat, Mate mate); {
Session session = ...;
Transacton tx = ...;
(cat);;
(mate);;
();;
();;
};
public void addMate(Cat cat, Mate mate); {
Session session = ...;
Transacton tx = ...;
(cat);;
(mate);;
();;
();;
};
As mentioned above, if the programmer of the business layer passes in a persisted PO object, then Hibernate will update the cat object (assuming that the programmer of the business layer has modified the cat attributes outside the Session). If the programmer of the business layer passes in, then save the PO object to the database.
BTW: Whether Hibernate updates the cat object or save cat object at this time depends on the unsave-value setting.
In this way, programmers at the business level no longer have to worry about the state of the PO. For them, whether cat is an object from new, it is just a VO or a PO object from the database, it is OK to addMate directly:
Java code
(cat, mate);;
(cat, mate);;
(cat, mate);;
This is what saveOrUpdate does.
Previous page12Read the full text