简体   繁体   English

架构验证:缺少表 [hibernate_sequences]

[英]Schema-validation: missing table [hibernate_sequences]

I use Spring 4.3.3.RELEASE, Hibernate 5.2.2.Final, the database is MySQL. I wanted to try strategy = GenerationType.TABLE .我用的是Spring 4.3.3.RELEASE,Hibernate 5.2.2.Final,数据库是MySQL。我想试试strategy = GenerationType.TABLE As I know for GenerationType.SEQUENCE I need sequences in the database to generate ids.据我所知,对于GenerationType.SEQUENCE ,我需要数据库中的序列来生成 ID。

This is my entity.这是我的实体。

@Entity(name = CommentTable.TABLE_NAME)
public class Comment {

    private Integer id;
    private String title;
    private String message;
    private LocalDateTime createdDateTime;
    private Issue issue;


   public Comment() {
   }

   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
   @Column(name = CommentTable.COLUMN_ID, unique = true, nullable = false)
   public Integer getId() {
       return id;
   }

   public void setId(Integer id) {
       this.id = id;
   }
//Other getters and setters.
}

Spring annotation configuration Spring注解配置

@Configuration
@ComponentScan("com.ita.training.otm") 
@ImportResource("classpath:/config.xml") // XML with DataSource bean
@EnableTransactionManagement
public class AppConfiguration {
}

Spring xml configuration Spring xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="emf"      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.ita.training.otm.core.model" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_test" />
        <property name="username" value="user" />
        <property name="password" value="1111" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven />

</beans>

When I run my application, I get当我运行我的应用程序时,我得到

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in class path resource [config.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.ita.training.otm.app.Main.main(Main.java:10)
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:951)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
... 11 more
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [hibernate_sequences]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:125)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.performValidation(SchemaValidatorImpl.java:95)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:62)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:184)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:65)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:307)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:490)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:878)
... 17 more

The exception is occured because you haven't mentioned generator in @GeneratedValue ..Below is the example 发生异常是因为您没有在@GeneratedValue提到generator 。下面是示例

    @GeneratedValue(strategy=GenerationType.TABLE, generator="course")
        @TableGenerator(
                name="course",
                table="GENERATOR_TABLE",
                pkColumnName = "key",
                valueColumnName = "next",
                pkColumnValue="course",
                allocationSize=30
            )
    private int id;

I solved it by using the property我通过使用属性解决了它

spring:
  jpa:
    hibernate:
      use-new-id-generator-mappings: false

I was using spring boot V2.5.9我用的是spring boot V2.5.9

With this setting the hibernate sequence is generated if missing.使用此设置,如果缺少,则会生成 hibernate 序列。 Then also no need for the @GeneratedValue annotation.然后也不需要@GeneratedValue注释。

I had to keep this project in runnable mode before I start working on my actual task, I have solved this exception and started my application with following steps:在开始执行实际任务之前,我必须将此项目保持在可运行模式,我已经解决了这个异常并通过以下步骤启动了我的应用程序:

  1. Created a new database schema in my local MYSQL server.在我的本地 MYSQL 服务器中创建了一个新的数据库模式。 and added it to properties file.并将其添加到属性文件中。<\/li>
  2. Changed hibernate dialect to org.hibernate.dialect.MySQL5Dialect将休眠方言更改为 org.hibernate.dialect.MySQL5Dialect<\/li>
  3. Changed spring.jpa.hibernate.ddl-auto from "validate" to "create-drop" option将 spring.jpa.hibernate.ddl-auto 从“validate”更改为“create-drop”选项<\/li>
  4. Restarted the Application.重新启动应用程序。<\/li><\/ol>

    Actual problem that I understood is, the existing database schema had tables, constraints and data which is not in sync with my current code.我理解的实际问题是,现有的数据库模式包含与我当前代码不同步的表、约束和数据。

    My Goal is to make my application to Start and these steps helped me.我的目标是让我的应用程序开始,这些步骤帮助了我。

    "

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring Hibernate Schema-Validation:缺少表 - Java Spring Hibernate Schema-Validation: Missing Table Hibernate + JPA:模式验证:缺少列 - Hibernate + JPA: Schema-validation: missing column Hibernate继承:模式验证:缺少列 - Hibernate inheritance: Schema-validation: missing column 使用Schema-validation验证除dbo结果以外的其他模式中的休眠访问表:缺少表 - Hibernate acessing table in schema other than dbo results with Schema-validation: missing table Hibernate无法创建表hibernate_sequences - Hibernate cannot create table hibernate_sequences SqlServer2016,Hibernate 模式验证:缺少表,但表存在于数据库中 - SqlServer2016, Hibernate schema-validation: Missing table, but table exists in db 模式验证:使用Flyway的Hibernate生成的SQL代码缺少表[…]错误消息 - Schema-validation: missing table […] error message using flyway for a SQL code generated by Hibernate 获取 entityManageFactory 错误。 模式验证:缺少表 [hibernate_sequence] - Getting entityManageFactory error. Schema-validation: missing table [hibernate_sequence] Spring Boot 项目由于 Schema-validation 无法运行:缺少序列 [hibernate_sequence] - Spring Boot project fails to run because of Schema-validation: missing sequence [hibernate_sequence] SchemaManagementException:模式验证:即使数据库中存在表,也缺少表 [chk_groups] - SchemaManagementException: Schema-validation: missing table [chk_groups] even when table is present in DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM