Hibernate with Spring Boot: Difference between revisions

From My Limbic Wiki
Line 32: Line 32:
  spring.datasource.'''password'''=password
  spring.datasource.'''password'''=password


'''#Optionnal'''
  # Enabling H2 Console
  # Enabling H2 Console
  spring.h2.console.enabled=true
  spring.h2.console.enabled=true

Revision as of 18:01, 23 September 2019

Hibernate ORM handles object-relational impedance mismatch problems by replacing direct, persistent database accesses with high-level object handling functions.

Vulgarization

Dependencies

pom.xml file

org.springframework.boot.spring-boot-starter-web
org.springframework.boot.spring-boot-starter-data-jpa
com.h2database.h2

Entity

@Entity
@Table(name="TABLE_NAME")
public class TableNameEntity{
   @Id
   @GeneratedValue
   private Long id;
   @Column(name="column_name", nullable=false, length=200)
   @Override
   public String toString() { ... }
}

Repository

@Repository
public interface TableNameRepository 
   extends JpaRepository<TableNameEntity, Long> { 
   ...
}

Datasource Configuration

application.properties
#Required
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=user
spring.datasource.password=password
#Optionnal
# Enabling H2 Console
spring.h2.console.enabled=true
# Custom H2 Console URL
spring.h2.console.path=/h2-console

# create database schema from SQL files
spring.jpa.hibernate.ddl-auto=none
#Turn Statistics on and log SQL stmts
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.generate_statistics=false 
#logging.level.org.hibernate.type=trace
#logging.level.org.hibernate.stat=debug
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n