Merge remote-tracking branch 'origin/BAEL-3985' into BAEL-3985
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.composite;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class BookApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(BookApplication.class);
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.composite.entity;
|
||||
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@EmbeddedId
|
||||
private BookId id;
|
||||
private String genre;
|
||||
private Integer price;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String author, String name, String genre, Integer price) {
|
||||
BookId id = new BookId(author, name);
|
||||
this.id = id;
|
||||
this.genre = genre;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public BookId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(BookId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getGenre() {
|
||||
return genre;
|
||||
}
|
||||
|
||||
public void setGenre(String genre) {
|
||||
this.genre = genre;
|
||||
}
|
||||
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Integer price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.composite.entity;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
@Embeddable
|
||||
public class BookId implements Serializable {
|
||||
|
||||
private String author;
|
||||
private String name;
|
||||
|
||||
public BookId() {
|
||||
}
|
||||
|
||||
public BookId(String author, String name) {
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
BookId bookId = (BookId) o;
|
||||
return Objects.equals(author, bookId.author) && Objects.equals(name, bookId.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(author, name);
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.composite.repository;
|
||||
|
||||
import com.baeldung.composite.entity.Book;
|
||||
import com.baeldung.composite.entity.BookId;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends JpaRepository<Book, BookId> {
|
||||
|
||||
List<Book> findByIdName(String name);
|
||||
|
||||
List<Book> findByIdAuthor(String author);
|
||||
|
||||
List<Book> findByGenre(String genre);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.schemageneration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AccountApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AccountApplication.class, args);
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.schemageneration;
|
||||
|
||||
import com.baeldung.schemageneration.model.Account;
|
||||
import com.baeldung.schemageneration.model.AccountSetting;
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.MetadataSources;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||
import org.hibernate.tool.schema.TargetType;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class HibernateUtil {
|
||||
|
||||
/**
|
||||
* Generates database create commands for the specified entities using Hibernate native API, SchemaExport.
|
||||
* Creation commands are exported into the create.sql file.
|
||||
*/
|
||||
public static void generateSchema() {
|
||||
Map<String, String> settings = new HashMap<>();
|
||||
settings.put(Environment.URL, "jdbc:h2:mem:schema");
|
||||
|
||||
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(settings).build();
|
||||
|
||||
MetadataSources metadataSources = new MetadataSources(serviceRegistry);
|
||||
metadataSources.addAnnotatedClass(Account.class);
|
||||
metadataSources.addAnnotatedClass(AccountSetting.class);
|
||||
Metadata metadata = metadataSources.buildMetadata();
|
||||
|
||||
SchemaExport schemaExport = new SchemaExport();
|
||||
schemaExport.setFormat(true);
|
||||
schemaExport.setOutputFile("create.sql");
|
||||
schemaExport.createOnly(EnumSet.of(TargetType.SCRIPT), metadata);
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.baeldung.schemageneration.model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "accounts")
|
||||
public class Account {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, length = 100)
|
||||
private String name;
|
||||
|
||||
@Column(name = "email_address")
|
||||
private String emailAddress;
|
||||
|
||||
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
|
||||
private List<AccountSetting> accountSettings = new ArrayList<>();
|
||||
|
||||
public Account() {
|
||||
}
|
||||
|
||||
public Account(String name, String emailAddress) {
|
||||
this.name = name;
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getEmailAddress() {
|
||||
return emailAddress;
|
||||
}
|
||||
|
||||
public void setEmailAddress(String emailAddress) {
|
||||
this.emailAddress = emailAddress;
|
||||
}
|
||||
|
||||
public List<AccountSetting> getAccountSettings() {
|
||||
return accountSettings;
|
||||
}
|
||||
|
||||
public void setAccountSettings(List<AccountSetting> accountSettings) {
|
||||
this.accountSettings = accountSettings;
|
||||
}
|
||||
|
||||
public void addAccountSetting(AccountSetting setting) {
|
||||
this.accountSettings.add(setting);
|
||||
setting.setAccount(this);
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.schemageneration.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "account_settings")
|
||||
public class AccountSetting {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
private String settingName;
|
||||
|
||||
@Column(name = "value", nullable = false)
|
||||
private String settingValue;
|
||||
|
||||
@ManyToOne()
|
||||
@JoinColumn(name ="account_id", nullable = false)
|
||||
private Account account;
|
||||
|
||||
public AccountSetting() {
|
||||
}
|
||||
|
||||
public AccountSetting(String settingName, String settingValue) {
|
||||
this.settingName = settingName;
|
||||
this.settingValue = settingValue;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getSettingName() {
|
||||
return settingName;
|
||||
}
|
||||
|
||||
public void setSettingName(String settingName) {
|
||||
this.settingName = settingName;
|
||||
}
|
||||
|
||||
public String getSettingValue() {
|
||||
return settingValue;
|
||||
}
|
||||
|
||||
public void setSettingValue(String settingValue) {
|
||||
this.settingValue = settingValue;
|
||||
}
|
||||
|
||||
public Account getAccount() {
|
||||
return account;
|
||||
}
|
||||
|
||||
public void setAccount(Account account) {
|
||||
this.account = account;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.schemageneration.repository;
|
||||
|
||||
import com.baeldung.schemageneration.model.Account;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface AccountRepository extends CrudRepository<Account, Long> {
|
||||
Account findByName(String name);
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.schemageneration.repository;
|
||||
|
||||
import com.baeldung.schemageneration.model.AccountSetting;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface AccountSettingRepository extends CrudRepository<AccountSetting, Long> {
|
||||
AccountSetting findByAccountId(Long accountId);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
spring.datasource.url=jdbc:h2:mem:baeldung
|
||||
|
||||
# JPA-Schema-Generation
|
||||
# Use below configuration to generate database schema create commands based on the entity models
|
||||
# and export them into the create.sql file
|
||||
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
|
||||
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=create.sql
|
||||
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-source=metadata
|
||||
#spring.jpa.properties.hibernate.format_sql=true
|
||||
|
||||
spring.jpa.show-sql=true
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.composite.repository;
|
||||
|
||||
import com.baeldung.composite.BookApplication;
|
||||
import com.baeldung.composite.entity.Book;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = BookApplication.class)
|
||||
public class BookRepositoryIntegrationTest {
|
||||
|
||||
public static final String JAVA_101 = "Java101";
|
||||
public static final String JANE = "Jane";
|
||||
public static final String TECH = "Tech";
|
||||
@Autowired
|
||||
BookRepository repository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
Book book1 = new Book("John", JAVA_101, TECH, 20);
|
||||
Book book2 = new Book(JANE, JAVA_101, "Arch", 25);
|
||||
Book book3 = new Book(JANE, "Scala101", TECH, 23);
|
||||
|
||||
repository.saveAll(Arrays.asList(book1, book2, book3));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
repository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByName() {
|
||||
List<Book> books = repository.findByIdName(JAVA_101);
|
||||
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByAuthor() {
|
||||
List<Book> books = repository.findByIdAuthor(JANE);
|
||||
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByGenre() {
|
||||
List<Book> books = repository.findByGenre(TECH);
|
||||
|
||||
assertEquals(2, books.size());
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.baeldung.schemageneration;
|
||||
|
||||
import com.baeldung.schemageneration.model.Account;
|
||||
import com.baeldung.schemageneration.model.AccountSetting;
|
||||
import com.baeldung.schemageneration.repository.AccountRepository;
|
||||
import com.baeldung.schemageneration.repository.AccountSettingRepository;
|
||||
import org.junit.After;
|
||||
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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AccountApplication.class)
|
||||
public class AccountRepositoryIntegrationTest {
|
||||
|
||||
private static final String USER_NAME = "Eduard";
|
||||
private static final String USER_EMAIL_ADDRESS = "eduard@gmx.com";
|
||||
private static final String ACCOUNT_SETTING_NAME = "Timezone";
|
||||
private static final String ACCOUNT_SETTING_VALUE = "UTC+02";
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private AccountSettingRepository accountSettingRepository;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
accountRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewAccount_whenSave_thenSuccess() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
accountRepository.save(account);
|
||||
|
||||
assertEquals(1, accountRepository.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedAccount_whenFindByName_thenFound() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
accountRepository.save(account);
|
||||
|
||||
Account accountFound = accountRepository.findByName(USER_NAME);
|
||||
|
||||
assertNotNull(accountFound);
|
||||
assertEquals(USER_NAME, accountFound.getName());
|
||||
assertEquals(USER_EMAIL_ADDRESS, accountFound.getEmailAddress());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedAccount_whenAccountSettingIsAdded_thenPersisted() {
|
||||
Account account = new Account(USER_NAME, USER_EMAIL_ADDRESS);
|
||||
account.addAccountSetting(new AccountSetting(ACCOUNT_SETTING_NAME, ACCOUNT_SETTING_VALUE));
|
||||
accountRepository.save(account);
|
||||
|
||||
Account accountFound = accountRepository.findByName(USER_NAME);
|
||||
assertNotNull(accountFound);
|
||||
AccountSetting accountSetting = accountSettingRepository.findByAccountId(accountFound.getId());
|
||||
|
||||
assertNotNull(accountSetting);
|
||||
assertEquals(ACCOUNT_SETTING_NAME, accountSetting.getSettingName());
|
||||
assertEquals(ACCOUNT_SETTING_VALUE, accountSetting.getSettingValue());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:mem:baeldung
|
||||
|
||||
Reference in New Issue
Block a user