In the third part, this section is much simpler.
The market is ruthless, opportunities and crises are everywhere. After reading the red fire of the business, Zhang San has lost his own support in the business field in the next year. The first thing to sell the car is shipped, the license is "A00002".
Package com.dao; import java.util. *; import net.sf.hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class test {autoInfo ai; people; public void dotest () {try {Configuration cfg = new Configuration () configure ();. SessionFactory sessions = cfg.buildSessionFactory (); Session session = sessions.openSession (); Transaction tx = session.beginTransaction (); ai = (AutoInfo) session .find ("from autoinfo where license_plate = 'A00002'"). get (0); people = ai.getownerno (); people.getautoinfoset (); transide (ai); session.delete (ai); tx.commit ); Session.close ();} catch (exception e) {system.out.println (e);}}}
Why remove a AutoInfo object from a People object?
Asked! Traditional JDBC programs can directly delete records in "A00002" as the condition, so there is no problem. But if you use the same way in Hibernate, you will cause a lot of trouble:
Net.sf.hibernate.ObjectDeletedException: Deleted Object Would Be Re-Saved By Cascade (Remove Deleted Object from Associations): 2, of class: bo.autoinfo
The reason that cannot be deleted is that there is a one-to-many relationship of people and auto_info tables. If you want to delete a record from Auto_INFO, you must use people.getautoinFoset (). Remove (AI) method is PEOPLE The AutoInfo object is queried with "A00002" to truly delete the autoinfo object.
After returning from Zhang San's lost, this hibernate tour is coming soon. Finally, I have to experience the deletion of the people table and the auto_info table there.
Package com.dao; import java.util. *; import net.sf.hib.Hibernate. *; import.sf.hibernate.cfg. *; import bo. *; public class test {people penople; public void dotest () { try {Configuration cfg = new Configuration () configure ();. SessionFactory sessions = cfg.buildSessionFactory (); Session session = sessions.openSession (); Transaction tx = session.beginTransaction (); people = (people) session.load ( People.class, new integer (1)); session.delete (person); tx.commit (); session.close ();} catch (exception e) {system.out.println (e);}}} Hibernate The advantage is once again reflected. We only need to delete a one-to-many relationship, and all other records associated with it will be deleted.
Finally, through this journey, it is also considered a feature of Hibernate. As an O / R mapping implementation, it is very good, I hope we can use it.