-
Notifications
You must be signed in to change notification settings - Fork 0
Hibernate Tutorial
sulabh84 edited this page Aug 26, 2025
·
1 revision
==========================================================
https://www.geeksforgeeks.org/java/hibernate-tutorial/
==========================================================
----------------------------------------------------------------
Batch Insert/Update
spring.jpa.properties.hibernate.jdbc.batch_size=5
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.batch_versioned_data=true
https://www.baeldung.com/jpa-hibernate-batch-insert-update
@PersistenceContext
private EntityManager entityManager;
entityManager.persist(employee)
entityManager.flush()
entityManager.clear()
---------------------------------------------------------------
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
List<Product> findAllByPrice(double price, Pageable pageable);
}
Pageable firstPageWithTwoElements = PageRequest.of(0, 2);
Pageable secondPageWithFiveElements = PageRequest.of(1, 5);
productRepository.findAll(Sort.by("name"));
----------------------------------------------------------------
By default, data.sql scripts get executed before the Hibernate is initialized.
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
schema.sql File
spring.jpa.hibernate.ddl-auto=none
https://www.baeldung.com/spring-boot-data-sql-and-schema-sql
-------------------------------------------------------------------
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "student_gfg_detail_id")
private StudentGfgDetail studentGfgDetail;
@OneToOne(mappedBy = "studentGfgDetail",
cascade = CascadeType.ALL)
private Student student;
mappedBy = "studentGfgDetail" tells Hibernate to look for a field named studentGfgDetail in the Student class and link that particular instance to the current student object.
-------------------------------------------------------------------
// A manufacturer can have many models
@OneToMany(mappedBy = "manufacturer")
private List<Model> models;
// A model belongs to one manufacturer
// Foreign key referencing the manufacturer table
@ManyToOne
@JoinColumn(name = "manufacture_id")
private Manufactures manufacturer;
--------------------------------------------------------------------
@ManyToMany
@JoinTable(
name = "course_like",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id"))
Set<Course> likedCourses;
@ManyToMany(mappedBy = "likedCourses")
Set<Student> likes;
new Attribute to the relation table
@Embeddable
class CourseRatingKey implements Serializable {
@Column(name = "student_id")
Long studentId;
@Column(name = "course_id")
Long courseId;
// standard constructors, getters, and setters
// hashcode and equals implementation
}
@Entity
class CourseRating {
@EmbeddedId
CourseRatingKey id;
@ManyToOne
@MapsId("studentId")
@JoinColumn(name = "student_id")
Student student;
@ManyToOne
@MapsId("courseId")
@JoinColumn(name = "course_id")
Course course;
int rating;
// standard constructors, getters, and setters
}
class Student {
// ...
@OneToMany(mappedBy = "student")
Set<CourseRating> ratings;
// ...
}
class Course {
// ...
@OneToMany(mappedBy = "course")
Set<CourseRating> ratings;
// ...
}
https://www.baeldung.com/jpa-many-to-many
--------------------------------------------------------------------------------------------------
Interview question -
Lazy loading in oneToMany relationship
Used Hibernate.initialize(user.getContracts()) - this should be called within transaction
--------------------------------------------------------------------------------------------------