Home> Java> javaTutorial> body text

How do annotations map database tables and objects in Hibernate?

WBOY
Release: 2024-05-06 16:48:01
Original
979 people have browsed it

Hibernate uses annotations to map Java classes to database tables. The steps include: adding dependencies, importing annotations, creating entity classes, and mapping properties. For example, the user entity class User is mapped to the users table and the id, username, and password columns are defined. The annotations @Id, @GeneratedValue, @Table, and @Column are used to specify the primary key, primary key generation strategy, table name, and column attributes. This mapping simplifies the interaction between objects and persistence, and Hibernate automatically handles object persistence and retrieval.

How do annotations map database tables and objects in Hibernate?

How annotations map database tables and objects in Hibernate

Hibernate is a popular object-relational mapping (ORM) framework that uses annotations to map database tables and objects. Java classes map to database tables. This eliminates tedious manual mapping and simplifies the interaction between models and persistence.

Steps:

  1. Add Hibernate dependency:

     org.hibernate hibernate-core 5.6.4.Final 
    Copy after login
  2. Import necessary annotations:

    import javax.persistence.*;
    Copy after login
  3. Create entity class:

    @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String username; private String password; }
    Copy after login
  4. Use annotation mapping Attributes:
  • @Id:Marks the primary key field.
  • @GeneratedValue:Specify the primary key generation strategy.
  • @Table:Specify the table name.

Practical case:

Consider a simple user table with the following columns:

  • id: Auto-increment primary key
  • username: String
  • password: String

## Java code:

@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username", nullable = false, length = 50) private String username; @Column(name = "password", nullable = false, length = 100) private String password; }
Copy after login

SQL table:

CREATE TABLE users ( id INT NOT NULL AUTO_INCREMENT, username VARCHAR(50) NOT NULL, password VARCHAR(100) NOT NULL, PRIMARY KEY (id) );
Copy after login
Now the Java entity class

Useris mapped to the database tableusers. Hibernate can automatically handle persistence and retrieval of objects to the database.

The above is the detailed content of How do annotations map database tables and objects in Hibernate?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!