简体   繁体   English

为什么Hibernate模式验证无法验证模式

[英]Why Hibernate schema-validation fail to validate schema

The same question has been asked number of times but none of them has solution to my problem. 曾多次问过同一问题,但没有一个能解决我的问题。

I have created a hibernate + H2 + Sprinvg mvc project. 我创建了一个休眠+ H2 + Sprinvg mvc项目。 I am using java based configuration. 我正在使用基于Java的配置。 I have the following beans related to Datasource, SessionFactory and TransactionManager 我有以下与Datasource,SessionFactory和TransactionManager相关的bean

@Configuration
@ComponentScan(basePackages="org.testpackage")
@EnableWebMvc
@EnableTransactionManagement

public class MyConfiguration extends WebMvcConfigurationSupport {


@Bean(initMethod="start",destroyMethod="stop")
public org.h2.tools.Server h2WebConsonleServer () throws SQLException {
  return org.h2.tools.Server.createWebServer("-web","-webAllowOthers","- 
  webDaemon","-webPort", "8082");
}

@Bean
public DataSource getDataSource() {     
    return new EmbeddedDatabaseBuilder()
            .generateUniqueName(false)
            .setName("mytestdb")
            .setType(EmbeddedDatabaseType.H2)
            .addDefaultScripts()
            .setScriptEncoding("UTF-8")
            .ignoreFailedDrops(true)
            .build();
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
    final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(getDataSource());
    sessionFactory.setHibernateProperties(hibernateProperties());
    sessionFactory.setPackagesToScan(new String[] {"org.testpackage.model"});

    return sessionFactory;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(final SessionFactory sessionFactory) {
    final HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);

    return txManager;
}

final Properties hibernateProperties() {
    final Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "validate");
    hibernateProperties.setProperty("hibernate.show_sql", "true");

    return hibernateProperties;
}
  //Some more beans  
}

I have the following Entity class 我有以下实体类

@Entity
@Table(name = "MYTESTDB.TEST_TABLE")
public class User{

@Id
@GeneratedValue
@Column(name = "id")
private int id;

@Column(name = "name")
private String name;

@Column(name = "email", unique = true)
private String email;


public User(int id, String name, String email) {
    super();
    this.id = id;
    this.name = name;
    this.email = email;
}

public User() {
}
//Getters and Setters
}

in the DataSource bean I am using addDefaultScripts() and I have 2 sql scripts which create Schema in H2 and insert some predefined value. 在DataSource bean中,我正在使用addDefaultScripts(),并且我有2个sql脚本,它们在H2中创建模式并插入一些预定义的值。 Which are as follows. 如下。

//schema.sql Script
CREATE SCHEMA `MYTESTDB` ;
Drop TABLE IF EXISTS MYTESTDBDB.TEST_TABLE;
CREATE TABLE MYTESTDB.TEST_TABLE ( 
   ID INT NOT NULL PRIMARY KEY, 
   NAME VARCHAR(50) NOT NULL, 
   EMAIL VARCHAR(20) NOT NULL, 
   );
CREATE UNIQUE INDEX ON MYTESTDB.TEST_TABLE (EMAIL)

//data.sql Script
INSERT INTO MYTESTDB.TEST_TABLE(id, name, email) 
VALUES ('1', 'Tom', 'tom12@hotmail.com');

If I use hibernate hbm2ddl.auto property value "create" everything works fine, hibernate drops the table and recreate it. 如果我使用hibernate hbm2ddl.auto属性值“ create”,一切正常,hibernate删除表并重新创建它。 I have verified it from the web browser. 我已经从网络浏览器验证了它。 But if I use "validate" property I get the following error exception 但是,如果我使用“ validate”属性,则会收到以下错误异常

Error creating bean with name 'sessionFactory' defined in org.testPackage.configuration.MYConfiguration: Invocation of init method    failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [MYTESTDB.TEST_TABLE]

Can anyone please help me to find out the problem? 谁能帮我找出问题所在?

Its working now. 现在正在工作。 With the help of @Slaw I am able to use "Validate" property. 在@Slaw的帮助下,我可以使用“ Validate”属性。 Use @Table(schema = "MYTESTDB", name = "TEST_TABLE") instead of @Table(name = "MYTESTDB.TEST_TABLE") . 使用@Table(schema = "MYTESTDB", name = "TEST_TABLE")而不是@Table(name = "MYTESTDB.TEST_TABLE") But I had to change a bit more in user entity class. 但是我必须在用户实体类中进行更多更改。 Instead of @GeneratedValue annotation I added @GeneratedValue(strategy = GenerationType.IDENTITY) . 代替@GeneratedValue注释,我添加了@GeneratedValue(strategy = GenerationType.IDENTITY) Now everything is working fine. 现在一切正常。

Thanks @Slaw and @Mykhailo for your valuable time. 感谢@Slaw和@Mykhailo宝贵的时间。

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

相关问题 Hibernate + JPA:模式验证:缺少列 - Hibernate + JPA: Schema-validation: missing column Hibernate继承:模式验证:缺少列 - Hibernate inheritance: Schema-validation: missing column 继承的实体类的休眠模式验证错误 - Hibernate Schema-validation error for the inherited entity class Hibernate 5 模式验证:缺少带有 HBM 文件的表 - Hibernate 5 Schema-validation: missing table with HBM files Java Spring Hibernate Schema-Validation:缺少表 - Java Spring Hibernate Schema-Validation: Missing Table 架构验证:缺少表 [hibernate_sequences] - Schema-validation: missing table [hibernate_sequences] 使用Schema-validation验证除dbo结果以外的其他模式中的休眠访问表:缺少表 - Hibernate acessing table in schema other than dbo results with Schema-validation: missing table Hibernate:模式验证:缺少表 - 区分大小写 Mysql 模式标识符变为大写 - Hibernate: Schema-validation: missing table - case-sensitive Mysql schema identifier turned upper-case Spring Boot 项目由于 Schema-validation 无法运行:缺少序列 [hibernate_sequence] - Spring Boot project fails to run because of Schema-validation: missing sequence [hibernate_sequence] Java Hibernate -Mysql Schema-validation:列中遇到错误的列类型 - Java Hibernate -Mysql Schema-validation: wrong column type encountered in column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM