Hibernate基础映射
在说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文件
- 映射类(*.java):它是描述数据库表的结构,表中的字段在类中被描述成属性,将来就可以实现把表中的记录映射成为该类的对象了。
- 映射文件(*.hbm.xml):它是指定数据库表和映射类之间的关系,包括映射类和数据库表的对应关系、表字段和类属性类型的对应关系以及表字段和类属性名称的对应关系等。
- 数据库配置文件(*.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 login3hibernate.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 loginHibernate的主键生成策略种类
手动:Assigned
Hibernate主动:uuid
数据库交互:
需要和数据库交互以生成id的:guid、identity、sequence、native、foreign
说明:需要和数据库交互生成,需要经过一次查询才能生成
Guid,identity:MySQL,SQLserver的生成方式
sequence:Oracle,db2的生成方式,自增序列
native:identity+sequence,跨平台
foreign:只适用基于共享主键的一对一关联映射的时候使用。即一个对象的主键是参照的另一张表的主键生成的。
总结:
Hibernate的基本映射:重点是对主键生成策略的认识,根据不同数据库选择不同的方式,重要理解。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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.

In this article, we will see how to perform bulk insert/update in Hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call. To understand and implement this, let us define our entity − @EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

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

Java framework technology stack: Introducing commonly used Java frameworks, such as SpringMVC, Hibernate, MyBatis, etc. With the continuous development of Java, more and more frameworks have been developed to simplify the development process. Among them, SpringMVC, Hibernate, MyBatis, etc. are one of the most commonly used frameworks in Java development. This article will introduce the basic concepts and usage of these frameworks to help readers better understand and apply these frameworks. First, let’s introduce Sp
