Java Web (Framework - Hibernate): Configuration

Keywords: Hibernate Attribute Database xml

First, a preliminary understanding of concepts
_hibernate is an open source object-relational mapping framework, which implements lightweight encapsulation of JDBC and establishes mapping relationship between pojo(javaBean) and database tables. It is a fully automated ORM framework. Hibernate automatically generates sql statements at the bottom level to achieve data persistence.
ORM:Object Relational Mapping. Object Relational Mapping. Object Relational Mapping. It means to establish a mapping relationship between an object and a table in a database, which can be manipulated by an object.
II. Resource Download
_Hibernate resource bundle address: http://hibernate.org/orm/ The downloaded resources are decompressed to get Hibernate's project directory
  
_lib package is a tool kit that needs to be used, required package is a necessary package file, other jar packages can be used as needed.
The documentation package is the relevant documentation.
_project is a case engineering provided by Hibernate
3. Case Demonstration
_In this article, only the most basic case demonstration is given. Development environment: win+idea
Step 1: Create a WEB project and import related packages as needed, such as database driver packages, log packages, core packages (necessary), C3p0 connection pools, etc.
Step 2: Create database and table files
  
Step 3: Establish entity classes according to the fields in the established table file. (The name of entity classes should be consistent with the fields in the table.)
Note: Entity classes need to provide parametric constructs and corresponding set/get methods. This is the rule for writing persistent objects.

Step 4: Generate corresponding mapping files according to entity classes.
In this case, XML files are used for relational mapping, and annotations are not used for the time being. idea can effectively support the mapping configuration of Xml.
Myeclipse requires configuration file settings to produce label code prompts.
idea and Myclipse can automatically generate XML files.
Mapping file naming rule class name. hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- ORM:Object Relational Mapping,Entity class O And tables of databases R Establishing Mapping Relations -->
    <class name="com.itheima.hibernate.domain.Customer" table="cst_customer">
        <!-- Attributes in classes correspond to primary keys in tables -->
        <id name="cust_id" column="cust_id">
            <generator class="native"/>
        </id>
        <!-- Attributes in classes correspond to fields in tables -->
        <property name="cust_name" column="cust_name"/>
        <property name="cust_source" column="cust_source"/>
        <property name="cust_industry" column="cust_industry"/>
        <property name="cust_level" column="cust_level"/>
        <property name="cust_phone" column="cust_phone"/>
        <property name="cust_mobile" column="cust_mobile"/>
    </class>
</hibernate-mapping>

Step 5: Create Hibernate Core Profile
Naming rules: hibernate.cfg.xml

<?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>
        <!-- Information to connect to database -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql:///hibernate_day01</property>
        <!-- User name -->
        <property name="hibernate.connection.username">****</property>
        <!-- Password -->
        <property name="hibernate.connection.password">****</property>

        <!-- Dialect of database:Generate different based on the underlying database SQL -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <!-- configure display SQL -->
        <property name="hibernate.show_sql">true</property>
        <!-- Configuration formatting SQL -->
        <property name="hibernate.format_sql">true</property>
        <!-- To configure hbm2ddl -->
        <property name="hibernate.hbm2ddl.auto">update</property>

        <!-- Load the mapping file -->
        <mapping resource="com/itheima/hibernate/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Step 6. Writing test methods

public class HibernateDemo1 {

    @Test
    /**
     * Save operation
     */
    public void demo1(){
        // Load Hibernate's core configuration file.
        Configuration configuration = new Configuration().configure();
        // Create a SessionFactory object.
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // Create Session (equivalent to Connection in JDBC)
        Session session = sessionFactory.openSession();
        // Open a transaction:
        Transaction transaction = session.beginTransaction();
        // Complete the operation:
        Customer customer = new Customer();
        customer.setCust_name("Future");

        session.save(customer);

        // Submission of affairs
        transaction.commit();
        // Release resources
        session.close();
    }
}

After the test method is executed, a data is added to the database successfully, and the test is successful!! __________

summary

In the process of Hibernate configuration, there are two key points to be noticed. One is the mapping file of the class, which can successfully match the entity class with the data table. The other is the core configuration file of Hibernate, which can configure all the information needed by Hibernate.

Posted by ungovernable on Sun, 24 Mar 2019 10:30:27 -0700