Table of Contents
Hibernate文件
基本映射:
Hibernate的主键生成策略种类
总结:
Home Database Mysql Tutorial Hibernate基础映射

Hibernate基础映射

Jun 07, 2016 pm 04:09 PM
hibernate Base mapping

在说Hibernate映射前,我们先来了解下对象关系映射 ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对这些对象的操作。我们来看一张图 vcHLudjPtcr9vt26zbbUz/PK/b7d1q685LXE07PJ5KOs

在说Hibernate映射前,我们先来了解下对象关系映射 ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对这些对象的操作。我们来看一张图

vcHLudjPtcr9vt26zbbUz/PK/b7d1q685LXE07PJ5KOsv8nS1M2ouf3Ts8nkudjPtdfUtq+y+sn6U1FM0+++5KOs1NrStc7xwt+8rbLjus3K/b7dsuPWrrzks+S1scfFwbqhozwvcD4KPGgyPkhpYmVybmF0ZdOzyeQ8L2gyPgo8cD4gPGltZyBzcmM9"http://www.2cto.com/uploadfile/Collfiles/20141110/20141110091545138.jpg" alt="\">

Hibernate文件

  1. 映射类(*.java):它是描述数据库表的结构,表中的字段在类中被描述成属性,将来就可以实现把表中的记录映射成为该类的对象了。
  2. 映射文件(*.hbm.xml):它是指定数据库表和映射类之间的关系,包括映射类和数据库表的对应关系、表字段和类属性类型的对应关系以及表字段和类属性名称的对应关系等。
  3. 数据库配置文件(*.properties/*.cfg.xml):它是指定与数据库连接时需要的连接信息,比如连接哪种数据库、登录数据库的用户名、登录密码以及连接字符串等。当然还可以把映射类的地址映射信息放在这里。

    基本映射:

    具体看操作

    1映射实体类

    //默认空构造函数的重要性
    public class User {
    
    	public User() {
    		// TODO Auto-generated constructor stub
    	}
    	
    	public User(String id,String name){
    		this.id=id;
    		this.name=name;
    		
    	}
    	private String id;
    	public String getId() {
    		return id;
    	}
    	public void setId(String id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	public Date getCreateTime() {
    		return createTime;
    	}
    	public void setCreateTime(Date createTime) {
    		this.createTime = createTime;
    	}
    	public Date getExprieTime() {
    		return exprieTime;
    	}
    	public void setExprieTime(Date exprieTime) {
    		this.exprieTime = exprieTime;
    	}
    
    	private String name;
    	private String password;
    	private Date createTime;
    	private Date exprieTime;
    
    }
    
    Copy after login

    实体类的设计原则:

    * 实现无参的默认的构造函数

    * 提供一个标识

    *建议不要使用final修饰实体类(因为采用load延时加载数据的时候会继承实体类生成代理对象)

    *建议为实体类生成getter和setter方法(如果不使用,需要用属性field标识)

    2映射文件User.hbm.xml

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
    	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
     <!--1、class和table的映射,name属性是实体名,table属性是表名(table可省略,则name即是映射的表名)--> 
    	<class name="com.bjpowernode.hibernate.User">
    	<!-2、主键映射,name属性是实体类的标识符属性,对应table的主键,即用column表示(column同样可省略)-->  
    		<id name="id" access="field">
    			<!--主键生成器,class属性表示生成策略,根据不同的需求选择-->   
    			<generator class="uuid"/>
    		</id>
    		 <!--3、其他属性的映射 property-->  
    		<property name="name" length="40" unique="true" />
    		<property name="password"/>
    		<property name="createTime"/>
    		<property name="exprieTime"/>
    		<filter name="testFiltere"  condition="id < :myid"></filter>
    	</class>
    </hibernate-mapping>
    Copy after login

    3hibernate.cfg.xml配置文件

    	<!DOCTYPE hibernate-configuration PUBLIC
    		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    	
    	<hibernate-configuration>
    		<session-factory>
    			<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    			<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernast_test</property>
    			<property name="hibernate.connection.username">root</property>
    			<property name="hibernate.connection.password">hanhan</property>
    			<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    			<!--打印sql-->
    			<property name="hibernate.show_sql">true</property>
    			<!--在没有表的时候,创建sessionfactroy 时,就会去创建表(update的方式,不删除原有数据)-->
    			<property name="hibernate.hbm2ddl.auto">update</property>
    			
    		<mapping  resource="com/bjpowernode/hibernate/User.hbm.xml"/>
    		</session-factory>
    	</hibernate-configuration>
    Copy after login

    Hibernate的主键生成策略种类

    手动:Assigned

    Hibernate主动:uuid

    数据库交互:

    需要和数据库交互以生成id的:guid、identity、sequence、native、foreign

    说明:需要和数据库交互生成,需要经过一次查询才能生成

    Guid,identity:MySQL,SQLserver的生成方式

    sequence:Oracle,db2的生成方式,自增序列

    native:identity+sequence,跨平台

    foreign:只适用基于共享主键的一对一关联映射的时候使用。即一个对象的主键是参照的另一张表的主键生成的。

    总结:

    Hibernate的基本映射:重点是对主键生成策略的认识,根据不同数据库选择不同的方式,重要理解。

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to integrate Hibernate in SpringBoot project How to integrate Hibernate in SpringBoot project May 18, 2023 am 09:49 AM

Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

Java Errors: Hibernate Errors, How to Handle and Avoid Java Errors: Hibernate Errors, How to Handle and Avoid Jun 25, 2023 am 09:09 AM

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

Selected Java JPA interview questions: Test your mastery of the persistence framework Selected Java JPA interview questions: Test your mastery of the persistence framework Feb 19, 2024 pm 09:12 PM

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

What are the differences between hibernate and mybatis What are the differences between hibernate and mybatis Jan 03, 2024 pm 03:35 PM

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate May 27, 2023 pm 05:06 PM

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

Introduction to Hibernate framework in Java language Introduction to Hibernate framework in Java language Jun 10, 2023 am 11:35 AM

Hibernate is an open source ORM framework that binds the data mapping between relational databases and Java programs to each other, making it easier for developers to access data in the database. Using the Hibernate framework can greatly reduce the work of writing SQL statements and improve the development efficiency and reusability of applications. Let's introduce the Hibernate framework from the following aspects. 1. Advantages of the Hibernate framework: object-relational mapping, hiding database access details, making development

How does Hibernate second level cache work? How does Hibernate second level cache work? Sep 14, 2023 pm 07:45 PM

Caching helps reduce database network calls when executing queries. Level 1 cache and session linking. It is implemented implicitly. The first level cache exists until the session object exists. Once the session object is terminated/closed, there will be no cached objects. Second level cache works for multiple session objects. It is linked with the session factory. Second level cache objects are available to all sessions using a single session factory. These cache objects will be terminated when a specific session factory is closed. To implement the second level cache we need to add the following dependencies to use the second level cache. <!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

See all articles