1. Object state
instantaneous (transient): There is no data in the database that corresponds, and more than the scope is reclaimed by the JVM garbage collector, which is generally new and has no association with the session.
Persistent (persistent): There may be data in the database corresponding to the session, currently associated with the session, and the associated session is not closed, the transaction is not committed, the persistent object state has changed, and the database will be affected when the transaction commits ( Hibernate can detect).
detached: There may be data in the database corresponding to it, but there is currently no session associated with it; The managed object state has changed and hibernate cannot be detected.
2. Manipulating the Persisted object-save ()
The session's Save () method converts a temporary object to a persisted object
the Save () method of the session completes the following actions :
L Add a News object to the session cache to make it persistent
l Use the identifier builder specified by the mapping file to assign a unique OID to the persisted object. The SetId () method sets the OID for the News object to be invalid when using a proxy primary key.
L plan to execute an INSERT statement that assembles the current property values of the Customer object into the INSERT statement
L Hibernate maintains its correspondence with database-related records by persisting the OID of the object. When a News object is persisted, the program is not allowed to modify its ID arbitrarily
3. Manipulating the Persisted object-update ()
The session's update () method converts a free object to a persisted object and plans to execute an UPDATE statement.
manipulating the Persisted object-saveorupdate ()
The method includes both the Save and update methods , and if the parameter is a temporary object, use the Save method, and if it is a free object, use the Update method and return it directly if it is a persisted object.
4. HQL (Hibernate Query Language) and Criteria
HQL
Object-oriented query languages, unlike SQL, object names in HQL are case-sensitive (except that the Java class and other parts of the property are case-insensitive); HQL is an object, not a table, and supports polymorphism; HQL is mostly done through query. How query is created:
Query q = session.createquery (HQL);
L from person
L from user user where user.name=:name
L from user User where User.name=:name and User.birthday <: Birthday
Criteria
The criteria is a more object-oriented query than HQL ; How to create the criteria:
Criteria crit = Session.createcriteria (Domainclass.class);
Simple attribute conditions such as: Criteria.add (Restrictions.eq (propertyname, Value)), Criteria.add (Restrictions.eqproperty (PropertyName, Otherpropertyname))
Note: The follow-up blog will elaborate, here a simple mention .
5. Correlation Mapping
L-Multiple to one (employee-department)
L One-to-many (Department-employee)
L One-to-one (Citizen-identity card)
L Many-to-many (teacher-student)
L Component Mappings (User-name)
L Set Mappings (set, list, map, bag)
L-Cascade Relationship Inverse and cascade (employee–department)
6. Multi-One (employee-department)
Mapping file <many-to-one name= "depart" column= "depart_id"/> er diagram:
Case study: Main code: Many-to-many: Hbm.xml:<many-to-one name= "dep" column= "Depid"/> Bean class: Private Department cus;
Test class Package Com.hbsi.many2one;
Import org.hibernate.Session;
Import Org.junit.Test;
Import Com.hbsi.utils.HibernateSessionFactory;
public class Many2one {Sessions session = Hibernatesessionfactory.getsession ();
@Test public void Add () {session. BeginTransaction ();
Department dep = new Department ();
Dep.setdepname ("network system");
Employee EMP1 = new Employee ();
Emp1.setempname ("se");
Employee EMP2 = new Employee ();
Emp2.setempname ("de"); If DEP is first saved, three SQL statements are inserted, or five SQL statements are produced and three inserts two updates if the DEP is saved.
This is because DEP generates a foreign key field in the employee table before it is not generated, and the transaction is updated again Session.save (DEP);
EMP1.SETDEP (DEP);
Session.save (EMP1);
EMP2.SETDEP (DEP);
Session.save (EMP2);
Session.gettransaction (). commit ();
Hibernatesessionfactory.closesession ();
}
@Test public void Find () {Employee EMP = (employee) session.get (employee.class,7);
Department dep = EMP.GETDEP ();
System.out.println (Emp.getempname () + "---" +dep.getdepname ());
Hibernatesessionfactory.closesession ();
} one-to-many: Hbm.xml: <set name= "EMP" table= "employee" > <key column= "depid"/>
<one-to-many class= "Employee"/> </set> Bean class: Private set<employee> emp;
Test class: Package com.hbsi.many2one;
Import org.hibernate.Session;
Import Org.junit.Test;
Import Com.hbsi.utils.HibernateSessionFactory;
public class Many2one {Sessions session = Hibernatesessionfactory.getsession ();
@Test public void Add () {session. BeginTransaction ();
Department dep = new Department ();
Dep.setdepname ("network system");
Employee EMP1 = new Employee ();
Emp1.setempname ("se");
Employee EMP2 = new Employee (); Emp2.setEmpName ("de"); If DEP is first saved, three SQL statements are inserted, or five SQL statements are produced and three inserts two updates if the DEP is saved.
This is because DEP generates a foreign key field in the employee table before it is not generated, and the transaction is updated again Session.save (DEP);
EMP1.SETDEP (DEP);
Session.save (EMP1);
EMP2.SETDEP (DEP);
Session.save (EMP2);
Session.gettransaction (). commit ();
Hibernatesessionfactory.closesession ();
@Test public void Find () {Employee EMP = (employee) session.get (employee.class,7);
Department dep = EMP.GETDEP ();
System.out.println (Emp.getempname () + "---" +dep.getdepname ());
Hibernatesessionfactory.closesession ();
}
}