springboot + JPA + MySql +实体表生成为大写

时间:2019-04-25 00:10:14

标签: mysql hibernate spring-boot jpa entity

有一个问题,在使用Hibernate到MySql的SpringBoot 2.1.4 JPA中,生成大写的表名,并且我不断收到错误的表,该表无法识别为 CONTENTSET 。 MySql中的实际表名是小写的 contentset

从日志文件生成的sql看起来不错,但是我的测试使错误避免抱怨大写表名。这是MySql数据库,表名区分大小写。

这是日志记录信息文本

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CONTENTSET" not found; SQL statement:
select contentset0_.contentsetid as contents1_1_, contentset0_.checksum as checksum2_1_, contentset0_.contentsetname as contents3_1_, contentset0_.zipped as zipped4_1_ from contentset contentset0_ [42102-199]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:451)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:427)
    at org.h2.message.DbException.get(DbException.java:205)

实体Bean

@Entity
@Table(name = "contentset")
public class ContentSet implements Serializable {

    @Id
    @Column(name="CONTENTSETID")
    private String contentSetId;

    @Column(name="CONTENTSETNAME", nullable=false)
    private String contentSetName;
    ......

ContentSetRepository.java文件

public interface ContentSetRepository extends JpaRepository<ContentSet, String> { }

我的application.properties测试

spring.datasource.url=jdbc:mysql://10.80.100.62:3306/receiver
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database = MYSQL
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.hibernate.ddl-auto = none
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

# NOTHING SEEMS TO RESOLVE UPPERCASE TABLE name
#spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
#spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

我的测试一直失败

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.AUTO_CONFIGURED)  
@TestPropertySource(locations = "classpath:application.properties")
public class ContentSetRepositoryMySqlTest {

    @Autowired private ContentSetRepository contentSetRepo;

    @Test
    public void testFindAll_For_ContentSetRepository() {
        Assertions.assertThat(contentSetRepo).isNotNull();

        try {
            Collection<ContentSet> all = contentSetRepo.findAll();
            Assertions.assertThat(all).isNotNull();
            Assertions.assertThat(all.size()).isGreaterThan(0);
        } catch(Exception ex ) {
            ex.printStackTrace();
            Assertions.fail("Exception : " + ex.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

也许您在pom.xml中包含了H2数据库依赖关系,或者您可能有与H2数据库有关的bean,所以错误提示:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CONTENTSET" not found; SQL statement:

表名和列名在H2中区分大小写,但在MySQL中不区分大小写。您可以使用pom.xml的内容来更新问题。