BAEL-2709: Implementing example for Spring Boot Hibernate. (#6478)

This commit is contained in:
dionisPrifti 2019-03-11 13:43:15 +01:00 committed by Eugen
parent 3c6e9001c6
commit 6cd8c51b4d
9 changed files with 140 additions and 1 deletions

View File

@ -0,0 +1,14 @@
package com.baeldung.springboothibernate.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ExampleApplication {
public static void main(String[] args) {
SpringApplication.run(ExampleApplication.class, args);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.springboothibernate.application.datasources;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DataSourceBean {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.springboothibernate.application.models;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
private String name;
public Book() {
super();
}
public Book(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.springboothibernate.application.repositories;
import com.baeldung.springboothibernate.application.models.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
}

View File

@ -0,0 +1,19 @@
package com.baeldung.springboothibernate.application.services;
import com.baeldung.springboothibernate.application.models.Book;
import com.baeldung.springboothibernate.application.repositories.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
public List<Book> list() {
return bookRepository.findAll();
}
}

View File

@ -3,3 +3,9 @@ spring.datasource.url = jdbc:hsqldb:mem:test;DB_CLOSE_DELAY=-1
spring.datasource.username = sa spring.datasource.username = sa
spring.datasource.password = spring.datasource.password =
spring.jpa.hibernate.ddl-auto = create spring.jpa.hibernate.ddl-auto = create
# Enabling H2 Console
spring.h2.console.enabled=true
# Uppercase Table Names
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

View File

@ -0,0 +1,27 @@
package com.baeldung.springboothibernate.application.tests;
import com.baeldung.springboothibernate.application.models.Book;
import com.baeldung.springboothibernate.application.services.BookService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BookServiceUnitTest {
@Autowired
private BookService bookService;
@Test
public void test() {
List<Book> books = bookService.list();
Assert.assertEquals(books.size(), 3);
}
}

View File

@ -12,5 +12,5 @@ hibernate.cache.use_second_level_cache=true
hibernate.cache.use_query_cache=true hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory
spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql spring.jpa.properties.hibernate.hbm2ddl.import_files=migrated_users.sql, import_books.sql
spring.datasource.data=import_*_users.sql spring.datasource.data=import_*_users.sql

View File

@ -0,0 +1,3 @@
insert into book values(1, 'The Tartar Steppe');
insert into book values(2, 'Poem Strip');
insert into book values(3, 'Restless Nights: Selected Stories of Dino Buzzati');