代码之家  ›  专栏  ›  技术社区  ›  PeterMmm

无法从Hibernate创建架构

  •  0
  • PeterMmm  · 技术社区  · 16 年前

    我试图将模式从Hibernate配置传播到RDBMS。代码运行时没有任何错误消息,但数据库没有得到更新。

    有什么提示吗?非常感谢。

    这就是hibernate核心,只有一个HSQL数据库。

    是的,我应该使用SchemaExport(我离开hibernate一段时间了),但它不会刷新到数据库。它是一个HSQL进程内数据库(jdbc:hsqldb:file:config/config)。

    更新3

     public static void exportSchema() {
            new SchemaExport(hbConfig).create(true, true);
        }
    
    
    public static void exportSchemaXXX() {
    
    // sessionFactory and hbConfig defined in the class
    
            Session sess = sessionFactory.openSession();
    
            sess.doWork(new Work() {
    
                public void execute(java.sql.Connection conn) throws SQLException {
                    System.err.println("work");
                    try {
                        Class dialect = Class.forName(hbConfig.getProperty("hibernate.dialect"));
                        String[] lines = hbConfig.generateSchemaCreationScript((Dialect) dialect.newInstance());
    
                        for (String s : lines) {
                            System.err.println(s);
                            Statement stm = conn.createStatement();
                            stm.execute(s);
                        }
                    } catch (Exception ex) {
                        System.err.println("Error: " + ex);
                    }
    
                }
            });
    
            sess.flush();
            sess.close();
        }
    
    3 回复  |  直到 15 年前
        1
  •  0
  •   Adam Batkin    16 年前

    确保您的事务得到提交。如果事务回滚,某些数据库实际上会回滚架构更改。

        2
  •  0
  •   Jared    16 年前

    您可以尝试在完成后强制提交,但不知道您使用的数据库,我不确定这是否是问题所在。假设您使用pojo来表示您的数据库模式,为什么您要通过JDBC连接逐个执行语句,而不是使用类似于以下内容的Hibernates内置模式类?

    config=new AnnotationConfiguration()
    config.addAnnotatedClass(Badge.class)
    config.addAnnotatedClass(Vote.class)
    config.configure()
    new SchemaExport(config).create(true,true)//create the database tables

    有关此项的更多信息,请参阅 this link

        3
  •  0
  •   PeterMmm    16 年前