簡體   English   中英

使用Schema-validation驗證除dbo結果以外的其他模式中的休眠訪問表:缺少表

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

我有一個具有兩種模式的數據源: dboexample

我已經在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;
    }
}

它運行時沒有出現預期的錯誤,然后我使用以下語句將表移動到示例架構(成功運行):

alter schema example transfer dbo.A

並更改了休眠類:

@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)

看來我需要以某種方式配置休眠以支持額外的架構,但不了解如何。

應用:

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>

原來,從hibernate.cfg.xml中刪除以下行可以解決問題

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM