Hibernate (Object-Oriented Database Operations)
JPA, java persistance api, java persistence layer interface, that is, some interfaces dealing with databases. The implementation is handed over to various vendors for implementation.
ORM, Object/Relation Mapping, Object/Relational Database ing. For object-oriented database operations, the underlying layer is still SQL statements.
Obtain the maven dependency of hibernate.
Org. hibernate
Hibernate-core
4.3.10.Final
Mysql
Mysql-connector-java
5.1.31
Hibernate is an excellent ORM implementation. For usage instructions, see.
The configuration file name is hibernate. cfg. xml, which is usually placed in the src directory. It uses c3p0 as the database connection pool.
Com. mysql. jdbc. Driver
Jdbc: mysql: //me.likeyichu.com: 3306/AliyunDB
True
UTF-8
Root
Password
20
1
5000
100
3000
True
2
True
Org. hibernate. dialect. MySQLDialect
Common classes @ javax. persistence. Entity
Indicates that this class corresponds to an object in a table in the database.
@ Javax. persistence. Table
The name attribute of this annotation indicates which table the java class corresponds.
@ Javax. persistence. Id
Indicate that this field is the primary key in the database table.
@ Javax. persistence. Column
The name attribute of this annotation specifies which column of the field corresponds to the table. By default, mutual matching of the same name is performed.
@ Javax. persistence. Temporal
The database has many time types. The value TemporalType. TIMESTAMP indicates the TIMESTAMP.
@ Javax. persistence. Transient
When a pojo field is not in the table, you can add this comment to avoid errors.
HQLorg. hibernate. SharedSessionContract. createQuery (String queryString)
Create a query using the given hql statement.
List org. hibernate. Query. list ()
Returns the query result as a list.
Sample Code
Package com. likeyichu. webservice. resource. me; import java. SQL. timestamp; import java. util. date; import java. util. list; import javax. persistence. column; import javax. persistence. entity; import javax. persistence. id; import javax. persistence. table; import javax. persistence. temporal; import javax. persistence. temporalType; import org. hibernate. session; import org. hibernate. sessionFactory; import org. hibernate. trans Action; import org. hibernate. cfg. configuration; import org. hibernate. service. serviceRegistry; import org. hibernate. service. serviceRegistryBuilder; @ Entity @ Table (name = "studentTable") public class Student {@ Idint id; String name; boolean isGirl; @ Column (name = "time_stamp ") @ Temporal (TemporalType. TIMESTAMP) Date timestamp; public int getId () {return id;} public void setId (int id) {this. id = id;} public Date get TimeStamp () {return timestamp;} public void setTimeStamp (Date timeStamp) {this. timestamp = timeStamp;} public String getName () {return name;} public void setName (String name) {this. name = name;} public boolean isGirl () {return isGirl;} public void setGirl (boolean isGirl) {this. isGirl = isGirl;} public static void main (String [] args) {// load src/hibernate. cfg. xml as Configuration conf = new Configuration (). co Nfigure (); // so long, annoyingServiceRegistry serviceRegistry = new ServiceRegistryBuilder (). applySettings (conf. getProperties ()). buildServiceRegistry (); SessionFactory sf = conf. buildSessionFactory (serviceRegistry); Session sess = sf. openSession (); Transaction ts = sess. beginTransaction (); Student student = new Student (); student. setName ("qiqi"); // Add a data sess row. save (student); ts. commit (); // obtain all female students and assemble them into a list. @ SuppressWarnings ("unchecked") List <Student> list = sess. createQuery ("from Student where isGirl = true "). list (); System. out. println (list); sess. close (); sf. close ();}}