本文介绍了架构验证:缺少表[hibernate_sequences]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用Spring 4.3.3.RELEASE,Hibernate 5.2.2.Final,数据库是MySQL。我想尝试 strategy = GenerationType.TABLE 。正如我对 GenerationType.SEQUENCE 所了解的,我需要数据库中的序列来生成ID。
这是我的实体。
@Entity(name = CommentTable.TABLE_NAME)
public class Comment {
private Integer id;
私有字符串标题;
私人字符串消息;
私人LocalDateTime createdDateTime;
私人问题;
public注释(){
}
@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;
}
//其他getter和setter。
}
Spring注解配置
@Configuration
@ComponentScan(com.ita.training.otm)
@ImportResource(classpath:/config.xml)// XML with DataSource bean
@EnableTransactionManagement
public class AppConfiguration {
}
Spring xml配置
<?xml version =1.0encoding =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。组织/架构/ TX /弹簧tx.xsd>
< bean id =emfclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>
< property name =dataSourceref =dataSource/>
< property name =packagesToScanvalue =com.ita.training.otm.core.model/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter/>
< / property>
< property name =jpaProperties>
<道具>
< prop key =hibernate.hbm2ddl.auto>验证< / prop>
< prop key =hibernate.dialect> org.hibernate.dialect.MySQL5Dialect< / prop>
< /道具>
< / property>
< / bean>
< bean id =dataSourceclass =org.springframework.jdbc.datasource.DriverManagerDataSource>
< property name =driverClassNamevalue =com.mysql.jdbc.Driver/>
< property name =urlvalue =jdbc:mysql:// localhost:3306 / spring_test/>
< property name =usernamevalue =user/>
< property name =passwordvalue =1111/>
< / bean>
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager>
< property name =entityManagerFactoryref =emf/>
< property name =dataSourceref =dataSource/>
< / bean>
< tx:annotation-driven />
< / beans>
当我运行我的应用程序时,我得到
线程main中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源[config.xml]中定义名称为'emf'的bean时创建错误:调用init方法失败;嵌套异常是javax.persistence.PersistenceException:[PersistenceUnit:默认]无法在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
来构建的SessionFactory
。在有机.bringframework.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)
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
。在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBea)中的
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) nFactory.java:197)美元,org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076 B $ B)
在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java: 851)在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541
)
。在org.springframework.context.annotation.AnnotationConfigApplicationContext<初始化>(AnnotationConfigApplicationContext.java:84 )
在com.ita.training.otm.app.Main.main(Main.java:10)
导致:javax.persistence.PersistenceException:[PersistenceUnit:default]无法构建Hibernate SessionFactory $在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:951)b
$ b。在org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:881)
在org.springframework.orm.j pa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
位于org.springframework.orm.jpa的org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)
。 AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:373)在org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:362)处
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory处
。 invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
... 11个
产生的原因:组织.hibernate.tool.schema.spi.SchemaManagementException:架构验证:缺少表[hibernate_sequence s]
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:
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
解决方案
且发生红色,因为您还没有在 @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 use Spring 4.3.3.RELEASE, Hibernate 5.2.2.Final, the database is MySQL.I wanted to try strategy = GenerationType.TABLE. As I know for GenerationType.SEQUENCE I need sequences in the database to generate ids.
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
@Configuration
@ComponentScan("com.ita.training.otm")
@ImportResource("classpath:/config.xml") // XML with DataSource bean
@EnableTransactionManagement
public class AppConfiguration {
}
Spring xml configuration
<?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(strategy=GenerationType.TABLE, generator="course")
@TableGenerator(
name="course",
table="GENERATOR_TABLE",
pkColumnName = "key",
valueColumnName = "next",
pkColumnValue="course",
allocationSize=30
)
private int id;
这篇关于架构验证:缺少表[hibernate_sequences]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!