Logo

dev-resources.site

for different kinds of informations.

Hibernate Cheat Sheet

Published at
11/12/2023
Categories
java
webdev
hibernate
springboot
Author
burakboduroglu
Categories
4 categories in total
java
open
webdev
open
hibernate
open
springboot
open
Author
14 person written this
burakboduroglu
open
Hibernate Cheat Sheet

## 📃 JPA Hibernate Annotations

@Entity :

  • This annotation is used to mark a class as an entity class.
  • This annotation is used to create a table in the database.
@Entity
public class Brand {
}
Enter fullscreen mode Exit fullscreen mode

@Table :

  • @Table annotation is used to specify the details of the table that will be created in the database.
  • The name attribute of the @Table annotation is used to specify the name of the table.
@Entity
@Table(name = "brands")
public class Brand {
}
Enter fullscreen mode Exit fullscreen mode

@Column :

  • @Column annotation is used to specify the details of the column that will be created in the database.
  • The name attribute of the @Column annotation is used to specify the name of the column.


@Entity
@Table(name = "brands")
public class Brand {

    @Column(name = "brandName")
    private String brandName;
}
Enter fullscreen mode Exit fullscreen mode

@id :

  • @id annotation is used to specify the primary key of an entity.
  • The @id annotation is always used with the @GeneratedValue annotation.
@Entity
@Table(name = "brands")
public class Brand {
    @Id
    @Column(name = "id")
    private int id;

}
Enter fullscreen mode Exit fullscreen mode

@ManyToOne :

  • @ManyToOne annotation is used to specify many to one relationship with another entity.
@Entity
@Table(name = "brands")
public class Brand {
    @ManyToOne
    @JoinColumn(name = "brandsDetails")
    private BrandDetail brandDetail;
}
Enter fullscreen mode Exit fullscreen mode

@OneToMany :

  • @OneToMany annotation is used to specify one to many relationship with another entity.
  • The mappedBy attribute of the @OneToMany annotation is used to specify the property of the entity that is the owner of the relationship.
@Entity
@Table(name = "brands")
public class Brand {

    @OneToMany(mappedBy = "brands", fetch = FetchType.EAGER)
    private BrandDetail brandDetail;
}
Enter fullscreen mode Exit fullscreen mode

@PrimaryKeyJoinColumn :

  • @PrimaryKeyJoinColumn annotation is used to specify the primary key of the entity that is the owner of the relationship.
@Entity
@Table(name = "brands")
public class Brand {

    @PrimaryKeyJoinColumn
    private int id;
}
Enter fullscreen mode Exit fullscreen mode

@JoinColumn :

  • @JoinColumn annotation is used to specify the column that will be created in the database as a foreign key.
@Entity
@Table(name = "brands")
public class Brand {

    @JoinColumn(name = "brandDetail")
    private BrandDetail brandDetail;
}
Enter fullscreen mode Exit fullscreen mode

@JoinTable ve @MapsId:

  • It is used to specify the join table that will be created in the database.
  • @JoinTable annotation is used to specify the join table that will be created in the database.
  • @MapsId annotation is used to specify the primary key of the entity that is the owner of the relationship.
@Entity
@Table(name = "brands")
public class Brand {

    @JoinTable(name = "brands")
    private BrandDetail brandDetail;
}
Enter fullscreen mode Exit fullscreen mode

@OneToOne:

  • @OneToOne annotation is used to specify one to one relationship with another entity.
  • The mappedBy attribute of the @OneToOne annotation is used to specify the property of the entity that is the owner of the relationship.
@Entity
@Table(name = "brands")
public class Brand {

    @OneToOne(mappedBy = "brands")
    private BrandDetail brandDetail;
}
Enter fullscreen mode Exit fullscreen mode

✅ If you like this article, you can give me a like on. 😎
Thanks for reading. 🙏

Spring Boot Guide

Visit my page✅

hibernate Article's
30 articles in total
Favicon
JOOQ Is Not a Replacement for Hibernate. They Solve Different Problems
Favicon
Persistence Context в Hibernate Zoo: путешествие объекта по жизненным состояниям
Favicon
Unidirectional associations for one-to-many
Favicon
Como eu reduzi em até 99% o tempo de resposta da minha API
Favicon
Hibernate Zoo: Жадный Гиппопотам и Ленивый Лемур (Lazy vs Eager)
Favicon
🐾 Hibernate Zoo: Путеводитель по языкам запросов в мире данных 🐾
Favicon
How To Fetch Data By Using DTO Projection In Spring Data JPA
Favicon
Ubuntu 22.04 Hibernate Using Swap File
Favicon
Зоопарк Hibernate: N+1 запросов или как накормить жадного бегемота
Favicon
Spring Data JPA Stream Query Methods
Favicon
Uma breve introdução ao Hibernate
Favicon
Ubuntu hibernate
Favicon
Eager vs Lazy Initialization of Spring Beans
Favicon
Understanding JPA Mappings in Spring Boot: One-to-One, One-to-Many, Many-to-One, and Many-to-Many Relationships
Favicon
Java Hibernate vs JPA: Rapid review for you
Favicon
Hibernate Connection Library with GUI Generation
Favicon
what is JPA? explain few configurations
Favicon
Demystifying Hibernate: A Beginner's Journey
Favicon
How to deal with N+1 problems with Hibernate
Favicon
Java Hibernate vs JPA: Quick Review
Favicon
Uppercase table names in Spring Boot
Favicon
Hiring Alert - Java Developer- Blockchain
Favicon
H2 database Setup Error Unable to load name org.hibernate.dialect.Oracle10gDialect
Favicon
Capitalisation of table name generated by Hibernate when using MySQL server
Favicon
Display SQL statement generated by Hibernate JPA in Spring Boot environment
Favicon
Advanced and dynamic searching with Spring Data JPA
Favicon
Defining JPA/Hibernate Entities in Kotlin
Favicon
Criteria Queries and JPA Metamodel with Spring Boot and Kotlin
Favicon
How to save Hibernate Entity changes to Database
Favicon
Hibernate Cheat Sheet

Featured ones: