除了使用模式验证的dbo结果之外的模式中的Hibernate访问表:缺少表

时间:2017-09-22 13:01:35

标签: java sql-server spring hibernate schema

我有一个包含两个模式的数据源: dbo 示例

我已经在dbo架构中创建了一个名为 A 的表,并将其映射到hibernate中:

@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;
    }
}

它运行没有任何预期的错误,然后我继续使用以下语句将表移动到示例模式(成功运行):

alter schema example transfer dbo.A

并改变了hibernate类:

@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;
    }
}

但是在运行它时会抛出以下异常,然后退出程序。

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)

我也尝试使用下表注释:

@Table(name = "example.A")

但它引发了一些相同的异常:

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)

似乎我需要以某种方式配置hibernate以支持额外的架构,但不了解如何。

应用:

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

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

和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>

1 个答案:

答案 0 :(得分:0)

事实证明,从hibernate.cfg.xml中删除以下行就可以了解

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