search
HomeJavajavaTutorialDetailed explanation of the steps to build the hibernate framework environment

1. Overview: The hibernate framework acts on the dao layer to achieve persistent storage of data. It operates the database in an object-oriented manner.

2. Construction of the hibernate framework

1. Import all jar packages in the required folder in the lib directory.

Mysql driver package.

2. Create a database in the table.

 3. Create entity class.

 4. Create entity mapping file (take crm practice Customer class as an example)

  Entity class name.hbm.xml

  Introduction of constraints File

<?xml  version="1.0" encoding="UTF-8"?>nbsp;hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><!-- 根元素
        package(可选):填写包名.后面凡是需要完整类名的地方,都可以省略包名了. --><hibernate-mapping><!-- class:映射类与表的关系
            name属性:实体属性名
            table属性:对应的表名     --><class><!-- id:映射主键属性名(OID)与主键列对应关系
            name属性: OID名称
            column属性(可选):主键列名,默认值就是name属性值
            length属性(可选):指定属性长度.默认值使用数据库对应列长度
            type属性(可选):指定当前列(属性)的类型.默认值会根据数据库类型自动指定类型.
                type="long"                hibernate类型
                type="java.lang.Long"    java类型
                <column name="cust_id" sql-type="bigint" ></column>  数据库类型     --><id><!--主键生成策略 
        increment:hibernate每次保存数据是,会查询数据库中最大的值,在最大值的基础上加1作为新的主键值(测试时使用)
            identity:主键自增,有数据库负责生成主键值
            sequence:序列,Oracle时使用
            hilo:高低位算法,适用于既不支持自增也不支持序列的库(用不着)
            native:identity|sequence|hilo自动三选一
            uuid:主键类型为字符串是使用.
            assigned:有我们手动指定ID值
        --><generator></generator></id><!-- property:映射非主键属性名与非主键列对应关系
            name属性: 属性名
            column属性(可选):非主键列名,默认值就是name属性值
            length属性(可选):指定属性长度.默认值使用数据库对应列长度
            type属性(可选):指定当前列(属性)的类型.默认值会根据数据库类型自动指定类型.
                type="long"                hibernate类型
                type="java.lang.Long"    java类型
                <column name="cust_id" sql-type="bigint" ></column>  数据库类型     --><property></property><property></property><property></property><property></property><property></property><property></property></class></hibernate-mapping>

Create the main configuration file

hibernate.cfg.xml (under src)

 1 <?xml  version="1.0" encoding="UTF-8"?> 2 nbsp;hibernate-configuration PUBLIC 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 5     <!-- 根元素 --> 6     <hibernate-configuration> 7         <!-- 以下都是为sessionFactory对象配置的 --> 8         <session-factory> 9         <!-- 必选配置10         11             //方言12             //所有数据库的sql语句都是基于SQL99标准的13             //每个数据库遵循SQL99标准的同时,也会扩充一部分SQL语句.这些标准之外的sql语句叫做方言   mysql方言: limit 0,514             //注意:mysql方言类一共有3个.一定要选最短的15             #hibernate.dialect org.hibernate.dialect.MySQLDialect16             //数据库驱动17             #hibernate.connection.driver_class com.mysql.jdbc.Driver18             //数据库连接url19             #hibernate.connection.url jdbc:mysql:///test20             //连接用户名21             #hibernate.connection.username gavin22             //连接密码23             #hibernate.connection.password24          -->25             <property>com.mysql.jdbc.Driver</property>26             <property>jdbc:mysql:///hibernate_54</property>27             <property>root</property>28             <property>1234</property>29             <property>org.hibernate.dialect.MySQLDialect</property>30         <!-- 可选配置 
31             //是否在控制台显示hibernate生成的sql32             hibernate.show_sql true33             //是否对显示到控制台的sql语句格式化34             hibernate.format_sql true35             //自动建表36             # create(测试时使用)        :  自动建表,每次启动hibernate的时候都会自动建表.37             # create-drop(测试时使用)     :  自动建表,每次启动hibernate的时候都会自动建表.释放资源时会将所有表删除.38             # update(常用)    :  自动建表,有表就不会再创建,如果已经存在的表不完全匹配.会自动修改表结构.39             # validate        :  校验表结构.不会自动建表.每次hibernate启动时都会检查表结构是否正确.40                                         //不正确=>抛出异常.41         -->42             <property>true</property>43             <property>true</property>44             45             <property>update</property>46             47             <!-- 指定数据库隔离级别 
48                 ## specify a JDBC isolation level49                 #hibernate.connection.isolation 450                 mysql 默认级别是451                 Oracle 默认级别是252             -->53             <property>4</property>54             <!-- 配置session与当前线程绑定 -->55             <property>thread</property>56             57             58         <!-- 映射引入配置 
59                 resource属性:填写引入映射文件的路径. 相对于src目录下.60         -->61             <mapping></mapping>62         </session-factory>63     </hibernate-configuration>

  

The above is the detailed content of Detailed explanation of the steps to build the hibernate framework environment. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.