简体   繁体   English

使用Schema-validation验证除dbo结果以外的其他模式中的休眠访问表:缺少表

[英]Hibernate acessing table in schema other than dbo results with Schema-validation: missing table

I have a datasource that has two schemas: dbo and example . 我有一个具有两种模式的数据源: dboexample

I've created a table in dbo schema called A , and mapped it in hibernate: 我已经在dbo模式中创建了一个名为A的表,并将其映射到休眠状态:

@Entity
@Table(name = "A")
public class ATable {
    private Integer id;

    @Id
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

It runs without any errors as expected, I then proceeded to move the table to example schema with the following statement (runs successfully): 它运行时没有出现预期的错误,然后我使用以下语句将表移动到示例架构(成功运行):

alter schema example transfer dbo.A

And changed the hibernate class as well: 并更改了休眠类:

@Entity
@Table(name = "A", schema = "example")
public class ATable {
    private Integer id;

    @Id
    @Column(name = "id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
}

But upon running it, it throws the following exception, and exit the program. 但是在运行它时,它将引发以下异常,并退出程序。

INFO: HHH000262: Table not found: A
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [A]
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:130)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.performValidation(SchemaValidatorImpl.java:100)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:65)
    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:459)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:711)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:727)
    at com.venditio.Application.main(Application.java:21)

I also tried using the following table annotation instead: 我还尝试使用以下表注释代替:

@Table(name = "example.A")

But it throws somewhat the same exception: 但这引发了一些相同的异常:

INFO: HHH000262: Table not found: example.A
Sep 22, 2017 3:53:36 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: example.A
Exception in thread "main" org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [example.A]
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:130)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.performValidation(SchemaValidatorImpl.java:100)
    at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:65)
    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:459)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:711)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:727)
    at com.venditio.Application.main(Application.java:21)

It seems that I need to somehow configure hibernate to support the extra schema, but don't understand how. 看来我需要以某种方式配置休眠以支持额外的架构,但不了解如何。

Application: 应用:

public class Application {
    private static SessionFactory sessionFactory;
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();
        sessionFactory = configuration.buildSessionFactory();

        sessionFactory.openSession().close();
    }
}

And hibernate.cfg.xml 还有hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!--MSSQL-->
        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/d1</property>
        <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

        <property name="hibernate.connection.username">a</property>
        <property name="hibernate.connection.password">1</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!--Global-->
        <property name="hibernate.connection.pool_size">10</property>
        <property name="show_sql">true</property>
        <property name="hibernate.hbm2ddl.auto">validate</property>
        <property name="hibernate.current_session_context_class">thread</property>

        <mapping class="com.a.A" />
    </session-factory>
</hibernate-configuration>

原来,从hibernate.cfg.xml中删除以下行可以解决问题

<property name="hibernate.hbm2ddl.auto">validate</property>

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

相关问题 Java Spring Hibernate Schema-Validation:缺少表 - Java Spring Hibernate Schema-Validation: Missing Table 架构验证:缺少表 [hibernate_sequences] - Schema-validation: missing table [hibernate_sequences] SqlServer2016,Hibernate 模式验证:缺少表,但表存在于数据库中 - SqlServer2016, Hibernate schema-validation: Missing table, but table exists in db Hibernate + JPA:模式验证:缺少列 - Hibernate + JPA: Schema-validation: missing column Hibernate继承:模式验证:缺少列 - Hibernate inheritance: Schema-validation: missing column 模式验证:使用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] SchemaManagementException:模式验证:即使数据库中存在表,也缺少表 [chk_groups] - SchemaManagementException: Schema-validation: missing table [chk_groups] even when table is present in DB 为什么Hibernate模式验证无法验证模式 - Why Hibernate schema-validation fail to validate schema Spring Boot 项目由于 Schema-validation 无法运行:缺少序列 [hibernate_sequence] - Spring Boot project fails to run because of Schema-validation: missing sequence [hibernate_sequence]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM