Merge pull request #9014 from dupirefr/author/BAEL-2808-uppercase_table_names_in_spring_jpa

[BAEL-2808] Customize Table Names Generation with Spring JPA
This commit is contained in:
Eric Martin 2020-04-12 11:21:17 -05:00 committed by GitHub
commit d2d13f075e
29 changed files with 1036 additions and 0 deletions

View File

@ -0,0 +1,2 @@
create table PERSON (ID int8 not null, FIRST_NAME varchar(255), LAST_NAME varchar(255), primary key (ID))
create table person (id int8 not null, first_name varchar(255), last_name varchar(255), primary key (id))

View File

@ -33,6 +33,11 @@
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>

View File

@ -0,0 +1,35 @@
package com.baeldung.namingstrategy;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
public Person() {}
public Person(Long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Long id() {
return id;
}
public String firstName() {
return firstName;
}
public String lastName() {
return lastName;
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.namingstrategy;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person, Long> {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class QuotedLowerCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toLowerCase(), true);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class QuotedUpperCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toUpperCase(), true);
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.namingstrategy;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataJpaNamingConventionApplication {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class UnquotedLowerCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toLowerCase(), false);
}
}

View File

@ -0,0 +1,12 @@
package com.baeldung.namingstrategy;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;
public class UnquotedUpperCaseNamingStrategy extends SpringPhysicalNamingStrategy {
@Override
protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) {
return new Identifier(name.toUpperCase(), false);
}
}

View File

@ -0,0 +1,81 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("quoted-lower-case-naming-strategy.properties")
class QuotedLowerCaseNamingStrategyH2IntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Unexpected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("quoted-lower-case-naming-strategy-on-postgres.properties")
class QuotedLowerCaseNamingStrategyPostgresLiveTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("quoted-upper-case-naming-strategy.properties")
class QuotedUpperCaseNamingStrategyH2IntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenException(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,81 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("quoted-upper-case-naming-strategy-on-postgres.properties")
class QuotedUpperCaseNamingStrategyPostgresLiveTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Unexpected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("spring-physical-naming-strategy.properties")
class SpringPhysicalNamingStrategyH2IntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndSpringNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Unexpected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Unexpected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("spring-physical-naming-strategy-on-postgres.properties")
class SpringPhysicalNamingStrategyPostgresLiveTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndSpringNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndSpringNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,86 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("unquoted-lower-case-naming-strategy.properties")
class UnquotedLowerCaseNamingStrategyH2IntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Unexpected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Unexpected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("unquoted-lower-case-naming-strategy-on-postgres.properties")
class UnquotedLowerCaseNamingStrategyPostgresLiveTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndLowerCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("unquoted-upper-case-naming-strategy.properties")
class UnquotedUpperCaseNamingStrategyH2IntegrationTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Expected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,85 @@
package com.baeldung.namingstrategy;
import org.hibernate.exception.SQLGrammarException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.TestPropertySource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
@DataJpaTest(excludeAutoConfiguration = TestDatabaseAutoConfiguration.class)
@TestPropertySource("unquoted-upper-case-naming-strategy-on-postgres.properties")
class UnquotedUpperCaseNamingStrategyPostgresLiveTest {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private PersonRepository personRepository;
@BeforeEach
void insertPeople() {
personRepository.saveAll(Arrays.asList(
new Person(1L, "John", "Doe"),
new Person(2L, "Jane", "Doe"),
new Person(3L, "Ted", "Mosby")
));
}
@ParameterizedTest
@ValueSource(strings = {"person", "PERSON", "Person"})
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonUnquoted_thenResult(String tableName) {
Query query = entityManager.createNativeQuery("select * from " + tableName);
// Expected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedUpperCase_thenException() {
Query query = entityManager.createNativeQuery("select * from \"PERSON\"");
// Unexpected result
assertThrows(SQLGrammarException.class, query::getResultStream);
}
@Test
void givenPeopleAndUpperCaseNamingStrategy_whenQueryPersonQuotedLowerCase_thenResult() {
Query query = entityManager.createNativeQuery("select * from \"person\"");
// Unexpected result
List<Person> result = (List<Person>) query.getResultStream()
.map(this::fromDatabase)
.collect(Collectors.toList());
assertThat(result).isNotEmpty();
}
public Person fromDatabase(Object databaseRow) {
Object[] typedDatabaseRow = (Object[]) databaseRow;
return new Person(
((BigInteger) typedDatabaseRow[0]).longValue(),
(String) typedDatabaseRow[1],
(String) typedDatabaseRow[2]
);
}
}

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/quoted-lower-case-strategy
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:quoted-lower-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/quoted-upper-case-strategy
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedUpperCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:quoted-upper-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.QuotedUpperCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/spring-strategy
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=default-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:spring-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=default-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/unquoted-lower-case-strategy
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:unquoted-lower-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedLowerCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/unquoted-upper-case-strategy
spring.datasource.username=postgres
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedUpperCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql

View File

@ -0,0 +1,9 @@
spring.datasource.url=jdbc:h2:mem:unquoted-upper-case-strategy
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.naming.physical-strategy=com.baeldung.namingstrategy.UnquotedUpperCaseNamingStrategy
#spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
#spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
#spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=upper-case-naming-strategy-ddl.sql