table of Contents:
- Configuration and basics
- The basic CRUD operations
Configuration
https://how2j.cn/k/hibernate/hibernate-tutorial/31.html
The final version of the Product Case Study
Step1: Create a database and table
CREATE TABLE product_ (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(30) ,
price float ,
PRIMARY KEY (id) ,
......
) DEFAULT CHARSET=UTF8;
Follow-up also adds other, not repeat them here
Step2: guide package
See links to the top
Step3: Creating an entity class
Member variables and database table column to one correspondence
public class Product {
int id;
String name;
float price;
Category category;
Set<User> users;
int version;
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public Set<User> getUsers() {
return users;
}
public void setUsers(Set<User> users) {
this.users = users;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
Step4: xml configuration entity corresponding category
- Product.hbm.xml P must be capitalized, and classes to be consistent
- class label shows the relationship of the entity classes and table
- id tag,
<generator class="native">
meaning the id from growth mode using a local database - version label, see (b) of the optimistic lock
- property label indicates that the variable
- set label is a variable, see (b) the relationship
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.how2java.pojo">
<class name="Product" table="product_">
<id name="id" column="id">
<generator class="native">
</generator>
</id>
<version name="version" column="ver" type="int"/>
<property name="name" />
<property name="price" />
<many-to-one name="category" class="Category" column="cid"/>
<set name="users" table="user_product">
<key column="pid"/>
<many-to-many column="uid" class="User"/>
</set>
</class>
</hibernate-mapping>
Step5: Configure total xml
In the src directory, after each one more entity classes to add a mapping
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/how2java/pojo/Product.hbm.xml" />
<mapping resource="com/how2java/pojo/Category.hbm.xml" />
<mapping resource="com/how2java/pojo/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
principle:
Hibernate applications through a Product object into the database table product_
hibernate.cfg.xml configuration file provides links to a database of basic information about
account password database driver ip port
Product.hbm.xml provide an object mapping table and
which table corresponds ? What attributes correspond to what field
Specific operation
Create a Product object, and hibernate through this object, inserted into the database
Hibernate basic steps are:
- Get SessionFactory
- Fetching a Session SessionFactory
- Open a transaction on the basis of Session
- Session by calling the save method to save objects to the database
- Commit the transaction
- Close Session
- Close SessionFactory
Template below
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.how2java.pojo.Product;
public class TestHibernate {
public static void main(String[] args) {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
// TODO:事件操作
s.getTransaction().commit();
s.close();
sf.close();
}
}
Note: code after CRUD TODO is in part omitted before and after
By (insert)
for (int i = 0; i < 10; i++) {
Product p = new Product();
p.setName("iphone"+i);
p.setPrice(i);
s.save(p);
}
Seek
Product p =(Product) s.get(Product.class, 6);
System.out.println("id=6的产品名称是: "+p.getName());
The second method is to get parameter id, it is a database key
delete
With the delete () method
Product p =(Product) s.get(Product.class, 5);
s.delete(p);
modify
By update () method
System.out.println(p.getName());
p.setName("iphone-modified");
s.update(p);