Learn JPA and Hibernate

https://start.spring.io/

in given above URL can  

User Given Below Configuration to Create Spring Boot Project with given dependencies to enable JPA and Hibernate

  1. Spring Web
  2. Spring Data JDBC
  3. Spring Data JPA
  4. H2 Database

<dependencies>

    <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jdbc</artifactId>

    </dependency>

    <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

    </dependency>

    <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

    </dependency>


    <dependency>

<groupId>com.h2database</groupId>

<artifactId>h2</artifactId>

<scope>runtime</scope>

    </dependency>

    <dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

    </dependency>

</dependencies>



Step 2:
By Running the Project you will find following logs which will tell you that your project is having your JPA JDBC and H2 database configured

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

:: Spring Boot :: (v3.2.0-M1)


2023-08-09T14:10:19.063+05:30 INFO 13596 --- [ main] c.i.s.l.LearnJpaAndHibernateApplication : Starting LearnJpaAndHibernateApplication using Java 19.0.2 with PID 13596 (D:\in28MinutesMicroServices\learn-jpa-and-hibernate\learn-jpa-and-hibernate\target\classes started by irfan in D:\in28MinutesMicroServices\learn-jpa-and-hibernate\learn-jpa-and-hibernate)

2023-08-09T14:10:19.066+05:30 INFO 13596 --- [ main] c.i.s.l.LearnJpaAndHibernateApplication : No active profile set, falling back to 1 default profile: "default"

2023-08-09T14:10:20.053+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode

2023-08-09T14:10:20.056+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JDBC repositories in DEFAULT mode.

2023-08-09T14:10:20.085+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20 ms. Found 0 JDBC repository interfaces.

2023-08-09T14:10:20.108+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode

2023-08-09T14:10:20.109+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.

2023-08-09T14:10:20.119+05:30 INFO 13596 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 1 ms. Found 0 JPA repository interfaces.

2023-08-09T14:10:21.021+05:30 INFO 13596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)

2023-08-09T14:10:21.034+05:30 INFO 13596 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]

2023-08-09T14:10:21.035+05:30 INFO 13596 --- [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.11]

2023-08-09T14:10:21.183+05:30 INFO 13596 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext

2023-08-09T14:10:21.186+05:30 INFO 13596 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2037 ms

2023-08-09T14:10:21.398+05:30 INFO 13596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...

2023-08-09T14:10:21.689+05:30 INFO 13596 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Added connection conn0: url=jdbc:h2:mem:ae06520d-cb29-4bd2-a75b-3f400bf37fc9 user=SA

2023-08-09T14:10:21.692+05:30 INFO 13596 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.

2023-08-09T14:10:21.765+05:30 INFO 13596 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]

2023-08-09T14:10:21.856+05:30 INFO 13596 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 6.2.6.Final

2023-08-09T14:10:21.860+05:30 INFO 13596 --- [ main] org.hibernate.cfg.Environment : HHH000406: Using bytecode reflection optimizer

2023-08-09T14:10:22.098+05:30 INFO 13596 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy

2023-08-09T14:10:22.292+05:30 INFO 13596 --- [ main] o.s.o.j.p.SpringPersistenceUnitInfo : No LoadTimeWeaver setup: ignoring JPA class transformer

2023-08-09T14:10:22.519+05:30 INFO 13596 --- [ main] o.h.b.i.BytecodeProviderInitiator : HHH000021: Bytecode provider name : bytebuddy

2023-08-09T14:10:22.868+05:30 INFO 13596 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]

2023-08-09T14:10:22.883+05:30 INFO 13596 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'

2023-08-09T14:10:22.950+05:30 WARN 13596 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning

2023-08-09T14:10:23.716+05:30 INFO 13596 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path ''

2023-08-09T14:10:23.731+05:30 INFO 13596 --- [ main] c.i.s.l.LearnJpaAndHibernateApplication : Started LearnJpaAndHibernateApplication in 5.309 seconds (process running for 6.113)


Given Above logs you can find 6 major things 

  1. Intialization Bootstrapping Spring Data JDBC
  2. Intialization Bootstrapping Spring Data JPA
  3. Connection Pool setup HikariPool-1 - Start completed
  4. Hibernate Configuration Hibernate ORM core
  5. H2 Database in memory database Runing on this url url=jdbc:h2:mem:ae06520d-cb29-4bd2-a75b-3f400bf37fc9 user=SA
  6. At the end Tomcat is launched Tomcat started on port 8080
For Step 5 H2 Database
we have to add the application.properties property

spring.h2.console.enabled=true

#this will eneble the H2 database configuration

http://localhost:8080/h2-console

run given aboce URL to get the H2 Database console





Note: always JDBC URL is incorrect

So make sure that you check this URL every time 
As of now we have not configured Any URL for the H2 Database
Thats by H2 Database JDBC URL generated Dynamicaly in Logs

jdbc:h2:mem:2f7f7564-afc0-48ac-bad2-a98dc04a5ccb



Click Connect


So as Dynamicaly generating H2 Database URL is bit annoying to configure to your H2 Console better we can congure static url using application.properties file 


spring.datasource.url=jdbc:h2:mem:testdb


like above in application.properties file

(stop and restart application)


Now open your H2 Database URl with given application.peroperties file URL


For creating Table in H2 database we can do  with given below Steps

Step 1:
Create file Schema.sql in resource folder
Add Given Below content to add Course Table with id name authoer and Id as primary Key

create table course

(

id bigint not null,

name varchar(255) not null,

author varchar(255) not null,

primary key (id)

);


with this file now while you start your server it will picked up schema.sql file to create course table in database


Now can see COURSE table in data base

with Given coloumn 
ID, Name, Author

Also can Query with given above select query


Also insert the data by given below query 

 insert into course (id,name,author)
values(1,'Learn Java', 'khanDev') 


Difference b/w JDBC and Spring JDBC is 
Ans: less Java code need to be write
JDBC:
if you using JDBC than need to write each and every thing like preparedStatement , than statement need to execute and try and catch and each and every thing to close into finally block and all
Spring JDBC:
In spring Boot Spring JDBC need to write very small java code 
public void deleteToDO(int id) {
    jdbcTemplate.update("delete from todo where id=?", id);
}


Comments

Popular posts from this blog

Introduction of RESTful Web Service

Implementing Dynamic Filtering for Rest API