This section introduces:
1 adding and deleting and changing the wording
2 Query the difference between load and query get methods
3 Demo
1 adding and deleting and changing the wording
Add Session.save ()
Modify Session.update ()
Delete Session.delete ()
Query Session.get ()/session.load ()
2 Query the difference between load and query get methods
A get does not consider caching, and get sends an SQL statement to the database immediately after the call, returning the persisted object. Load is called after the proxy object is returned, the proxy object only holds the entity object ID, and only the SQL statement is emitted using the object's non-primary key attribute.
b query for data that does not exist, get returns Null,load throws an exception.
3 Demo
Hibernate.cfg.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/bendi</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" ></property>
<property name= "dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name= "Show_sql" >true</property>
<property name= "Format_sql" >true</property>
<property name= "Hbm2ddl.auto" >update</property>
<mapping resource = "Student.hbm.xml"/>
</session-factory>
Student.hbm.xml
<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<!--Generated 2017-12-20 0:42:12 by Hibernate Tools 3.4.0.CR1--
<class name= "Com.ddwei.student.Student" table= "Student" >
<id name= "pid" type= "int" >
<column name= "PID"/>
<generator class= "Increment"/>
</id>
<property name= "name" type= "Java.lang.String" >
<column name= "Name"/>
</property>
<property name= "Sex" type= "java.lang.String" >
<column name= "SEX"/>
</property>
<property name= "Birthday" type= "date" >
<!--<property name= "Birthday" type= "Time" >-
<!--<property name= "Birthday" type= "timestamp" >-
<column name= "BIRTHDAY"/>
</property>
<!--<property name= "Address" type= "java.lang.String" >-
<!--<column name= "ADDRESS"/>--
<!--</property>--
<property name= "Picture" type= "Java.sql.Blob" >
<column name= "Picture"/>
</property>
<component name= "Address" class= "com.ddwei.student.Address" >
<property name= "postcode" column= "postcode"/>
<property name= "Phone" column= "Phone"/>
<property name= "Address" column= "Address"/>
</component>
</class>
Studenttest.java
Package hibernate_001;
Import Java.io.File; Import Java.io.FileInputStream; Import java.io.FileNotFoundException; Import Java.io.FileOutputStream; Import java.io.IOException; Import Java.io.InputStream; Import Java.io.OutputStream; Import Java.sql.Blob; Import java.sql.SQLException; Import Java.util.Date;
Import Org.hibernate.Hibernate; Import org.hibernate.Session; Import Org.hibernate.SessionFactory; Import org.hibernate.Transaction; Import org.hibernate.cfg.Configuration; Import Org.hibernate.service.ServiceRegistry; Import Org.hibernate.service.ServiceRegistryBuilder; Import Org.junit.After; Import Org.junit.Before; Import Org.junit.Test;
Import com.ddwei.student.Address; Import com.ddwei.student.Student;
public class Studenttest { private sessionfactory sessionfactory; private session session; priva Te Transaction trasaction; @Test public void Testsavestudent () {// student Student =new Student (1, "Zhou Enlai", "male", new Date (), "Shaoxing");//Create student object student Student = new Student (); student.setname ("Qin Shihuang"); student.setsex ("male"); student.setbirthday (New Date ()); student.setaddress ("Palace of the House"); address address = new Address ("105789", "18912345678", "Hebei Dunhuang Parish"); student.setaddress (address); session.save (student);//Session Save student object into database } @Test //test modify record public void Testupdate () { student Student = (Student) session.get (Student.class, 1); student.setbirthday ( New Date ()); session.update (student); } @Test //test query record public void TestLoad () { student Student = (Student) seSsion.load (Student.class, 1); string AAA = Student.getname (); system.out.println ("student1=" +aaa); } @Test //test query record public void Testget () { student Student = (Student) session.ge T (Student.class, 1); system.out.println ("student2=" +student);
} @Test //test Delete record public void Testdelete () { student Student = (Student) session . Get (Student.class, 1); session.delete (student);  ,} &NB Sp @Before public void init () { 1 Create configuration Object configuration config = new configuration (). Configure (); //2 Create service object serviceregistry Servicere = new Serviceregistrybuilder (). Applysettings (Config.getproperties ()). Buildserviceregistry (); //3 Create session Factory sessionfactory = Config.buildsessionfactory (Servicere); //4 Open Session session = Sessionfactory.opensession (); //5 Create Transaction trasaction = Session.begintransaction (); } &NBSp @After public void Destroy () { trasaction.commit (); session.close (); Sessionfactory.close (); }
}
Student.java
Package com.ddwei.student;
Import Java.sql.Blob; Import Java.util.Date;
public class Student {
Java Beans Design Principles/** * 1 public class 2 Total without parameter construction Method 3 private Property 4 Property Setter/getter method */
private int pid;//number private string name;//name private string sex;//sex private date birthday;//date of birth private Address address;//Home Address private Blob picture;//photo
Public Student () {
}
public Student (int pid, string name, string sex, Date birthday) {//Super (); This.pid = pid; THIS.name = name; This.sex = sex; This.birthday = Birthday; this.address = address; }
@Override public String toString () {return "Student [pid=" + pid + ", name=" + name + ", sex=" + Sex + ", birthday = "+ Birthday +", address= "+ address +"] "; }
public int getpid () {return PID; }
public void setpid (int pid) {this.pid = pid; }
Public Date Getbirthday () {return birthday; }
public void Setbirthday (Date birthday) {this.birthday = birthday; }
Public Address getaddress () {return address; }
public void setaddress (address address) {this.address = address; }
Public String GetName () {return name; }
public void SetName (String name) {this.name = name; }
Public String Getsex () {return sex; }
public void Setsex (String sex) {this.sex = sex; }
Public Blob Getpicture () {return picture; }
public void Setpicture (Blob picture) {this.picture = picture; }
}
Address.java
Package com.ddwei.student;
Address class public class address {private string postcode;//zip code private string phone;//Phone private string address;//address Pu Blic String Getpostcode () {return postcode; } public void Setpostcode (String postcode) {This.postcode = postcode; } public String Getphone () {return phone; } public void Setphone (String phone) {this.phone = phone; } public String getaddress () {return address; The public void setaddress (String address) {this.address = address; }//Non-parametric construction method public address () {}//Parameter construction method public address (string postcode, String phone, string Address) { Super (); This.postcode = postcode; This.phone = phone; this.address = address; } }
A tentative study on Hibernate course single-table mapping 3-5 hibernate additions and deletions