【问题标题】:hibernate_sequence table is not being created in MySQL未在 MySQL 中创建 hibernate_sequence 表
【发布时间】:2016-11-21 18:20:17
【问题描述】:

我在 Spring Boot 应用程序中使用 MySQL 作为数据库。我删除了数据库并再次创建它并运行了 Junit 测试,它现在没有创建 hibernate_sequence 表,而是在它确实拥有该表之前。

我的实体类是这样的

@Entity
public class Greeting {

@Id
@GeneratedValue
private Long id;

private String text;
//getters & setters
}

我的休眠属性

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.connection.sid=xe
spring.jpa.properties.hibernate.connection.characterEncoding=utf8
spring.jpa.properties.hibernate.connection.CharSet=utf8
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.LocalSessionFactoryBean
spring.jpa.properties.hibernate.id.new_generator_mappings=true

我的 Junit 测试

@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@SpringBootTest
public class GreetingRepositoryDAOTest {
    @Resource
    private BaseRepository greetingRepositoryImpl;
    @Test
    public void test() {
        Greeting gr = new Greeting();
        gr.setText("Third Text");
        greetingRepositoryImpl.save(gr);
    }
}

【问题讨论】:

    标签: java hibernate spring-boot


    【解决方案1】:

    hbm2ddl values

    使用create 而不是update

    例子:

    spring.jpa.properties.hibernate.hbm2ddl.auto=create
    

    Database URL

    将以下参数添加到您的数据库 url 以自动创建数据库:createDatabaseIfNotExist=true

    例子:

    spring.datasource.url=jdbc:mysql://localhost/testdb?createDatabaseIfNotExist=true
    

    【讨论】:

    • 应用程序启动期间 Spring Boot 的控制台输出是什么?您将看到可能指向问题的 Hibernate 日志条目。
    • 当我运行 Spring Boot 应用程序时没有错误,只有当我尝试通过 DAO 添加数据时才会出现错误。
    • 明白。但我希望在应用程序启动期间查看日志条目,因为它们可以帮助识别许多内容,包括:配置文件、休眠配置等。
    • 运行Junit测试时出错-anotepad.com/notes/2ecwe5springboot应用运行时的日志-anotepad.com/notes/enh83d
    • @DeepakSunandaPrabhakar 我已经修改了我的答案以包含更多信息。
    【解决方案2】:

    我不知道,我是对还是错。我希望它会工作。

    @Entity
    public class Greeting {
    
    @Id
    //GenerationType.IDENTITY use for create primary key
    //GenerationType.AUTO use for create table without primary key
    //for this reason hibernate create a table hibernate_sequence that store 
    //last value (auto incremented value)
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    
    private String text;
    //getters & setters
    }
    

    【讨论】:

      猜你喜欢
      • 2019-01-04
      • 2019-10-27
      • 2014-02-25
      • 1970-01-01
      • 2018-09-13
      • 2017-05-18
      • 2018-01-22
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多