As an important link in ssh, it is necessary to understand Hibernate's implementation of the O/R mapping.
The main use of the Java reflection mechanism to obtain a complete SQL statement.
Preparatory work:
1. Object
Student Entity classes:
Public classStudent {Private intID; PrivateString name; Private intAge ; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }}
2. Relationship
_student table:
-- database:hibernate Create Table int Primary Key int varchar ());
3. Analog O/R Mapping:
Sessionstu for the Student entity class (to complete the persistence of the student object):
Public classSessionstu {PrivateString TableName; Privatemap<string, string> field2column =NewHashmap<string,string>(); Privatestring[] fields; PublicSessionstu () {//The following should be build out by parsing the XML//This example demonstrates only the core part of Hibernate: O/R Mapping (full SQL with Reflection)TableName = "_student"; Field2column.put ("id", "_id"); Field2column.put ("Name", "_name"); Field2column.put ("Age", "_age"); fields=Newstring[field2column.size ()]; Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(ClassNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } Public voidSave (Student Student)throwsException {//TODO auto-generated Method StubString sql =Createsql (); Connection Conn= Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/hibernate", "Root", "" "); PreparedStatement PS=conn.preparestatement (SQL); PS=setparameters (ps,student); Ps.executeupdate (); Ps.close (); Conn.close (); } //Important PrivatePreparedStatement setparameters (preparedstatement PS, Student Student)throwsexception{ for(inti = 0; i < fields.length; i++) {String Get_method= "Get" + (Char) (Fields[i].charat (0) -32) +fields[i].substring (1); Method m = student.getclass (). GetMethod (Get_method); String type = M.getreturntype (). GetName (); String te= Type.substring (Type.lastindexof (".") +1); Switch(TE) { Case"Int": Ps.setint (i+1, (int) M.invoke (student)); Break; Case"String": ps.setstring (i+1, (String) M.invoke (student)); default: Break; } } returnPS; } Privatestring Createsql () {string Columnsstr= ""; intindex = 0; for(String key:field2Column.keySet ()) {Fields[index]=key; Columnsstr+ = (Field2column.get (key) + ","); Index++; } columnsstr= columnsstr.substring (0, Columnsstr.length ()-1); String unknow = ""; for(inti = 0; I < field2column.size (); i++) {Unknow+= "?,"; } unknow= Unknow.substring (0,unknow.length ()-1); String SQL= "INSERT INTO" + This. TableName + "(" + Columnsstr + ") VALUES (" + Unknow + ")"; SYSTEM.OUT.PRINTLN (SQL); returnSQL; } }
4. Test:
Public class Testsessionsave { publicstaticvoidthrows Exception { New Student (); Student.setage (a); Student.setid (5); Student.setname ("Hibernate"); New Sessionstu (); Ss.save (student); }}
5. Done
Hibernate O/R mapping simulation