First, what is hibernate
Hibernate framework is one of the mainstream persistence layer framework, which is based on the mainstream persistence framework of JDBC, which can greatly simplify the code of the DAO layer and improve the working efficiency, so it is popular with the developers. With the STRUT2 and spring framework, this is the classic SSH combination.
Second, Orm thought
The programming idea of learning the framework is that ORM (Object Relational Mapping) is the idea of object-relational mapping, which in turn makes CRUD operations on the database. The so-called ORM idea is to match the entity object to table one by one in the database
The ORM principle is as follows
III. environment Construction 1. Required JAR Package
2. Two configuration file 2.1 mapping profile
2.2 Core configuration Files
This profile is typically in the source folder of SRC
Iv. CRUD Operations
@Test
Public void Testcrud () { //First step to load the core configuration file
Configuration configuration = new configuration ();
Configuration.configure ();
Get Sessionfactory Object
Sessionfactory factory = Configuration.buildsessionfactory ();
Get the Session object (the session object here is equivalent to connection in JDBC)
Session session = Factory.opensession ();
Open transaction
Transaction ts = session.begintransaction ();
try{
CRUD operations are performed here
Add Operation user
User user = new user ();
User.set (...);
Session.save (user);
Find operations
User user = Session.get (user.class, "UID");//Find the object under the specified ID
Other query methods
Using the HQL statement with the query object
Query qr = Session.createquery ("From User where property name =?") ");
Qr.setstring (0, "...");
list<user> User = Qr.list ();
Using the Criteria object query
Criteria = Session.createcriteria (User. Class);
list<user> list = Criteria.list ();
Querying with SQLQuery objects
sqlquery sq = session.createsqlquery ("SELECT * from user");
Sq.addentity (User.class);
list<user> list = Sq.list ();
The modify operation first looks at the modified
User user = Session.get (user.class, "UID");
User.set (...);
Session.update (user);
Delete operation
User user = Session.get (user.class, "UID");
Session.delete (user);
Or
User user = new user ();
User.setuid ("UID");
Session.delete (user);
Ts.commit ();
}catch (Exception e) {
Ts.rollback ();
}finally{
Session.close ();
Factory.close ();
}
}
V. Correlation between entities 5.1-to-many relationships
Each entity corresponds to a mapping profile if a one-to-many relationship between two entities is required two mapping profiles
In one to many
The one's class tag adds
<!--in the customer mapping file means that all contacts use the set label to represent the name attribute in all contact Set Tags: property values are written in the client entity class to represent the set collection name of the set collection names in the entity class. Hibernate mechanism: Two-way maintenance foreign key words ah one and more which side have configured foreign keys in One-to-many class is the contact entity classpath Inverse: Discard relationship maintenance because Hibernate is a two-way gradual maintenance -- <set name= "Setlinkman" inverse= "true" cascade= "Save-update,delete" > <key column= "clid" ></ key> <one-to-many class= "Cn.entity.LinkMan"/> </set>
Add the many class tag
<many-to-one name= "Customer" class= "Cn.entity.Customer" column= "Clid" ></many-to-one>
Not finished ...
I am in school sophomore students self-study Java, the level of limited, there are mistakes you great God must be more advice, thank you!
Hibernate of Framework Learning notes