BAEL-3971: Generate DDL script in Spring (#9120)
* BAEL-3971: Generate DDL script in Spring * BAEL-3971: implement review comments * BAEL-3971: fix typo * BAEL-3971: move code examples to module spring-data-jpa-5 Co-authored-by: ashleyfrieze <ashley@incredible.org.uk>
This commit is contained in:
parent
2d7bc17dda
commit
0cad7c5cc5
@ -61,6 +61,7 @@
|
||||
<module>spring-data-jpa-2</module>
|
||||
<module>spring-data-jpa-3</module>
|
||||
<module>spring-data-jpa-4</module>
|
||||
<module>spring-data-jpa-5</module>
|
||||
<module>spring-data-keyvalue</module>
|
||||
<module>spring-data-mongodb</module>
|
||||
<module>spring-data-neo4j</module>
|
||||
|
@ -1,4 +1,5 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring JPA @Embedded and @EmbeddedId](TBD)
|
||||
|
||||
### Eclipse Config
|
||||
@ -9,3 +10,4 @@ This can be ignored:
|
||||
- Project -> Properties -> Java Persistance -> JPA -> Error/Warnings -> Select Ignore on "No persistence xml file found in project"
|
||||
Or:
|
||||
- Eclipse -> Preferences - Validation - disable the "Build" execution of the JPA Validator
|
||||
|
||||
|
@ -33,5 +33,5 @@
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
@ -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);
|
||||
}
|
@ -1,2 +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
|
||||
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -1 +1,3 @@
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:mem:baeldung
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user