Object-oriented is developed from the basic Principles of software engineering (such as coupling, aggregation and encapsulation), and the relational database is developed from the mathematical theory, the two sets of theories have distinct differences, and Java is a completely object-oriented language, and this conflict is becoming more and more obvious.
For the current information system of Java EE, the process of object-oriented analysis and object-oriented design is usually adopted. The system is based on the object-oriented approach from requirement design to system design. However, in the detailed design phase, because data persistence needs to be saved to the relational database, it is very frustrating to have to modify the design plan from the bottom up, and return to the old way of programming in the process.
As shown in code 9.2, the data is queried by the database, each field in the result set is read as a bar, and then assembled into an object, and code 9.3 shows the opposite, saving an object to the database. Each property of the object needs to be split, pieced together into a SQL string, and then committed to the database. The process of converting back and forth is cumbersome and extremely error-prone, for an object-oriented program such as nightmares.
Code 9.2 querying Database Assembly objects
List persons = new Arraylist (); ResultSet rs = stmt.executequery ("SELECT * from person"); To process a result set while (Rs.next ()) { person person = new person int id = rs.getint ("id"); String name = rs.getstring ("name");
Null:rs.getInt ("Age"); Person.setid (ID); Person.setname (name); Persion.setaget (age); Persons.add (person); } |
Code 9.3 saving objects to data
String sql = "INSERT into T_person (ID, name, age) values
Statement stmt = Con.createstatement (); Stmt.executeupdate (SQL) Con.commit () |
Good object-oriented developers are tired of this repetitive labor and begin to adopt the usual "positive" lazy approach of creating tools that automate the entire process and help developers care only about objects without having to consider the structure of the database. For relational databases, the best outcome of this effort is the object/relational mapping (ORM) tool introduced in the next SectionTo.