Object set preservation in hibernate

Keywords: Java Hibernate Session xml

1. Objects are often used to operate in java web design, and object sets are saved in hibernate (one-to-many)

The following steps are required:

1) Designing data table relationships

2) Introducing jar packages requires attention to introducing database connector

3) Write entity classes

4) Configuration mapping file and hibernate.cfg.xml

 

Two cases were as follows:

The relationship between employees and departments, many employees belong to a department, a department has many employees. You can set a foreign key on the employee table to solve one-to-many relationships.

2.1) Create tables

CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,
ename VARCHAR(50) NOT NULL,
depid INT,
FOREIGN KEY (depid) REFERENCES dept(id) ON DELETE CASCADE);

CREATE TABLE dept(
id INT PRIMARY KEY AUTO_INCREMENT,
dname VARCHAR(100));

2.2) Introducing jar packages

 

2.3) Write entity classes

Employee category

public class Employee {
    private int id;
    private String ename;
    private Dept dept = new Dept();
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    
    

}

Sectoral category

public class Dept {
    private int id;
    private String dname;
    private Set<Employee> employees = new HashSet<Employee>();
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public Set<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(Set<Employee> employees) {
        this.employees = employees;
    }
}

 

2.4) Configuration mapping file

Departmental mapping file, dept.hbm.xml

<hibernate-mapping 
    package="com.baidu.entity">
    <class name="Dept" table="dept">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="dname" column="dname"></property>
        
        <set name="employees" table="emploee">
            <key column="depid"></key>
            <one-to-many class="Employee"/>
        </set>

    </class>
</hibernate-mapping>

Note: When configuring one-to-many, the Department is one-to-many for employees, and the content set to be configured

1) Which is the collection attribute, employees?

2) The set property stores the table corresponding to the object, employee

3) the foreign key of the table, depid

4) The type of object stored in the collection, class=Employee

 

Employee mapping file, employee.hbm.xml

<hibernate-mapping 
    package="com.baidu.entity">
    <class name="Employee" table="employee">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        
        <property name="ename" column="ename"></property>
        <many-to-one name="dept" class="Dept" column="depid"></many-to-one>
    </class>

</hibernate-mapping>

Note: When configuring many-to-one, employees are many-to-one to departments. The content to configure is many-to-one

1) Which object to map

2) Which foreign key of the table is associated with the object

3) Types of mapping objects

 

Global configuration hibernate.cfg.xml to view previous blogs hibernate development process

 

3 Simple Testing

public void fun1(){
        Configuration configuration = new Configuration();
        configuration.configure();
        
        SessionFactory sessionFac = configuration.buildSessionFactory();
        Session session = sessionFac.openSession();
        Transaction bt = session.beginTransaction();
        
        Dept dept = new Dept();
        dept.setDname("test");
        Employee employee = new Employee();
        employee.setEname("test3");
        employee.setDept(dept);
        
        
        Employee employee2 = new Employee();
        employee2.setEname("test4");
        employee2.setDept(dept);
        
        session.save(employee);
        session.save(employee2);
        session.save(dept);
        
        bt.commit();
        
        session.close();
        sessionFac.close();
        
    }

 

 

It should be noted that

In tables, one-to-many or more-to-one relationships are expressed by foreign keys; in classes, by object sets, such as set/list/map; and in configuration files, attention should be paid to the configuration of set/list/map.

If a table has foreign keys, it will add another object to that object by manipulating the table, otherwise there will be problems.

In hibernate, the only way to map an object to a foreign key field is many-to-one.

Posted by minc on Fri, 05 Jul 2019 18:01:04 -0700