February 23rd, 2023
Hibernate – JPA
pom.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
3.0.2
com.brains.jpa.hibernate
jpa-hibernate-demo
0.0.1-SNAPSHOT
jpa-hibernate-demo
Demo project for Spring Boot
17
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-starter-web
com.h2database
h2
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
These annotions are provided by Hibernate which can track the timestamp of created and updated entries. JPA does not provide these annotations.
@UpdateTimestamp
@CreationTimestamp
package com.brains.jpa.hibernate.jpahibernatedemo.entity;
import java.time.LocalDateTime;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table
public class Course {
@Id
@GeneratedValue
private Long id;
private String name;
@UpdateTimestamp
private LocalDateTime lastUpdatedDate;
@CreationTimestamp
private LocalDateTime createdDate;
public Course(String name) {
this.name = name;
}
}
application.properties
spring.datasource.url=jdbc:h2:mem:testdb;NON_KEYWORDS=USER;DB_CLOSE_ON_EXIT=FALSE
spring.h2.console.enabled=true
#spring.data.jpa.repositories.bootstrap-mode=default
spring.jpa.defer-datasource-initialization=true
# Turn Statistics ON
spring.jpa.properties.hibernate.generate_statistics=true
logging.level.org.hibernate.stat=debug
logging.level.org.hibernate=debug
# Show all queries
spring.jpa.show-sql=true
# Format the queries
spring.jpa.properties.hibernate.format_sql=true
# Enable seeing the parameters with query
logging.level.org.hibernate.type=trace
Normally you should use CURRENT_DATE
, CURRENT_TIMESTAMP
, or LOCALTIMESTAMP
in H2. They all have standard-compliant implementations and work as expected.
H2 should probably reject SYSDATE
and all similar functions in all compatibility modes, with exception for Oracle compatibility mode in which it should be a keyword.
data.sql
insert into course(id, name, created_date, last_updated_date)
values(10001, 'JPA in 5 steps', LOCALTIMESTAMP, LOCALTIMESTAMP);
insert into course(id, name, created_date, last_updated_date)
values(10002, 'JDBC in 10 steps', LOCALTIMESTAMP, LOCALTIMESTAMP);
insert into course(id, name, created_date, last_updated_date)
values(10003, 'JPQL in 50 steps', LOCALTIMESTAMP, LOCALTIMESTAMP);
package com.brains.jpa.hibernate.jpahibernatedemo.repository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.brains.jpa.hibernate.jpahibernatedemo.entity.Course;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
@Repository
@Transactional
public class CourseRepository {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
EntityManager em;
public Course findById(Long id) {
return em.find(Course.class, id);
}
public void deleteById(Long id) {
Course course = findById(id);
em.remove(course);
}
public Course save(Course course) {
if(course.getId() == null) {
//insert
em.persist(course);
}else {
//update
em.merge(course);
}
return course;
}
public void playWithEntityManager() {
Course course1 = new Course("AngularJs in 100 Steps");
em.persist(course1);
Course course2 = findById(10001L);
course2.setName("JPA in 5 steps -- updated");
}
}
package com.brains.jpa.hibernate.jpahibernatedemo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.brains.jpa.hibernate.jpahibernatedemo.repository.CourseRepository;
@SpringBootApplication
public class JpaHibernateDemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(JpaHibernateDemoApplication.class, args);
}
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private CourseRepository courseRepository;
@Override
public void run(String... args) throws Exception {
courseRepository.playWithEntityManager();
}
}
Leave a Reply