Introduction to ORM mapping using annotations
In the past two years, more projects have used annotations for ORM mapping of some relatively stable entity classes, which makes programming more concise and simple. In fact, there are not many changes in the operation process between using annotations for ORM mapping and using xml for mapping. The main steps are: importing packages, writing annotated entity classes, writing core configuration files, and writing test classes.
The project code has been uploaded to GitHub: Project name: HibernateUseAnnotation
The entire project structure is as follows:
1. Guide package
The author is using the MySQL database , so the imported data package is as shown below:
2. Write entity classes
The annotations used for mapping are basically defined in the javax.persistence.* package. The main ones I commonly use are the following four :
1 package com.rocky.domain; 2 3 import javax.persistence.Entity; 4 import javax.persistence.Id; 5 import javax.persistence.Table; 6 7 @Entity 8 @Table(name = "tb_user") 9 public class User10 {11 @Id12 private Integer uId;13 private String uName;14 private Integer uAge;15 public User()16 {17 super();18 }19 @Override20 public String toString()21 {22 return "User [uId=" + uId + ", uName=" + uName + ", uAge=" + uAge + "]";23 }24 public Integer getuId()25 {26 return uId;27 }28 public void setuId(Integer uId)29 {30 this.uId = uId;31 }32 public String getuName()33 {34 return uName;35 }36 public void setuName(String uName)37 {38 this.uName = uName;39 }40 public Integer getuAge()41 {42 return uAge;43 }44 public void setuAge(Integer uAge)45 {46 this.uAge = uAge;47 }48 }
3. Write the core configuration file
The configuration file using annotations is basically the same as the configuration file mapped by xml, but the attribute used when introducing external mapping is class instead of resource attribute.
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 9 <!--指定数据库的参数 -->10 <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>11 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>12 <property name="hibernate.connection.url">jdbc:mysql://192.168.100.100:3306/test</property>13 <property name="hibernate.connection.username">root</property>14 <property name="hibernate.connection.password">1</property>15 16 <!--指定hibernate是否显式SQL语句及其格式化 -->17 <property name="hibernate.show_sql">true</property>18 <property name="hibernate.format_sql">true</property>19 20 <!--指定表的生成方式-即是否由hibernate自动创建表,其取值为:create-drop、21 create、validate、update-->22 <property name="hibernate.hbm2ddl.auto">update</property>23 24 <!--引入 ORM配置文件 -->25 <mapping class="com.rocky.domain.User"/>26 </session-factory>27 28 </hibernate-configuration>
4. Write test class
1 package com.rocky.testdriver; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 8 import com.rocky.domain.User; 9 10 public class TestDriver11 {12 13 public static void main(String[] args)14 {15 // 加载配置文件16 Configuration cfg = new Configuration();17 cfg.configure();18 // 创建会话工厂19 SessionFactory factory = cfg.buildSessionFactory();20 // 获取非线程绑定会话21 Session session = factory.openSession();22 // 开启事务23 Transaction tx = session.beginTransaction();24 // 创建User对象25 User user = new User();26 user.setuId(1);27 user.setuName("rocky");28 user.setuAge(28);29 30 // 将数据插入数据库31 session.save(user);32 33 // 提交事务34 tx.commit();35 // 关闭会话36 session.close();37 }38 39 }
The above is the detailed content of Introduction to ORM mapping using annotations. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











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

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

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

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.

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

Python is a versatile programming language that is popular in the field of data analysis and machine learning. Its simplicity, readability, and rich library make it ideal for handling complex data tasks. One such powerful application is RFM analysis, a technique used in marketing to segment customers based on their purchasing behavior. In this tutorial, we will guide you through the process of implementing RFM analysis using Python. We will start by explaining the concept of RFM analysis and its importance in marketing. We will then gradually dive into the practical aspects of RFM analysis using Python. In the next part of the article, we will demonstrate how to calculate an RFM score for each customer using Python, taking into account the values for recency, frequency, and

Recently, "Black Myth: Wukong" has attracted huge attention around the world. The number of people online at the same time on each platform has reached a new high. This game has achieved great commercial success on multiple platforms. The Xbox version of "Black Myth: Wukong" has been postponed. Although "Black Myth: Wukong" has been released on PC and PS5 platforms, there has been no definite news about its Xbox version. It is understood that the official has confirmed that "Black Myth: Wukong" will be launched on the Xbox platform. However, the specific launch date has not yet been announced. It was recently reported that the Xbox version's delay was due to technical issues. According to a relevant blogger, he learned from communications with developers and "Xbox insiders" during Gamescom that the Xbox version of "Black Myth: Wukong" exists.

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
