DEV Community

Cover image for Hibernate Cheat Sheet
Burak Boduroğlu
Burak Boduroğlu

Posted on

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✅

Top comments (2)

Collapse
 
aman1234 profile image
AMAN1234

nice

Collapse
 
mnzit profile image
Manjit Shakya

nice one